Skip to content

Commit

Permalink
set attributes in tracer.Start() (#286)
Browse files Browse the repository at this point in the history
* set attributes in tracer.Start()

* add tests

* remove no longer needed attribute additions
  • Loading branch information
lizthegrey authored and rghetia committed Nov 4, 2019
1 parent e99cac2 commit d25fae7
Show file tree
Hide file tree
Showing 3 changed files with 33 additions and 9 deletions.
4 changes: 0 additions & 4 deletions plugin/httptrace/clienttrace.go
Original file line number Diff line number Diff line change
Expand Up @@ -79,10 +79,6 @@ func NewClientTrace(ctx context.Context) *httptrace.ClientTrace {

func (ct *clientTracer) start(hook, spanName string, attrs ...core.KeyValue) {
_, sp := ct.tr.Start(ct.Context, spanName, trace.WithAttributes(attrs...), trace.WithSpanKind(trace.SpanKindClient))
// TODO(paivagustavo): remove this for loop when `trace.WithAttributes(attrs...)` works.
for _, attr := range attrs {
sp.SetAttribute(attr)
}
ct.mtx.Lock()
defer ct.mtx.Unlock()
if ct.root == nil {
Expand Down
37 changes: 32 additions & 5 deletions sdk/trace/trace_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -287,6 +287,33 @@ func TestStartSpanWithFollowsFrom(t *testing.T) {

// TODO: [rghetia] Equivalent of SpanKind Test.

func TestSetSpanAttributesOnStart(t *testing.T) {
te := &testExporter{}
tp, _ := NewProvider(WithSyncer(te))
span := startSpan(tp, "StartSpanAttribute", apitrace.WithAttributes(key.String("key1", "value1")))
got, err := endSpan(te, span)
if err != nil {
t.Fatal(err)
}

want := &export.SpanData{
SpanContext: core.SpanContext{
TraceID: tid,
TraceFlags: 0x1,
},
ParentSpanID: sid,
Name: "StartSpanAttribute/span0",
Attributes: []core.KeyValue{
key.String("key1", "value1"),
},
SpanKind: "internal",
HasRemoteParent: true,
}
if diff := cmpDiff(got, want); diff != "" {
t.Errorf("SetSpanAttributesOnStart: -got +want %s", diff)
}
}

func TestSetSpanAttributes(t *testing.T) {
te := &testExporter{}
tp, _ := NewProvider(WithSyncer(te))
Expand Down Expand Up @@ -655,20 +682,20 @@ func checkChild(p core.SpanContext, apiSpan apitrace.Span) error {

// startSpan starts a span with a name "span0". See startNamedSpan for
// details.
func startSpan(tp *Provider, trName string) apitrace.Span {
return startNamedSpan(tp, trName, "span0")
func startSpan(tp *Provider, trName string, args ...apitrace.SpanOption) apitrace.Span {
return startNamedSpan(tp, trName, "span0", args...)
}

// startNamed Span is a test utility func that starts a span with a
// passed name and with ChildOf option. remote span context contains
// TraceFlags with sampled bit set. This allows the span to be
// automatically sampled.
func startNamedSpan(tp *Provider, trName, name string) apitrace.Span {
func startNamedSpan(tp *Provider, trName, name string, args ...apitrace.SpanOption) apitrace.Span {
args = append(args, apitrace.ChildOf(remoteSpanContext()), apitrace.WithRecord())
_, span := tp.GetTracer(trName).Start(
context.Background(),
name,
apitrace.ChildOf(remoteSpanContext()),
apitrace.WithRecord(),
args...,
)
return span
}
Expand Down
1 change: 1 addition & 0 deletions sdk/trace/tracer.go
Original file line number Diff line number Diff line change
Expand Up @@ -63,6 +63,7 @@ func (tr *tracer) Start(ctx context.Context, name string, o ...apitrace.SpanOpti
for _, l := range opts.Links {
span.AddLink(l)
}
span.SetAttributes(opts.Attributes...)

span.tracer = tr

Expand Down

0 comments on commit d25fae7

Please sign in to comment.