Skip to content

Commit

Permalink
Merge pull request #7317 from fpetkovski/otel-resource-attrs
Browse files Browse the repository at this point in the history
Allow specifying OTLP resource attributes for traces
  • Loading branch information
fpetkovski authored Apr 30, 2024
2 parents 2d23490 + 5960dd6 commit 17afd29
Show file tree
Hide file tree
Showing 5 changed files with 23 additions and 20 deletions.
2 changes: 2 additions & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -14,6 +14,8 @@ We use *breaking :warning:* to mark changes that are not backward compatible (re

### Added

- [#7317](https://github.com/thanos-io/thanos/pull/7317) Tracing: allow specifying resource attributes for the OTLP configuration.

### Changed

### Removed
Expand Down
1 change: 1 addition & 0 deletions docs/tracing.md
Original file line number Diff line number Diff line change
Expand Up @@ -79,6 +79,7 @@ type: OTLP
config:
client_type: ""
service_name: ""
resource_attributes: {}
reconnection_period: 0s
compression: ""
insecure: false
Expand Down
1 change: 1 addition & 0 deletions pkg/tracing/otlp/config_yaml.go
Original file line number Diff line number Diff line change
Expand Up @@ -22,6 +22,7 @@ type retryConfig struct {
type Config struct {
ClientType string `yaml:"client_type"`
ServiceName string `yaml:"service_name"`
ResourceAttributes map[string]string `yaml:"resource_attributes"`
ReconnectionPeriod time.Duration `yaml:"reconnection_period"`
Compression string `yaml:"compression"`
Insecure bool `yaml:"insecure"`
Expand Down
31 changes: 17 additions & 14 deletions pkg/tracing/otlp/otlp.go
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,8 @@ import (
"strconv"
"strings"

"go.opentelemetry.io/otel/attribute"

"github.com/thanos-io/thanos/pkg/tracing/migration"

"github.com/go-kit/log"
Expand Down Expand Up @@ -70,26 +72,27 @@ func NewTracerProvider(ctx context.Context, logger log.Logger, conf []byte) (*tr
if err != nil {
logger.Log(err)
}
tp := newTraceProvider(ctx, processor, logger, config.ServiceName, sampler)
tp := newTraceProvider(ctx, processor, logger, config.ServiceName, config.ResourceAttributes, sampler)

return tp, nil
}

func newTraceProvider(ctx context.Context, processor tracesdk.SpanProcessor, logger log.Logger, serviceName string, sampler tracesdk.Sampler) *tracesdk.TracerProvider {
var (
r *resource.Resource
err error
)
func newTraceProvider(
ctx context.Context,
processor tracesdk.SpanProcessor,
logger log.Logger,
serviceName string,
attrs map[string]string,
sampler tracesdk.Sampler,
) *tracesdk.TracerProvider {
resourceAttrs := make([]attribute.KeyValue, 0, len(attrs)+1)
if serviceName != "" {
r, err = resource.New(
ctx,
resource.WithAttributes(semconv.ServiceNameKey.String(serviceName)),
)
} else {
r, err = resource.New(
ctx,
)
resourceAttrs = append(resourceAttrs, semconv.ServiceNameKey.String(serviceName))
}
for k, v := range attrs {
resourceAttrs = append(resourceAttrs, attribute.String(k, v))
}
r, err := resource.New(ctx, resource.WithAttributes(resourceAttrs...))
if err != nil {
level.Warn(logger).Log("msg", "jaeger: detecting resources for tracing provider failed", "err", err)
}
Expand Down
8 changes: 2 additions & 6 deletions pkg/tracing/otlp/otlp_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,7 @@ import (
"testing"

"github.com/efficientgo/core/testutil"

"github.com/thanos-io/thanos/pkg/tracing"
"github.com/thanos-io/thanos/pkg/tracing/migration"

Expand All @@ -20,12 +21,7 @@ import (
func TestContextTracing_ClientEnablesTracing(t *testing.T) {
exp := tracetest.NewInMemoryExporter()

tracerOtel := newTraceProvider(
context.Background(),
tracesdk.NewSimpleSpanProcessor(exp),
log.NewNopLogger(),
"thanos",
tracesdk.AlwaysSample())
tracerOtel := newTraceProvider(context.Background(), tracesdk.NewSimpleSpanProcessor(exp), log.NewNopLogger(), "thanos", nil, tracesdk.AlwaysSample())
tracer, _ := migration.Bridge(tracerOtel, log.NewNopLogger())
clientRoot, _ := tracing.StartSpan(tracing.ContextWithTracer(context.Background(), tracer), "a")

Expand Down

0 comments on commit 17afd29

Please sign in to comment.