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

Allow specifying OTLP resource attributes for traces #7317

Merged
merged 2 commits into from
Apr 30, 2024
Merged
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
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 @@
"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 @@
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)
Dismissed Show dismissed Hide dismissed
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
Loading