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
6 changes: 6 additions & 0 deletions pkg/tracing/plugin/otlp.go
Original file line number Diff line number Diff line change
Expand Up @@ -38,6 +38,7 @@ import (
"go.opentelemetry.io/otel/exporters/otlp/otlptrace/otlptracehttp"
"go.opentelemetry.io/otel/propagation"
"go.opentelemetry.io/otel/sdk/trace"
semconv "go.opentelemetry.io/otel/semconv/v1.21.0"
)

const exporterPlugin = "otlp"
Expand Down Expand Up @@ -193,6 +194,11 @@ func newTracer(ctx context.Context, procs []trace.SpanProcessor) (io.Closer, err
for _, proc := range procs {
opts = append(opts, trace.WithSpanProcessor(proc))
}

attributes := map[string][]string{
string(semconv.RPCMethodKey): {"PullImage", "Create"},
}
opts = append(opts, trace.WithSampler(trace.ParentBased(AttributeBased(attributes))))
provider := trace.NewTracerProvider(opts...)
otel.SetTracerProvider(provider)

Expand Down
46 changes: 46 additions & 0 deletions pkg/tracing/plugin/sampler.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,46 @@
package plugin

import (
"fmt"
"slices"

sdkTrace "go.opentelemetry.io/otel/sdk/trace"
"go.opentelemetry.io/otel/trace"
)

type attributeSampler struct {
selectedAttrs map[string][]string
}

// AttributeBased returns a Sampler that samples every trace matching a set of attributes.
// It only need to find one matching key-value pairs present in the attributes of the span.
func AttributeBased(selectedAttrs map[string][]string) attributeSampler {
return attributeSampler{
selectedAttrs: selectedAttrs,
}
}

func (ss attributeSampler) ShouldSample(parameters sdkTrace.SamplingParameters) sdkTrace.SamplingResult {
psc := trace.SpanContextFromContext(parameters.ParentContext)
println(parameters.Name)
for _, attr := range parameters.Attributes {
println(attr.Key, attr.Value.Emit())
if selectedValues, ok := ss.selectedAttrs[string(attr.Key)]; ok {
if slices.Contains(selectedValues, attr.Value.Emit()) {
println("Sampled")
return sdkTrace.SamplingResult{
Decision: sdkTrace.RecordAndSample,
Tracestate: psc.TraceState(),
}
}
}
}
return sdkTrace.SamplingResult{
Decision: sdkTrace.Drop,
Tracestate: psc.TraceState(),
}
}

func (ss attributeSampler) Description() string {
return fmt.Sprintf("AttributeBased:{%v}", ss.selectedAttrs)
}