Skip to content

Commit d25fae7

Browse files
lizthegreyrghetia
authored andcommitted
set attributes in tracer.Start() (#286)
* set attributes in tracer.Start() * add tests * remove no longer needed attribute additions
1 parent e99cac2 commit d25fae7

File tree

3 files changed

+33
-9
lines changed

3 files changed

+33
-9
lines changed

plugin/httptrace/clienttrace.go

-4
Original file line numberDiff line numberDiff line change
@@ -79,10 +79,6 @@ func NewClientTrace(ctx context.Context) *httptrace.ClientTrace {
7979

8080
func (ct *clientTracer) start(hook, spanName string, attrs ...core.KeyValue) {
8181
_, sp := ct.tr.Start(ct.Context, spanName, trace.WithAttributes(attrs...), trace.WithSpanKind(trace.SpanKindClient))
82-
// TODO(paivagustavo): remove this for loop when `trace.WithAttributes(attrs...)` works.
83-
for _, attr := range attrs {
84-
sp.SetAttribute(attr)
85-
}
8682
ct.mtx.Lock()
8783
defer ct.mtx.Unlock()
8884
if ct.root == nil {

sdk/trace/trace_test.go

+32-5
Original file line numberDiff line numberDiff line change
@@ -287,6 +287,33 @@ func TestStartSpanWithFollowsFrom(t *testing.T) {
287287

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

290+
func TestSetSpanAttributesOnStart(t *testing.T) {
291+
te := &testExporter{}
292+
tp, _ := NewProvider(WithSyncer(te))
293+
span := startSpan(tp, "StartSpanAttribute", apitrace.WithAttributes(key.String("key1", "value1")))
294+
got, err := endSpan(te, span)
295+
if err != nil {
296+
t.Fatal(err)
297+
}
298+
299+
want := &export.SpanData{
300+
SpanContext: core.SpanContext{
301+
TraceID: tid,
302+
TraceFlags: 0x1,
303+
},
304+
ParentSpanID: sid,
305+
Name: "StartSpanAttribute/span0",
306+
Attributes: []core.KeyValue{
307+
key.String("key1", "value1"),
308+
},
309+
SpanKind: "internal",
310+
HasRemoteParent: true,
311+
}
312+
if diff := cmpDiff(got, want); diff != "" {
313+
t.Errorf("SetSpanAttributesOnStart: -got +want %s", diff)
314+
}
315+
}
316+
290317
func TestSetSpanAttributes(t *testing.T) {
291318
te := &testExporter{}
292319
tp, _ := NewProvider(WithSyncer(te))
@@ -655,20 +682,20 @@ func checkChild(p core.SpanContext, apiSpan apitrace.Span) error {
655682

656683
// startSpan starts a span with a name "span0". See startNamedSpan for
657684
// details.
658-
func startSpan(tp *Provider, trName string) apitrace.Span {
659-
return startNamedSpan(tp, trName, "span0")
685+
func startSpan(tp *Provider, trName string, args ...apitrace.SpanOption) apitrace.Span {
686+
return startNamedSpan(tp, trName, "span0", args...)
660687
}
661688

662689
// startNamed Span is a test utility func that starts a span with a
663690
// passed name and with ChildOf option. remote span context contains
664691
// TraceFlags with sampled bit set. This allows the span to be
665692
// automatically sampled.
666-
func startNamedSpan(tp *Provider, trName, name string) apitrace.Span {
693+
func startNamedSpan(tp *Provider, trName, name string, args ...apitrace.SpanOption) apitrace.Span {
694+
args = append(args, apitrace.ChildOf(remoteSpanContext()), apitrace.WithRecord())
667695
_, span := tp.GetTracer(trName).Start(
668696
context.Background(),
669697
name,
670-
apitrace.ChildOf(remoteSpanContext()),
671-
apitrace.WithRecord(),
698+
args...,
672699
)
673700
return span
674701
}

sdk/trace/tracer.go

+1
Original file line numberDiff line numberDiff line change
@@ -63,6 +63,7 @@ func (tr *tracer) Start(ctx context.Context, name string, o ...apitrace.SpanOpti
6363
for _, l := range opts.Links {
6464
span.AddLink(l)
6565
}
66+
span.SetAttributes(opts.Attributes...)
6667

6768
span.tracer = tr
6869

0 commit comments

Comments
 (0)