-
Notifications
You must be signed in to change notification settings - Fork 1
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Signed-off-by: peterdeme <[email protected]> Try early return when dropping Signed-off-by: peterdeme <[email protected]>
- Loading branch information
Showing
4 changed files
with
130 additions
and
5 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,75 @@ | ||
package opentelemetry | ||
|
||
import ( | ||
"fmt" | ||
|
||
"github.com/spacelift-io/spcontext" | ||
"github.com/spacelift-io/spcontext/tracing/internal" | ||
"go.opentelemetry.io/otel" | ||
"go.opentelemetry.io/otel/attribute" | ||
"go.opentelemetry.io/otel/codes" | ||
"go.opentelemetry.io/otel/trace" | ||
) | ||
|
||
// Tracer is an OpenTelemetry implementation of a Tracer. | ||
type Tracer struct { | ||
} | ||
|
||
// OnSpanStart is called when a new span is created. | ||
func (t *Tracer) OnSpanStart(ctx *spcontext.Context, name, resource string) *spcontext.Context { | ||
var opts []trace.SpanStartOption | ||
if resource != "" { | ||
opts = append(opts, trace.WithAttributes(attribute.String("resource", resource))) | ||
} | ||
|
||
exitingSpan := trace.SpanFromContext(ctx) | ||
if exitingSpan != nil && exitingSpan.SpanContext().IsValid() { | ||
// exitingSpan.TracerProvider().Tracer() | ||
// opts = append(opts, trace.WithLinks(trace.Link{SpanContext: exitingSpan.SpanContext()})) | ||
ctx = spcontext.FromStdContext(trace.ContextWithSpan(ctx, exitingSpan)) // Propagate parent span context | ||
} | ||
|
||
newCtx, _ := otel.GetTracerProvider().Tracer("spacelift.io/tracing").Start(ctx, name, opts...) | ||
|
||
return spcontext.FromStdContext(newCtx) | ||
} | ||
|
||
// OnSpanClose is called when a span is closed. | ||
func (t *Tracer) OnSpanClose(ctx *spcontext.Context, err error, fields []any, drop, analyze bool) { | ||
span := trace.SpanFromContext(ctx) | ||
if span == nil || !span.SpanContext().IsValid() { | ||
ctx.Warnf("No span in context.") | ||
return | ||
} | ||
|
||
if drop { | ||
span.SetAttributes(attribute.Bool(dropSampleKey, true)) | ||
//return | ||
} | ||
|
||
for key, value := range internal.DeduplicateFields(fields) { | ||
span.SetAttributes(attribute.String(key, fmt.Sprintf("%v", value))) | ||
} | ||
|
||
if err != nil { | ||
span.RecordError(err) | ||
span.SetStatus(codes.Error, "") | ||
} | ||
|
||
span.End(trace.WithStackTrace(err != nil)) | ||
} | ||
|
||
// GetLogFields returns the fields which should be used in a log message in this context. | ||
func (t *Tracer) GetLogFields(ctx *spcontext.Context) []any { | ||
span := trace.SpanFromContext(ctx) | ||
if span == nil { | ||
return nil | ||
} | ||
|
||
spanCtx := span.SpanContext() | ||
|
||
return []interface{}{ | ||
"otel.trace_id", spanCtx.TraceID(), | ||
"otel.span_id", spanCtx.SpanID(), | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,33 @@ | ||
package opentelemetry | ||
|
||
// import ( | ||
// "go.opentelemetry.io/otel/sdk/trace" | ||
// otelTrace "go.opentelemetry.io/otel/trace" | ||
// ) | ||
|
||
const dropSampleKey = "drop" | ||
|
||
// type Sampler struct { | ||
// root trace.Sampler | ||
// } | ||
|
||
// func NewSampler(root trace.Sampler) *Sampler { | ||
// return &Sampler{root: root} | ||
// } | ||
|
||
// func (s *Sampler) ShouldSample(p trace.SamplingParameters) trace.SamplingResult { | ||
// for _, attr := range p.Attributes { | ||
// if attr.Key == dropSampleKey { | ||
// return trace.SamplingResult{ | ||
// Decision: trace.Drop, | ||
// Tracestate: otelTrace.SpanContextFromContext(p.ParentContext).TraceState(), | ||
// } | ||
// } | ||
// } | ||
|
||
// return s.root.ShouldSample(p) | ||
// } | ||
|
||
// func (s *Sampler) Description() string { | ||
// return "Spacelift Sampler" | ||
// } |