Skip to content

Commit

Permalink
fix(opentelemetry): Replace baggage with attribute
Browse files Browse the repository at this point in the history
  • Loading branch information
Marouane Kherrabi committed Jun 14, 2022
1 parent ff14584 commit 3e645a2
Show file tree
Hide file tree
Showing 3 changed files with 39 additions and 18 deletions.
14 changes: 6 additions & 8 deletions core/zap/logger.go
Original file line number Diff line number Diff line change
Expand Up @@ -4,8 +4,9 @@ import (
"context"
"fmt"

"go.opentelemetry.io/otel/attribute"

"flamingo.me/flamingo/v3/framework/opentelemetry"
"go.opentelemetry.io/otel/baggage"
"go.opentelemetry.io/otel/metric/instrument"
"go.opentelemetry.io/otel/metric/instrument/syncint64"
"go.opentelemetry.io/otel/metric/unit"
Expand All @@ -18,8 +19,8 @@ import (
)

var (
logCount syncint64.Counter
keyLevel, _ = baggage.NewKeyProperty("level")
logCount syncint64.Counter
keyLevel attribute.Key = "level"
)

func init() {
Expand Down Expand Up @@ -74,11 +75,8 @@ func (l *Logger) WithContext(ctx context.Context) flamingo.Logger {
}

func (l *Logger) record(level string) {
areaBaggage, _ := baggage.NewMember(opentelemetry.KeyArea.Key(), l.configArea)
levelBaggage, _ := baggage.NewMember(keyLevel.Key(), level)
bagg, _ := baggage.New(areaBaggage, levelBaggage)
ctx := baggage.ContextWithBaggage(context.Background(), bagg)
logCount.Add(ctx, 1)
logCount.Add(context.Background(), 1, attribute.String(opentelemetry.KeyArea.Key(), l.configArea),
keyLevel.String(level))
}

// Debug logs a message at debug level
Expand Down
29 changes: 27 additions & 2 deletions framework/opentelemetry/module.go
Original file line number Diff line number Diff line change
Expand Up @@ -98,7 +98,10 @@ func (m *Module) Configure(injector *dingo.Injector) {
schemaURL,
attribute.String("service.name", m.serviceName),
)),
tracesdk.WithSampler(tracesdk.NeverSample()),
tracesdk.WithSampler(tracesdk.ParentBased(tracesdk.NeverSample(),
tracesdk.WithLocalParentSampled(tracesdk.AlwaysSample()), tracesdk.WithLocalParentNotSampled(tracesdk.NeverSample()),
tracesdk.WithRemoteParentSampled(tracesdk.AlwaysSample()), tracesdk.WithRemoteParentNotSampled(tracesdk.NeverSample()),
)),
)
tp := tracesdk.NewTracerProvider(
tracerProviderOptions...,
Expand Down Expand Up @@ -141,7 +144,7 @@ type correlationIDInjector struct {

func (rt *correlationIDInjector) RoundTrip(req *http.Request) (*http.Response, error) {
span := trace.SpanFromContext(req.Context())
if span.IsRecording() {
if span.SpanContext().IsSampled() {
req.Header.Add("X-Correlation-ID", span.SpanContext().TraceID().String())
}
return rt.next.RoundTrip(req)
Expand Down Expand Up @@ -174,3 +177,25 @@ func GetMeter() metric.Meter {
})
return meter
}

func (m *Module) CueConfig() string {
return `
flamingo: opentelemetry: {
jaeger: {
enable: bool | *false
endpoint: string | *"http://localhost:14268/api/traces"
}
zipkin: {
enable: bool | *false
endpoint: string | *"http://localhost:9411/api/v2/spans"
}
serviceName: string | *"flamingo"
tracing: sampler: {
whitelist: [...string]
blacklist: [...string]
allowParentTrace: bool | *true
}
publicEndpoint: bool | *true
}
`
}
14 changes: 6 additions & 8 deletions framework/web/filter/request_metrics_filter.go
Original file line number Diff line number Diff line change
Expand Up @@ -5,8 +5,9 @@ import (
"net/http"
"strconv"

"go.opentelemetry.io/otel/attribute"

"flamingo.me/flamingo/v3/framework/opentelemetry"
"go.opentelemetry.io/otel/baggage"
"go.opentelemetry.io/otel/metric/instrument"
"go.opentelemetry.io/otel/metric/instrument/syncint64"
"go.opentelemetry.io/otel/metric/unit"
Expand Down Expand Up @@ -36,7 +37,7 @@ var (
// responseCount count the number of responses served by the application
responsesCount syncint64.Counter
// keyHTTPStatus defines response http status code
keyHTTPStatus, _ = baggage.NewKeyProperty("status_code")
keyHTTPStatus attribute.Key = "status_code"
)

func init() {
Expand Down Expand Up @@ -82,12 +83,9 @@ func (r responseMetrics) Apply(ctx context.Context, rw http.ResponseWriter) erro
err = r.result.Apply(ctx, responseWriter)
}

statusBaggage, _ := baggage.NewMember(keyHTTPStatus.Key(), strconv.Itoa(responseWriter.status/100)+"xx")
bagg := baggage.FromContext(ctx)
bagg, _ = bagg.SetMember(statusBaggage)
c := baggage.ContextWithBaggage(ctx, bagg)
responseBytesCount.Add(c, responseWriter.bytes)
responsesCount.Add(c, 1)
statusAttribute := keyHTTPStatus.String(strconv.Itoa(responseWriter.status/100) + "xx")
responseBytesCount.Add(ctx, responseWriter.bytes, statusAttribute)
responsesCount.Add(ctx, 1, statusAttribute)

return err
}
Expand Down

0 comments on commit 3e645a2

Please sign in to comment.