Skip to content

Commit

Permalink
chore: add more otel params
Browse files Browse the repository at this point in the history
Signed-off-by: Lukáš Zapletal <[email protected]>
  • Loading branch information
lzap committed Oct 12, 2023
1 parent b84a0dc commit c0b85a0
Show file tree
Hide file tree
Showing 6 changed files with 53 additions and 3 deletions.
6 changes: 5 additions & 1 deletion internal/db/connection.go
Original file line number Diff line number Diff line change
Expand Up @@ -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)
Expand All @@ -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)
}
Expand Down
4 changes: 2 additions & 2 deletions internal/identity/account.go
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down
1 change: 1 addition & 0 deletions internal/jobs/ctx.go
Original file line number Diff line number Diff line change
Expand Up @@ -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
}

Expand Down
15 changes: 15 additions & 0 deletions internal/logging/ctx.go
Original file line number Diff line number Diff line change
Expand Up @@ -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.
Expand Down Expand Up @@ -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)
}
28 changes: 28 additions & 0 deletions internal/telemetry/zerolog_exporter.go
Original file line number Diff line number Diff line change
Expand Up @@ -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"
Expand All @@ -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())
}
Expand Down
2 changes: 2 additions & 0 deletions pkg/worker/job.go
Original file line number Diff line number Diff line change
Expand Up @@ -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().
Expand Down

0 comments on commit c0b85a0

Please sign in to comment.