Skip to content
This repository was archived by the owner on Aug 30, 2019. It is now read-only.

Commit 4b8821b

Browse files
committed
model: add and use Span.SetMetric
1 parent 5c3e4dd commit 4b8821b

File tree

5 files changed

+12
-14
lines changed

5 files changed

+12
-14
lines changed

cmd/trace-agent/transaction_sampler_test.go

+1-2
Original file line numberDiff line numberDiff line change
@@ -12,8 +12,7 @@ import (
1212
func createTrace(serviceName string, operationName string, topLevel bool, hasPriority bool, priority int) processedTrace {
1313
ws := model.WeightedSpan{TopLevel: topLevel, Span: &model.Span{Service: serviceName, Name: operationName}}
1414
if hasPriority {
15-
ws.Metrics = make(map[string]float64)
16-
ws.Metrics[sampler.SamplingPriorityKey] = float64(priority)
15+
ws.SetMetric(sampler.SamplingPriorityKey, float64(priority))
1716
}
1817
wt := model.WeightedTrace{&ws}
1918
return processedTrace{WeightedTrace: wt, Root: ws.Span}

model/span.go

+8
Original file line numberDiff line numberDiff line change
@@ -44,3 +44,11 @@ func (s *Span) Weight() float64 {
4444

4545
return 1.0 / sampleRate
4646
}
47+
48+
// SetMetric sets a value in the span Metrics map.
49+
func (s *Span) SetMetric(key string, val float64) {
50+
if s.Metrics == nil {
51+
s.Metrics = make(map[string]float64)
52+
}
53+
s.Metrics[key] = val
54+
}

model/top_level.go

+1-7
Original file line numberDiff line numberDiff line change
@@ -44,17 +44,11 @@ func (s *Span) setTopLevel(topLevel bool) {
4444
return
4545
}
4646
delete(s.Metrics, topLevelKey)
47-
if len(s.Metrics) == 0 {
48-
s.Metrics = nil
49-
}
5047
return
5148
}
52-
if s.Metrics == nil {
53-
s.Metrics = make(map[string]float64, 1)
54-
}
5549
// Setting the metrics value, so that code downstream in the pipeline
5650
// can identify this as top-level without recomputing everything.
57-
s.Metrics[topLevelKey] = 1
51+
s.SetMetric(topLevelKey, 1)
5852
}
5953

6054
// TopLevel returns true if span is top-level.

model/top_level_test.go

+1-1
Original file line numberDiff line numberDiff line change
@@ -139,7 +139,7 @@ func TestTopLevelGetSetMetrics(t *testing.T) {
139139
span.setTopLevel(true)
140140
assert.Equal(float64(1), span.Metrics["_top_level"], "should have a _top_level:1 flag")
141141
span.setTopLevel(false)
142-
assert.Nil(span.Metrics, "no meta at all")
142+
assert.Equal(len(span.Metrics), 0, "no meta at all")
143143

144144
span.Metrics = map[string]float64{"custom": 42}
145145

sampler/coresampler.go

+1-4
Original file line numberDiff line numberDiff line change
@@ -174,8 +174,5 @@ func GetTraceAppliedSampleRate(root *model.Span) float64 {
174174

175175
// SetTraceAppliedSampleRate sets the currently applied sample rate in the trace data to allow chained up sampling.
176176
func SetTraceAppliedSampleRate(root *model.Span, sampleRate float64) {
177-
if root.Metrics == nil {
178-
root.Metrics = make(map[string]float64)
179-
}
180-
root.Metrics[model.SpanSampleRateMetricKey] = sampleRate
177+
root.SetMetric(model.SpanSampleRateMetricKey, sampleRate)
181178
}

0 commit comments

Comments
 (0)