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

Debug Logging for sdk/trace #2500

Merged
merged 4 commits into from Jan 11, 2022
Merged
Show file tree
Hide file tree
Changes from 1 commit
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
3 changes: 3 additions & 0 deletions CHANGELOG.md
Expand Up @@ -8,6 +8,9 @@ This project adheres to [Semantic Versioning](https://semver.org/spec/v2.0.0.htm

## [Unreleased]

### Added
- Log the configuration of TracerProviders, and Tracers for debugging. To enable use a logger with Verbosity (V level) >=1 (#TODO)
MrAlias marked this conversation as resolved.
Show resolved Hide resolved

### Changed

- Jaeger exporter takes into additional 70 bytes overhead into consideration when sending UDP packets (#2489)
Expand Down
2 changes: 1 addition & 1 deletion example/namedtracer/main.go
Expand Up @@ -55,7 +55,7 @@ func initTracer() {

func main() {
// Set logging level to info to see SDK status messages
stdr.SetVerbosity(1)
stdr.SetVerbosity(5)

// initialize trace provider.
initTracer()
Expand Down
11 changes: 11 additions & 0 deletions exporters/stdout/stdouttrace/trace.go
Expand Up @@ -106,3 +106,14 @@ func (e *Exporter) Shutdown(ctx context.Context) error {
}
return nil
}

// MarshalLog is the marshaling function used by the logging system to represent this exporter.
func (e *Exporter) MarshalLog() interface{} {
return struct {
Type string
WithTimestamps bool
}{
Type: "stdout",
WithTimestamps: e.timestamps,
}
}
1 change: 1 addition & 0 deletions sdk/go.mod
Expand Up @@ -5,6 +5,7 @@ go 1.16
replace go.opentelemetry.io/otel => ../

require (
github.com/go-logr/logr v1.2.2
github.com/google/go-cmp v0.5.6
github.com/stretchr/testify v1.7.0
go.opentelemetry.io/otel v1.3.0
Expand Down
10 changes: 10 additions & 0 deletions sdk/resource/resource.go
Expand Up @@ -109,6 +109,16 @@ func (r *Resource) String() string {
return r.attrs.Encoded(attribute.DefaultEncoder())
}

func (r *Resource) MarshalLog() interface{} {
return struct {
Attributes []attribute.KeyValue
SchemaURL string
}{
Attributes: r.attrs.ToSlice(),
MrAlias marked this conversation as resolved.
Show resolved Hide resolved
SchemaURL: r.schemaURL,
}
}

// Attributes returns a copy of attributes from the resource in a sorted order.
// To avoid allocating a new slice, use an iterator.
func (r *Resource) Attributes() []attribute.KeyValue {
Expand Down
15 changes: 14 additions & 1 deletion sdk/trace/batch_span_processor.go
Expand Up @@ -238,7 +238,7 @@ func (bsp *batchSpanProcessor) exportSpans(ctx context.Context) error {
}

if l := len(bsp.batch); l > 0 {
global.Debug("exporting spans", "count", len(bsp.batch))
global.Debug("exporting spans", "count", len(bsp.batch), "dropped", bsp.dropped)
err := bsp.e.ExportSpans(ctx, bsp.batch)

// A new batch is always created after exporting, even if the batch failed to be exported.
Expand Down Expand Up @@ -369,3 +369,16 @@ func (bsp *batchSpanProcessor) enqueueBlockOnQueueFull(ctx context.Context, sd R
}
return false
}

// MarshalLog is the marshaling function used by the logging system to represent this exporter.
func (bsp *batchSpanProcessor) MarshalLog() interface{} {
return struct {
Type string
SpanExpoter SpanExporter
MrAlias marked this conversation as resolved.
Show resolved Hide resolved
Config BatchSpanProcessorOptions
}{
Type: "BatchSpanProcessor",
SpanExpoter: bsp.e,
MrAlias marked this conversation as resolved.
Show resolved Hide resolved
Config: bsp.o,
}
}
43 changes: 43 additions & 0 deletions sdk/trace/batch_span_processor_test.go
Expand Up @@ -23,9 +23,11 @@ import (
"testing"
"time"

"github.com/go-logr/logr/funcr"
"github.com/stretchr/testify/assert"
"github.com/stretchr/testify/require"

"go.opentelemetry.io/otel/internal/global"
sdktrace "go.opentelemetry.io/otel/sdk/trace"
"go.opentelemetry.io/otel/sdk/trace/tracetest"
"go.opentelemetry.io/otel/trace"
Expand Down Expand Up @@ -480,3 +482,44 @@ func TestBatchSpanProcessorForceFlushQueuedSpans(t *testing.T) {
assert.Len(t, exp.GetSpans(), i+1)
}
}

func BenchmarkSpanProcessor(b *testing.B) {
tp := sdktrace.NewTracerProvider(
sdktrace.WithBatcher(
tracetest.NewNoopExporter(),
sdktrace.WithMaxExportBatchSize(10),
))
tracer := tp.Tracer("bench")
ctx := context.Background()

b.ResetTimer()
b.ReportAllocs()

for i := 0; i < b.N; i++ {
for j := 0; j < 10; j++ {
_, span := tracer.Start(ctx, "bench")
span.End()
}
}
}

func BenchmarkSpanProcessorVerboseLogging(b *testing.B) {
global.SetLogger(funcr.New(func(prefix, args string) {}, funcr.Options{Verbosity: 5}))
tp := sdktrace.NewTracerProvider(
sdktrace.WithBatcher(
tracetest.NewNoopExporter(),
sdktrace.WithMaxExportBatchSize(10),
))
tracer := tp.Tracer("bench")
ctx := context.Background()

b.ResetTimer()
b.ReportAllocs()

for i := 0; i < b.N; i++ {
for j := 0; j < 10; j++ {
_, span := tracer.Start(ctx, "bench")
span.End()
}
}
}
21 changes: 21 additions & 0 deletions sdk/trace/provider.go
Expand Up @@ -21,6 +21,7 @@ import (
"sync/atomic"

"go.opentelemetry.io/otel"
"go.opentelemetry.io/otel/internal/global"
"go.opentelemetry.io/otel/sdk/instrumentation"
"go.opentelemetry.io/otel/sdk/resource"
"go.opentelemetry.io/otel/trace"
Expand Down Expand Up @@ -52,6 +53,23 @@ type tracerProviderConfig struct {
resource *resource.Resource
}

// MarshalLog is the marshaling function used by the logging system to represent this exporter.
func (cfg tracerProviderConfig) MarshalLog() interface{} {
return struct {
SpanProcessors []SpanProcessor
SamplerType string
IDGeneratorType string
SpanLimits SpanLimits
Resource *resource.Resource
}{
SpanProcessors: cfg.processors,
SamplerType: fmt.Sprintf("%T", cfg.sampler),
IDGeneratorType: fmt.Sprintf("%T", cfg.idGenerator),
SpanLimits: cfg.spanLimits,
Resource: cfg.resource, //TODO: Resource marshals to a string currently
MrAlias marked this conversation as resolved.
Show resolved Hide resolved
}
}

type TracerProvider struct {
mu sync.Mutex
namedTracer map[instrumentation.Library]*tracer
Expand Down Expand Up @@ -91,6 +109,8 @@ func NewTracerProvider(opts ...TracerProviderOption) *TracerProvider {
resource: o.resource,
}

global.Info("TracerProvider created", "config", o)

for _, sp := range o.processors {
tp.RegisterSpanProcessor(sp)
}
Expand Down Expand Up @@ -125,6 +145,7 @@ func (p *TracerProvider) Tracer(name string, opts ...trace.TracerOption) trace.T
instrumentationLibrary: il,
}
p.namedTracer[il] = t
global.Info("Tracer created", "name", name, "version", c.InstrumentationVersion(), "schemaURL", c.SchemaURL())
}
return t
}
Expand Down
11 changes: 11 additions & 0 deletions sdk/trace/simple_span_processor.go
Expand Up @@ -115,3 +115,14 @@ func (ssp *simpleSpanProcessor) Shutdown(ctx context.Context) error {
func (ssp *simpleSpanProcessor) ForceFlush(context.Context) error {
return nil
}

// MarshalLog is the marshaling function used by the logging system to represent this exporter.
func (ssp *simpleSpanProcessor) MarshalLog() interface{} {
return struct {
Type string
Exporter SpanExporter
}{
Type: "SimpleSpanProcessor",
Exporter: ssp.exporter,
}
}