diff --git a/internal/db/connection.go b/internal/db/connection.go index 9012bebe..fff89232 100644 --- a/internal/db/connection.go +++ b/internal/db/connection.go @@ -79,6 +79,10 @@ func Initialize(ctx context.Context, schema string) error { if jobId != "" { zx = zx.Str("job_id", jobId) } + jobType := logging.JobType(ctx) + if jobType != "" { + zx = zx.Str("job_type", jobType) + } reservationId := logging.ReservationId(ctx) if reservationId != 0 { zx = zx.Int64("reservation_id", reservationId) @@ -91,7 +95,7 @@ func Initialize(ctx context.Context, schema string) error { if requestId != "" { zx = zx.Str("request_id", requestId) } - accountId := identity.AccountIdOrNil(ctx) + accountId := identity.AccountIdOrZero(ctx) if accountId != 0 { zx = zx.Int64("account_id", accountId) } diff --git a/internal/identity/account.go b/internal/identity/account.go index 79c51f89..b66d95a2 100644 --- a/internal/identity/account.go +++ b/internal/identity/account.go @@ -22,8 +22,8 @@ func AccountId(ctx context.Context) int64 { return value.(int64) } -// AccountIdOrNil returns current account model or 0 when not set. -func AccountIdOrNil(ctx context.Context) int64 { +// AccountIdOrZero returns current account model or 0 when not set. +func AccountIdOrZero(ctx context.Context) int64 { value := ctx.Value(accountIdCtxKey) if value == nil { return 0 diff --git a/internal/jobs/ctx.go b/internal/jobs/ctx.go index f5fae4ed..2274c537 100644 --- a/internal/jobs/ctx.go +++ b/internal/jobs/ctx.go @@ -18,6 +18,7 @@ func copyContext(ctx context.Context) context.Context { nCtx = logging.WithTraceId(nCtx, logging.TraceId(ctx)) nCtx = logging.WithEdgeRequestId(nCtx, logging.EdgeRequestId(ctx)) nCtx = identity.WithAccountId(nCtx, identity.AccountId(ctx)) + nCtx = logging.WithReservationId(nCtx, logging.ReservationId(ctx)) return nCtx } diff --git a/internal/logging/ctx.go b/internal/logging/ctx.go index d11b6a8c..470a4ad2 100644 --- a/internal/logging/ctx.go +++ b/internal/logging/ctx.go @@ -12,6 +12,7 @@ const ( correlationCtxKey commonKeyId = iota jobIdCtxKey commonKeyId = iota reservationIdCtxKey commonKeyId = iota + jobTypeCtxKey commonKeyId = iota ) // CorrelationId returns UI correlation id or an empty string when not set. @@ -83,3 +84,17 @@ func ReservationId(ctx context.Context) int64 { func WithReservationId(ctx context.Context, id int64) context.Context { return context.WithValue(ctx, reservationIdCtxKey, id) } + +// JobType returns relevant context data or empty string when not set. +func JobType(ctx context.Context) string { + value := ctx.Value(jobTypeCtxKey) + if value == nil { + return "" + } + return value.(string) +} + +// WithJobType returns context copy with relevant value. +func WithJobType(ctx context.Context, id string) context.Context { + return context.WithValue(ctx, jobTypeCtxKey, id) +} diff --git a/internal/telemetry/zerolog_exporter.go b/internal/telemetry/zerolog_exporter.go index 46677672..64b30f4f 100644 --- a/internal/telemetry/zerolog_exporter.go +++ b/internal/telemetry/zerolog_exporter.go @@ -3,6 +3,8 @@ package telemetry import ( "context" + "github.com/RHEnVision/provisioning-backend/internal/identity" + "github.com/RHEnVision/provisioning-backend/internal/logging" "github.com/rs/zerolog" "go.opentelemetry.io/otel/attribute" "go.opentelemetry.io/otel/codes" @@ -26,11 +28,37 @@ func (e *loggerExporter) ExportSpans(ctx context.Context, spans []trace.ReadOnly parentId := span.Parent().SpanID() statusCode := span.Status().Code statusMsg := span.Status().Description + t := e.logger.Trace(). Str("trace_id", traceId). Str("span_id", spanId). Dur("duration", duration) + accountId := identity.AccountIdOrZero(ctx) + if accountId != 0 { + t = t.Int64("account_id", accountId) + } + + if requestId := logging.EdgeRequestId(ctx); requestId != "" { + t = t.Str("request_id", requestId) + } + + if orgId := identity.Identity(ctx).Identity.OrgID; orgId != "" { + t = t.Str("org_id", orgId) + } + + if accNum := identity.Identity(ctx).Identity.AccountNumber; accNum != "" { + t = t.Str("account_number", accNum) + } + + if jobId := logging.JobId(ctx); jobId != "" { + t = t.Str("job_id", jobId) + } + + if jobType := logging.JobType(ctx); jobType != "" { + t = t.Str("job_type", jobType) + } + if parentId.IsValid() { t = t.Str("span_id_parent", parentId.String()) } diff --git a/pkg/worker/job.go b/pkg/worker/job.go index 03518fa8..40f6d257 100644 --- a/pkg/worker/job.go +++ b/pkg/worker/job.go @@ -90,6 +90,8 @@ func contextLogger(origCtx context.Context, job *Job) (context.Context, *zerolog ctx = logging.WithTraceId(ctx, job.TraceID) ctx = logging.WithEdgeRequestId(ctx, job.EdgeID) ctx = identity.WithAccountId(ctx, job.AccountID) + ctx = logging.WithJobId(ctx, job.ID.String()) + ctx = logging.WithJobType(ctx, job.Type.String()) logger := zerolog.Ctx(ctx) logger = ptr.To(logger.With().