Skip to content
This repository was archived by the owner on Oct 3, 2023. It is now read-only.

Commit b73e52c

Browse files
gottwaldRamon Nogueira
authored andcommitted
[trace] Add DefaultTraceAttributes (#2)
The attributes are added to SpanData as long as there is not an existing attribute on the span with the same key.
1 parent adbe5d1 commit b73e52c

1 file changed

Lines changed: 18 additions & 0 deletions

File tree

stackdriver.go

Lines changed: 18 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -81,6 +81,9 @@ type Options struct {
8181
// MetricPrefix overrides the OpenCensus prefix of a stackdriver metric.
8282
// Optional.
8383
MetricPrefix string
84+
85+
// DefaultTraceAttributes will be appended to every span that is exported.
86+
DefaultTraceAttributes map[string]interface{}
8487
}
8588

8689
// Exporter is a stats.Exporter and trace.Exporter
@@ -125,9 +128,24 @@ func (e *Exporter) ExportView(vd *view.Data) {
125128

126129
// ExportSpan exports a SpanData to Stackdriver Trace.
127130
func (e *Exporter) ExportSpan(sd *trace.SpanData) {
131+
if len(e.traceExporter.o.DefaultTraceAttributes) > 0 {
132+
sd = e.sdWithDefaultTraceAttributes(sd)
133+
}
128134
e.traceExporter.ExportSpan(sd)
129135
}
130136

137+
func (e *Exporter) sdWithDefaultTraceAttributes(sd *trace.SpanData) *trace.SpanData {
138+
newSD := *sd
139+
newSD.Attributes = make(map[string]interface{})
140+
for k, v := range e.traceExporter.o.DefaultTraceAttributes {
141+
newSD.Attributes[k] = v
142+
}
143+
for k, v := range sd.Attributes {
144+
newSD.Attributes[k] = v
145+
}
146+
return &newSD
147+
}
148+
131149
// Flush waits for exported data to be uploaded.
132150
//
133151
// This is useful if your program is ending and you do not

0 commit comments

Comments
 (0)