-
Notifications
You must be signed in to change notification settings - Fork 435
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
contrib/go.uber.org/zap: add basic integration
- Loading branch information
1 parent
1f0966d
commit 5e2a071
Showing
6 changed files
with
122 additions
and
0 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,29 @@ | ||
// Unless explicitly stated otherwise all files in this repository are licensed | ||
// under the Apache License Version 2.0. | ||
// This product includes software developed at Datadog (https://www.datadoghq.com/). | ||
// Copyright 2016 Datadog, Inc. | ||
|
||
package zap_test | ||
|
||
import ( | ||
"context" | ||
"go.uber.org/zap" | ||
zaptrace "gopkg.in/DataDog/dd-trace-go.v1/contrib/go.uber.org/zap" | ||
"gopkg.in/DataDog/dd-trace-go.v1/ddtrace/tracer" | ||
) | ||
|
||
func ExampleWithTraceFields() { | ||
// start the DataDog tracer | ||
tracer.Start() | ||
defer tracer.Stop() | ||
|
||
// create the application logger | ||
logger, _ := zap.NewProduction() | ||
|
||
// start a new span | ||
span, ctx := tracer.StartSpanFromContext(context.Background(), "ExampleWithTraceCorrelation") | ||
defer span.Finish() | ||
|
||
// log a message using the context containing span information | ||
zaptrace.WithTraceFields(ctx, logger).Info("this is a log with tracing information") | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,40 @@ | ||
// Unless explicitly stated otherwise all files in this repository are licensed | ||
// under the Apache License Version 2.0. | ||
// This product includes software developed at Datadog (https://www.datadoghq.com/). | ||
// Copyright 2016 Datadog, Inc. | ||
|
||
// Package zap provides functions to correlate logs and traces using go.uber.org/zap package (https://github.com/uber-go/zap). | ||
package zap // import "gopkg.in/DataDog/dd-trace-go.v1/contrib/go.uber.org/zap" | ||
|
||
import ( | ||
"context" | ||
"go.uber.org/zap" | ||
"strconv" | ||
|
||
"gopkg.in/DataDog/dd-trace-go.v1/ddtrace/ext" | ||
"gopkg.in/DataDog/dd-trace-go.v1/ddtrace/tracer" | ||
"gopkg.in/DataDog/dd-trace-go.v1/internal/telemetry" | ||
) | ||
|
||
const componentName = "go.uber.org/zap" | ||
|
||
func init() { | ||
telemetry.LoadIntegration(componentName) | ||
tracer.MarkIntegrationImported(componentName) | ||
} | ||
|
||
// WithTraceFields looks for an active span in the provided context and if found, it returns a new *zap.Logger with | ||
// traceID and spanID keys set. | ||
func WithTraceFields(ctx context.Context, logger *zap.Logger) *zap.Logger { | ||
span, ok := tracer.SpanFromContext(ctx) | ||
if ok { | ||
traceID := strconv.FormatUint(span.Context().TraceID(), 10) | ||
spanID := strconv.FormatUint(span.Context().SpanID(), 10) | ||
|
||
return logger.With( | ||
zap.String(ext.LogKeyTraceID, traceID), | ||
zap.String(ext.LogKeySpanID, spanID), | ||
) | ||
} | ||
return logger | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,48 @@ | ||
// Unless explicitly stated otherwise all files in this repository are licensed | ||
// under the Apache License Version 2.0. | ||
// This product includes software developed at Datadog (https://www.datadoghq.com/). | ||
// Copyright 2016 Datadog, Inc. | ||
|
||
package zap | ||
|
||
import ( | ||
"context" | ||
"strconv" | ||
"testing" | ||
|
||
"github.com/stretchr/testify/assert" | ||
"github.com/stretchr/testify/require" | ||
"go.uber.org/zap" | ||
"go.uber.org/zap/zapcore" | ||
"go.uber.org/zap/zaptest/observer" | ||
|
||
"gopkg.in/DataDog/dd-trace-go.v1/ddtrace/tracer" | ||
internallog "gopkg.in/DataDog/dd-trace-go.v1/internal/log" | ||
) | ||
|
||
func TestWithTraceFields(t *testing.T) { | ||
tracer.Start(tracer.WithLogger(internallog.DiscardLogger{})) | ||
defer tracer.Stop() | ||
|
||
// start a new span | ||
span, ctx := tracer.StartSpanFromContext(context.Background(), "test") | ||
defer span.Finish() | ||
|
||
observed, logs := observer.New(zapcore.InfoLevel) | ||
|
||
logger := zap.New(observed) | ||
logger = WithTraceFields(ctx, logger) | ||
logger.Info("some message") | ||
|
||
traceID := strconv.FormatUint(span.Context().TraceID(), 10) | ||
spanID := strconv.FormatUint(span.Context().SpanID(), 10) | ||
|
||
require.Equal(t, 1, logs.Len()) | ||
infoLog := logs.All()[0] | ||
|
||
require.Len(t, infoLog.Context, 2) | ||
assert.Equal(t, "dd.trace_id", infoLog.Context[0].Key) | ||
assert.Equal(t, traceID, infoLog.Context[0].String) | ||
assert.Equal(t, "dd.span_id", infoLog.Context[1].Key) | ||
assert.Equal(t, spanID, infoLog.Context[1].String) | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters