1
0
mirror of https://github.com/open-telemetry/opentelemetry-go.git synced 2024-12-14 10:13:10 +02:00
opentelemetry-go/internal/trace/parent/parent.go

41 lines
946 B
Go
Raw Normal View History

package parent
import (
"context"
"go.opentelemetry.io/otel/api/core"
"go.opentelemetry.io/otel/api/key"
"go.opentelemetry.io/otel/api/trace"
)
func GetSpanContextAndLinks(ctx context.Context, ignoreContext bool) (core.SpanContext, bool, []trace.Link) {
lsctx := trace.SpanFromContext(ctx).SpanContext()
rsctx := trace.RemoteSpanContextFromContext(ctx)
if ignoreContext {
links := addLinkIfValid(nil, lsctx, "current")
links = addLinkIfValid(links, rsctx, "remote")
return core.EmptySpanContext(), false, links
}
if lsctx.IsValid() {
return lsctx, false, nil
}
if rsctx.IsValid() {
return rsctx, true, nil
}
return core.EmptySpanContext(), false, nil
}
func addLinkIfValid(links []trace.Link, sc core.SpanContext, kind string) []trace.Link {
if !sc.IsValid() {
return links
}
return append(links, trace.Link{
SpanContext: sc,
Attributes: []core.KeyValue{
key.String("ignored-on-demand", kind),
},
})
}