Skip to content

Commit

Permalink
draft: added some more config options for jaeger
Browse files Browse the repository at this point in the history
Signed-off-by: Aditi Ahuja <ahuja.aditi@gmail.com>
  • Loading branch information
metonymic-smokey committed Jun 19, 2022
1 parent d1944fd commit 14bbd28
Show file tree
Hide file tree
Showing 2 changed files with 45 additions and 11 deletions.
47 changes: 36 additions & 11 deletions pkg/tracing/jaeger/jaeger.go
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,7 @@ import (
"context"
"fmt"
"io"
"math"
"strconv"
"strings"

Expand All @@ -20,6 +21,7 @@ import (
"github.com/uber/jaeger-client-go"
"github.com/uber/jaeger-client-go/config"
jaeger_prometheus "github.com/uber/jaeger-lib/metrics/prometheus"
"go.opentelemetry.io/otel/attribute"
otel_jaeger "go.opentelemetry.io/otel/exporters/jaeger"
"go.opentelemetry.io/otel/sdk/resource"
tracesdk "go.opentelemetry.io/otel/sdk/trace"
Expand Down Expand Up @@ -70,12 +72,39 @@ func NewTracerProvider(ctx context.Context, logger log.Logger, conf []byte) (*tr
}
}

tags := getAttributesFromTags(config)
samplingFraction := getSamplingFraction(config.SamplerType, config.SamplerParam)
processor := tracesdk.NewBatchSpanProcessor(exporter)
tp := newTraceProvider(ctx, logger, processor, config.SamplerParam, config.ServiceName)
tp := newTraceProvider(ctx, logger, processor, samplingFraction, tags, config.ServiceName)

return tp, nil
}

// getSamplingFraction returns the sampling fraction based on the sampler type.
// Ref: https://www.jaegertracing.io/docs/1.35/sampling/#client-sampling-configuration
func getSamplingFraction(samplerType string, samplingFactor float64) float64 {
if samplerType == "const" {
if samplingFactor > 1 {
return 1.0
} else if samplingFactor < 0 {
return 0.0
}
return math.Round(samplingFactor) // Returns either 0 or 1 for values [0,1].
} else if samplerType == "probabilistic" {
return samplingFactor
} else if samplerType == "ratelimiting" {
return math.Round(samplingFactor) // Needs to be an integer.
}
return samplingFactor
}

// getAttributesFromTags returns tags as OTel attributes
func getAttributesFromTags(config Config) []attribute.KeyValue {
opentracingTags := parseTags(config.Tags)
return migration.ConvertOTTagsToOTelAttrs(opentracingTags)
}

// getCollectorEndpoints returns Jaeger options populated with collector related options.
func getCollectorEndpoints(config Config) []otel_jaeger.CollectorEndpointOption {
var jaegerCollectorEndpointOptions []otel_jaeger.CollectorEndpointOption
if config.User != "" {
Expand All @@ -89,6 +118,7 @@ func getCollectorEndpoints(config Config) []otel_jaeger.CollectorEndpointOption
return jaegerCollectorEndpointOptions
}

// getAgentEndpointOptions returns Jaeger options populated with agent related options.
func getAgentEndpointOptions(config Config) []otel_jaeger.AgentEndpointOption {
var jaegerAgentEndpointOptions []otel_jaeger.AgentEndpointOption
jaegerAgentEndpointOptions = append(jaegerAgentEndpointOptions, otel_jaeger.WithAgentHost(config.AgentHost))
Expand All @@ -98,16 +128,11 @@ func getAgentEndpointOptions(config Config) []otel_jaeger.AgentEndpointOption {
}

func newTraceProvider(ctx context.Context, logger log.Logger, processor tracesdk.SpanProcessor,
samplingFactor float64, serviceName string) *tracesdk.TracerProvider {

var fraction float64
if samplingFactor == 0 {
fraction = 0
} else {
fraction = 1 / float64(samplingFactor)
}
samplingFraction float64, tags []attribute.KeyValue, serviceName string) *tracesdk.TracerProvider {

resource, err := resource.New(ctx, resource.WithAttributes(tracing.CollectAttributes(serviceName)...))
attributes := tracing.CollectAttributes(serviceName)
attributes = append(attributes, tags...)
resource, err := resource.New(ctx, resource.WithAttributes(attributes...))
if err != nil {
level.Warn(logger).Log("msg", "jaeger: detecting resources for tracing provider failed", "err", err)
}
Expand All @@ -116,7 +141,7 @@ func newTraceProvider(ctx context.Context, logger log.Logger, processor tracesdk
tracesdk.WithSpanProcessor(processor),
tracesdk.WithSampler(
migration.SamplerWithOverride(
tracesdk.ParentBased(tracesdk.TraceIDRatioBased(fraction)),
tracesdk.ParentBased(tracesdk.TraceIDRatioBased(samplingFraction)),
migration.ForceTracingAttributeKey,
),
),
Expand Down
9 changes: 9 additions & 0 deletions pkg/tracing/migration/bridge.go
Original file line number Diff line number Diff line change
Expand Up @@ -13,6 +13,7 @@ import (
"github.com/opentracing/opentracing-go"
ot_propagator "go.opentelemetry.io/contrib/propagators/ot"
"go.opentelemetry.io/otel"
"go.opentelemetry.io/otel/attribute"
bridge "go.opentelemetry.io/otel/bridge/opentracing"
"go.opentelemetry.io/otel/propagation"
tracesdk "go.opentelemetry.io/otel/sdk/trace"
Expand Down Expand Up @@ -73,6 +74,14 @@ func (s shutdownAsCloser) Close() error {
return s()
}

func ConvertOTTagsToOTelAttrs(tags []opentracing.Tag) []attribute.KeyValue {
var attrs []attribute.KeyValue
for _, tag := range tags {
attrs = append(attrs, attribute.String(tag.Key, tag.Value.(string)))
}
return attrs
}

// This wrapper is necessary to enable proper trace propagation for gRPC
// calls between components. The bridge.BridgeTracer currently supports injection /
// extraction of only single carrier type which is opentracing.HTTPHeadersCarrier.
Expand Down

0 comments on commit 14bbd28

Please sign in to comment.