mirror of
https://github.com/nais/wonderwall.git
synced 2026-02-14 17:49:54 +00:00
feat(session): add even more tracing
Co-authored-by: sindrerh2 <sindre.rodseth.hansen@nav.no>
This commit is contained in:
@@ -7,7 +7,9 @@ import (
|
||||
"time"
|
||||
|
||||
"github.com/bsm/redislock"
|
||||
"github.com/nais/wonderwall/internal/o11y/otel"
|
||||
"github.com/redis/go-redis/v9"
|
||||
"go.opentelemetry.io/otel/attribute"
|
||||
)
|
||||
|
||||
const (
|
||||
@@ -37,6 +39,11 @@ func NewRedisLock(client redis.Cmdable, key string) *RedisLock {
|
||||
}
|
||||
|
||||
func (r *RedisLock) Acquire(ctx context.Context, duration time.Duration) error {
|
||||
ctx, span := otel.StartSpan(ctx, "RedisLock.Acquire")
|
||||
defer span.End()
|
||||
span.SetAttributes(attribute.String("redis.lock_duration", duration.String()))
|
||||
span.SetAttributes(attribute.Bool("redis.lock_acquired", false))
|
||||
|
||||
lock, err := r.locker.Obtain(ctx, lockKey(r.key), duration, nil)
|
||||
if errors.Is(err, redislock.ErrNotObtained) {
|
||||
return ErrAcquireLock
|
||||
@@ -46,10 +53,13 @@ func (r *RedisLock) Acquire(ctx context.Context, duration time.Duration) error {
|
||||
}
|
||||
|
||||
r.lock = lock
|
||||
span.SetAttributes(attribute.Bool("redis.lock_acquired", true))
|
||||
return nil
|
||||
}
|
||||
|
||||
func (r *RedisLock) Release(ctx context.Context) error {
|
||||
ctx, span := otel.StartSpan(ctx, "RedisLock.Release")
|
||||
defer span.End()
|
||||
return r.lock.Release(ctx)
|
||||
}
|
||||
|
||||
|
||||
@@ -40,10 +40,8 @@ func (in *reader) Get(r *http.Request) (*Session, error) {
|
||||
|
||||
ticket, err := getTicket(r, in.cookieCrypter)
|
||||
if err != nil {
|
||||
span.SetAttributes(attribute.Bool("session.valid_ticket", false))
|
||||
return nil, err
|
||||
}
|
||||
span.SetAttributes(attribute.Bool("session.valid_ticket", true))
|
||||
|
||||
return in.getForTicket(r.Context(), ticket)
|
||||
}
|
||||
|
||||
@@ -6,9 +6,10 @@ import (
|
||||
"fmt"
|
||||
"time"
|
||||
|
||||
"github.com/redis/go-redis/v9"
|
||||
|
||||
"github.com/nais/wonderwall/internal/o11y/otel"
|
||||
"github.com/nais/wonderwall/pkg/metrics"
|
||||
"github.com/redis/go-redis/v9"
|
||||
"go.opentelemetry.io/otel/attribute"
|
||||
)
|
||||
|
||||
type redisSessionStore struct {
|
||||
@@ -24,11 +25,16 @@ func NewRedis(client redis.Cmdable) Store {
|
||||
}
|
||||
|
||||
func (s *redisSessionStore) Read(ctx context.Context, key string) (*EncryptedData, error) {
|
||||
ctx, span := otel.StartSpan(ctx, "RedisSessionStore.Read")
|
||||
defer span.End()
|
||||
span.SetAttributes(attribute.Bool("redis.key_exists", false))
|
||||
|
||||
encryptedData := &EncryptedData{}
|
||||
err := metrics.ObserveRedisLatency(metrics.RedisOperationRead, func() error {
|
||||
return s.client.Get(ctx, key).Scan(encryptedData)
|
||||
})
|
||||
if err == nil {
|
||||
span.SetAttributes(attribute.Bool("redis.key_exists", true))
|
||||
return encryptedData, nil
|
||||
}
|
||||
|
||||
@@ -40,6 +46,10 @@ func (s *redisSessionStore) Read(ctx context.Context, key string) (*EncryptedDat
|
||||
}
|
||||
|
||||
func (s *redisSessionStore) Write(ctx context.Context, key string, value *EncryptedData, expiration time.Duration) error {
|
||||
ctx, span := otel.StartSpan(ctx, "RedisSessionStore.Write")
|
||||
defer span.End()
|
||||
span.SetAttributes(attribute.String("redis.key_expiry", expiration.String()))
|
||||
|
||||
err := metrics.ObserveRedisLatency(metrics.RedisOperationWrite, func() error {
|
||||
return s.client.Set(ctx, key, value, expiration).Err()
|
||||
})
|
||||
@@ -51,6 +61,9 @@ func (s *redisSessionStore) Write(ctx context.Context, key string, value *Encryp
|
||||
}
|
||||
|
||||
func (s *redisSessionStore) Delete(ctx context.Context, keys ...string) error {
|
||||
ctx, span := otel.StartSpan(ctx, "RedisSessionStore.Delete")
|
||||
defer span.End()
|
||||
|
||||
err := metrics.ObserveRedisLatency(metrics.RedisOperationDelete, func() error {
|
||||
return s.client.Del(ctx, keys...).Err()
|
||||
})
|
||||
@@ -66,6 +79,9 @@ func (s *redisSessionStore) Delete(ctx context.Context, keys ...string) error {
|
||||
}
|
||||
|
||||
func (s *redisSessionStore) Update(ctx context.Context, key string, value *EncryptedData) error {
|
||||
ctx, span := otel.StartSpan(ctx, "RedisSessionStore.Update")
|
||||
defer span.End()
|
||||
|
||||
_, err := s.Read(ctx, key)
|
||||
if err != nil {
|
||||
return err
|
||||
|
||||
@@ -7,6 +7,8 @@ import (
|
||||
"net/http"
|
||||
|
||||
"github.com/nais/liberator/pkg/keygen"
|
||||
"github.com/nais/wonderwall/internal/o11y/otel"
|
||||
"go.opentelemetry.io/otel/attribute"
|
||||
|
||||
"github.com/nais/wonderwall/internal/crypto"
|
||||
"github.com/nais/wonderwall/pkg/cookie"
|
||||
@@ -58,6 +60,10 @@ func (c *Ticket) SetCookie(w http.ResponseWriter, opts cookie.Options, crypter c
|
||||
// getTicket returns a Ticket from the session cookie found in the http.Request, given a crypto.Crypter that
|
||||
// can decrypt the cookie is provided.
|
||||
func getTicket(r *http.Request, crypter crypto.Crypter) (*Ticket, error) {
|
||||
r, span := otel.StartSpanFromRequest(r, "Session.getTicket")
|
||||
defer span.End()
|
||||
span.SetAttributes(attribute.Bool("session.valid_ticket", false))
|
||||
|
||||
ticketJson, err := cookie.GetDecrypted(r, cookie.Session, crypter)
|
||||
if errors.Is(err, http.ErrNoCookie) {
|
||||
return nil, fmt.Errorf("ticket: cookie %w", ErrNotFound)
|
||||
@@ -75,5 +81,6 @@ func getTicket(r *http.Request, crypter crypto.Crypter) (*Ticket, error) {
|
||||
return nil, fmt.Errorf("ticket: unmarshalling: %w", err)
|
||||
}
|
||||
|
||||
span.SetAttributes(attribute.Bool("session.valid_ticket", true))
|
||||
return &ticket, nil
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user