diff --git a/api/trace/api.go b/api/trace/api.go index 8807516c854..b364af44a24 100644 --- a/api/trace/api.go +++ b/api/trace/api.go @@ -103,6 +103,7 @@ type SpanOption func(*SpanOptions) type SpanOptions struct { Attributes []core.KeyValue StartTime time.Time + Links []Link Relation Relation Record bool SpanKind SpanKind @@ -197,6 +198,13 @@ func FollowsFrom(sc core.SpanContext) SpanOption { } } +// LinkedTo allows instantiating a Span with initial Links. +func LinkedTo(sc core.SpanContext, attrs ...core.KeyValue) SpanOption { + return func(o *SpanOptions) { + o.Links = append(o.Links, Link{sc, attrs}) + } +} + // WithSpanKind specifies the role a Span on a Trace. func WithSpanKind(sk SpanKind) SpanOption { return func(o *SpanOptions) { diff --git a/plugin/othttp/handler.go b/plugin/othttp/handler.go index 4e0bbdfeacd..361bf83503d 100644 --- a/plugin/othttp/handler.go +++ b/plugin/othttp/handler.go @@ -150,11 +150,9 @@ func (h *Handler) ServeHTTP(w http.ResponseWriter, r *http.Request) { if sc.IsValid() { // not a valid span context, so no link / parent relationship to establish var opt trace.SpanOption if h.public { - // TODO: If the endpoint is a public endpoint, it should start a new trace - // and incoming remote sctx should be added as a link - // (WithLinks(links...), this option doesn't exist yet). Replace ChildOf - // below with something like: opt = trace.WithLinks(sc) - opt = trace.ChildOf(sc) + // If the endpoint is a public endpoint, it should start a new trace + // and incoming remote sctx should be added as a link. + opt = trace.LinkedTo(sc) } else { // not a private endpoint, so assume child relationship opt = trace.ChildOf(sc) } diff --git a/sdk/trace/tracer.go b/sdk/trace/tracer.go index bee8da87bb3..03ceaf5d1e0 100644 --- a/sdk/trace/tracer.go +++ b/sdk/trace/tracer.go @@ -60,6 +60,10 @@ func (tr *tracer) Start(ctx context.Context, name string, o ...apitrace.SpanOpti spanName := tr.spanNameWithPrefix(name) span := startSpanInternal(tr, spanName, parent, remoteParent, opts) + for _, l := range opts.Links { + span.AddLink(l) + } + span.tracer = tr if span.IsRecording() {