Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

chore: add more otel params #716

Merged
merged 1 commit into from
Oct 12, 2023
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
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 {
Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

This had wrong name, it does not return nil but 0 in fact.

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)
}

Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

This is the gist of the patch - I need to have in our zerolog exporter (which is used on stage/prod) request id which I can use to correlate all the records. It was not present at all, only trace_id which is unfortunately different because we cannot use it between api and worker pods (it gets closed).

Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

The job_id and job_type should be optional (set only when available) IMO, not to set them from all processes, having them blank may confuse us.

Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Yeah, rebased. All fields are now compared and only added when needed.

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())
Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

In addition, I add job id and type which is sometimes useful for correlating records. For this I need to put these into context when job starts.


logger := zerolog.Ctx(ctx)
logger = ptr.To(logger.With().
Expand Down