1
0
mirror of https://github.com/open-telemetry/opentelemetry-go.git synced 2026-06-03 18:35:08 +02:00

API: create new linked span from current context (#2115)

* API: create new linked span from current context

* ran make precommit

* Update trace/trace.go

* Updated CHANGELOG

Co-authored-by: Tyler Yahn <MrAlias@users.noreply.github.com>
This commit is contained in:
Bhautik Pipaliya
2021-07-22 11:39:27 -07:00
committed by GitHub
parent db81d4aa3d
commit abe2243718
3 changed files with 27 additions and 0 deletions
+1
View File
@@ -12,6 +12,7 @@ This project adheres to [Semantic Versioning](https://semver.org/spec/v2.0.0.htm
- Added `WithOSDescription` resource configuration option to set OS (Operating System) description resource attribute (`os.description`). (#1840)
- Added `WithOS` resource configuration option to set all OS (Operating System) resource attributes at once. (#1840)
- Added API `LinkFromContext` to return Link which encapsulates SpanContext from provided context and also encapsulates attributes. (#2115)
### Changed
+8
View File
@@ -408,6 +408,14 @@ type Link struct {
DroppedAttributeCount int
}
// LinkFromContext returns a link encapsulating the SpanContext in the provided ctx.
func LinkFromContext(ctx context.Context, attrs ...attribute.KeyValue) Link {
return Link{
SpanContext: SpanContextFromContext(ctx),
Attributes: attrs,
}
}
// SpanKind is the role a Span plays in a Trace.
type SpanKind int
+18
View File
@@ -16,8 +16,13 @@ package trace
import (
"bytes"
"context"
"testing"
"github.com/stretchr/testify/assert"
"go.opentelemetry.io/otel/attribute"
"github.com/google/go-cmp/cmp"
)
@@ -643,3 +648,16 @@ func TestSpanContextDerivation(t *testing.T) {
t.Fatalf("WithTraceState: Unexpected context created: %s", cmp.Diff(modified, to))
}
}
func TestLinkFromContext(t *testing.T) {
k1v1 := attribute.String("key1", "value1")
spanCtx := SpanContext{traceID: TraceID([16]byte{1}), remote: true}
receiverCtx := ContextWithRemoteSpanContext(context.Background(), spanCtx)
link := LinkFromContext(receiverCtx, k1v1)
if !assertSpanContextEqual(link.SpanContext, spanCtx) {
t.Fatalf("LinkFromContext: Unexpected context created: %s", cmp.Diff(link.SpanContext, spanCtx))
}
assert.Equal(t, link.Attributes[0], k1v1)
}