Skip to content

Commit

Permalink
Merge pull request #9 from george-maroun/issue4/context
Browse files Browse the repository at this point in the history
Issue4/context
  • Loading branch information
george-maroun authored Aug 11, 2023
2 parents 56285fc + f6e8be6 commit fbc9633
Show file tree
Hide file tree
Showing 6 changed files with 186 additions and 142 deletions.
Binary file modified .DS_Store
Binary file not shown.
1 change: 1 addition & 0 deletions .gitignore
Original file line number Diff line number Diff line change
@@ -1,2 +1,3 @@
.vscode/
dist/
bin/
265 changes: 135 additions & 130 deletions cover.out

Large diffs are not rendered by default.

30 changes: 20 additions & 10 deletions internal/checkers/checker.go
Original file line number Diff line number Diff line change
Expand Up @@ -66,13 +66,6 @@ func ExecuteChecker(c Checker, pass *analysis.Pass, call CallContext, cfg Config

hasTraceId := false
for i := 0; i < len(keyValuesArgs); i += 2 {
arg := keyValuesArgs[i]
basic, ok := arg.(*ast.BasicLit)
if !ok {
// Just ignore it if its not of type BasicLiteral
continue
}

// We use traceId not traceID based on spanId in Google stackdriver stuctured logging
// https://cloud.google.com/logging/docs/structured-logging
// In the opentelemetry docs it is "TraceId"
Expand All @@ -82,8 +75,25 @@ func ExecuteChecker(c Checker, pass *analysis.Pass, call CallContext, cfg Config
// This is also how its defined in the OpenTelemetry spec for jsonLogs
// https://opentelemetry.io/docs/specs/otel/protocol/file-exporter/#examples
// https://opentelemetry.io/docs/specs/otel/logs/
if basic.Value == "\"traceId\"" {
hasTraceId = true
arg := keyValuesArgs[i]

switch v := arg.(type) {
case *ast.BasicLit:
if strings.Contains(strings.ToLower(v.Value), "trace") {
hasTraceId = true
}
case *ast.Ident:
// v.Name contains the name (label) of the variable
if strings.Contains(strings.ToLower(v.Name), "trace") {
hasTraceId = true
}
case *ast.SelectorExpr:
// Check if the selector (right part) of the qualified identifier contains "trace"
if strings.Contains(strings.ToLower(v.Sel.Name), "trace") {
hasTraceId = true
}
}
if hasTraceId == true {
break
}
}
Expand Down Expand Up @@ -351,4 +361,4 @@ func isWithValuesCallOnNewLogger(file *ast.File, pos token.Pos) bool {
return true
})
return result
}
}
16 changes: 15 additions & 1 deletion testdata/src/a/fix_import/example.go
Original file line number Diff line number Diff line change
Expand Up @@ -6,14 +6,28 @@ import (
"context"
)

type telemetry struct {
TraceLogKey string
}

func SomeFunc(ctx context.Context, eventType, deliveryID string, payload []byte) error {
log := zapr.NewLogger(zap.L()).WithValues("eventType", eventType, "deliverID", deliveryID) // want `missing traceId in logging keys`
log = log.WithValues("eventType", "hello")
log.Info("Tracing")
return nil
}

func SomeFunc1(eventType, deliveryID string, payload []byte) error {
func SomeFunc1(ctx context.Context, eventType, deliveryID string, payload []byte) error {
telemetryInstance := telemetry{
TraceLogKey: "dummyTrace",
}
log := zapr.NewLogger(zap.L()).WithValues(telemetryInstance.TraceLogKey, "someValue") // cannot be detected
log = log.WithValues("eventType", "hello")
log.Info("Tracing")
return nil
}

func SomeFunc2(eventType, deliveryID string, payload []byte) error {
log := zapr.NewLogger(zap.L()).WithValues("eventType", eventType, "deliverID", deliveryID) // cannot be detected
log = log.WithValues("eventType", "hello")
log.Info("Tracing")
Expand Down
16 changes: 15 additions & 1 deletion testdata/src/a/fix_import/example.go.golden
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,10 @@ import (
"go.uber.org/zap"
)

type telemetry struct {
TraceLogKey string
}

func SomeFunc(ctx context.Context, eventType, deliveryID string, payload []byte) error {
span := trace.SpanFromContext(ctx)
log := zapr.NewLogger(zap.L()).WithValues("traceId", span.SpanContext().TraceID().String(), "spanId", span.SpanContext().SpanID().String(), "eventType", eventType, "deliverID", deliveryID) // want `missing traceId in logging keys`
Expand All @@ -15,7 +19,17 @@ func SomeFunc(ctx context.Context, eventType, deliveryID string, payload []byte)
return nil
}

func SomeFunc1(eventType, deliveryID string, payload []byte) error {
func SomeFunc1(ctx context.Context, eventType, deliveryID string, payload []byte) error {
telemetryInstance := telemetry{
TraceLogKey: "dummyTrace",
}
log := zapr.NewLogger(zap.L()).WithValues(telemetryInstance.TraceLogKey, "someValue") // cannot be detected
log = log.WithValues("eventType", "hello")
log.Info("Tracing")
return nil
}

func SomeFunc2(eventType, deliveryID string, payload []byte) error {
log := zapr.NewLogger(zap.L()).WithValues("eventType", eventType, "deliverID", deliveryID) // cannot be detected
log = log.WithValues("eventType", "hello")
log.Info("Tracing")
Expand Down

0 comments on commit fbc9633

Please sign in to comment.