Skip to content
Closed
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
27 changes: 19 additions & 8 deletions go/config.go
Original file line number Diff line number Diff line change
Expand Up @@ -3,18 +3,20 @@
import (
"context"

"github.com/launchdarkly/observability-sdk/go/internal/defaults"

Check failure on line 6 in go/config.go

View workflow job for this annotation

GitHub Actions / Quality Check

File is not properly formatted (goimports)
"go.opentelemetry.io/otel/trace"
)

type observabilityConfig struct {
serviceName string
serviceVersion string
environment string
backendURL string
otlpEndpoint string
manualStart bool
context context.Context
debug bool
serviceName string
serviceVersion string
environment string
backendURL string
otlpEndpoint string
manualStart bool
context context.Context
debug bool
samplingRateMap map[trace.SpanKind]float64
}

func defaultConfig() observabilityConfig {
Expand Down Expand Up @@ -86,3 +88,12 @@
c.debug = true
}
}

// WithSamplingRateMap sets the sampling rate for each span kind.
// This setting can influence the quality of metrics used for experiments and guarded
// releases and should only be adjusted with consultation.
func WithSamplingRateMap(rates map[trace.SpanKind]float64) Option {
return Option(func(conf *observabilityConfig) {
conf.samplingRateMap = rates
})
}
5 changes: 4 additions & 1 deletion go/internal/otel/otel.go
Original file line number Diff line number Diff line change
Expand Up @@ -210,7 +210,6 @@ func createTracerProvider(
return nil, fmt.Errorf("creating OTLP trace exporter: %w", err)
}
opts = append([]sdktrace.TracerProviderOption{
sdktrace.WithSampler(sampler),
sdktrace.WithBatcher(newTraceExporter(exporter, customSampler),
sdktrace.WithBatchTimeout(time.Second),
sdktrace.WithExportTimeout(30*time.Second),
Expand All @@ -219,6 +218,10 @@ func createTracerProvider(
),
sdktrace.WithResource(resources),
}, opts...)
// Only configure a sampler when there is a sampling configuration.
if sampler != nil {
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 different than the highlight SDK that always had a sampler with defaults.

opts = append(opts, sdktrace.WithSampler(sampler))
}
return sdktrace.NewTracerProvider(opts...), nil
}

Expand Down
21 changes: 6 additions & 15 deletions go/plugin.go
Original file line number Diff line number Diff line change
Expand Up @@ -56,20 +56,6 @@ func (p ObservabilityPlugin) Metadata() ldplugins.Metadata {
return ldplugins.NewMetadata("launchdarkly-observability")
}

type allSampler struct{}

// Description provide a description of the sampler.
func (a *allSampler) Description() string {
return "samples all traces"
}

// ShouldSample determines if the trace should be sampled.
func (a *allSampler) ShouldSample(parameters trace.SamplingParameters) trace.SamplingResult {
return trace.SamplingResult{
Decision: trace.RecordAndSample,
}
}

func (p ObservabilityPlugin) getSamplingConfig(projectId string) (*gql.GetSamplingConfigResponse, error) {
var ctx context.Context
if p.config.context != nil {
Expand Down Expand Up @@ -101,7 +87,12 @@ func (p ObservabilityPlugin) Register(client interfaces.LDClientInterface, ldmd
logging.SetLogger(logging.ConsoleLogger{})
}

var s trace.Sampler = &allSampler{}
var s trace.Sampler
if len(p.config.samplingRateMap) > 0 {
s = getSampler(p.config.samplingRateMap)
} else {
s = nil
}
otel.SetConfig(otel.Config{
OtlpEndpoint: p.config.otlpEndpoint,
ResourceAttributes: attributes,
Expand Down
64 changes: 64 additions & 0 deletions go/trace_sampler.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,64 @@
package ldobserve

import (
"encoding/binary"
"fmt"

"github.com/samber/lo"
sdktrace "go.opentelemetry.io/otel/sdk/trace"
"go.opentelemetry.io/otel/trace"
)

type traceSampler struct {
traceIDUpperBounds map[trace.SpanKind]uint64
description string
}

func (ts traceSampler) ShouldSample(p sdktrace.SamplingParameters) sdktrace.SamplingResult {
psc := trace.SpanContextFromContext(p.ParentContext)
if psc.IsSampled() {
return sdktrace.SamplingResult{
Decision: sdktrace.RecordAndSample,
Tracestate: psc.TraceState(),
}
}
bound, ok := ts.traceIDUpperBounds[p.Kind]
if !ok {
bound, ok = ts.traceIDUpperBounds[trace.SpanKindUnspecified]
// If there are no bounds specified, then we sample all
// Avoiding doing work here versus having default bounds which would
// would require additional work per span.
if !ok {
return sdktrace.SamplingResult{
Decision: sdktrace.RecordAndSample,
Tracestate: psc.TraceState(),
}
}
}

x := binary.BigEndian.Uint64(p.TraceID[8:16]) >> 1
if x < bound {
return sdktrace.SamplingResult{
Decision: sdktrace.RecordAndSample,
Tracestate: psc.TraceState(),
}
}
return sdktrace.SamplingResult{
Decision: sdktrace.Drop,
Tracestate: psc.TraceState(),
}
}

func (ts traceSampler) Description() string {
return ts.description
}

// creates a per-span-kind sampler that samples each kind at a provided fraction.
func getSampler(rates map[trace.SpanKind]float64) traceSampler {
return traceSampler{
description: fmt.Sprintf("TraceIDRatioBased{%+v}", rates),
traceIDUpperBounds: lo.MapEntries(rates, func(key trace.SpanKind, value float64) (trace.SpanKind, uint64) {
return key, uint64(value * (1 << 63))
}),
}
}
Loading
Loading