Skip to content

Commit

Permalink
feat: annotate incoming sample rate (#658)
Browse files Browse the repository at this point in the history
Closes #612 

This issue simply annotates all incoming spans that have an original
sample rate.

---------

Co-authored-by: Kent Quirk <kentquirk@honeycomb.io>
  • Loading branch information
fchikwekwe and kentquirk authored Apr 7, 2023
1 parent 4c6d3e0 commit 8c4e99b
Show file tree
Hide file tree
Showing 3 changed files with 63 additions and 38 deletions.
61 changes: 33 additions & 28 deletions app/app_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -257,7 +257,7 @@ func TestAppIntegration(t *testing.T) {
case <-time.After(time.Millisecond):
}
}
assert.Equal(t, `{"data":{"foo":"bar","trace.trace_id":"1"},"dataset":"dataset"}`+"\n", out.String())
assert.Equal(t, `{"data":{"foo":"bar","meta.refinery.original_sample_rate":1,"trace.trace_id":"1"},"dataset":"dataset"}`+"\n", out.String())
}

func TestAppIntegrationWithNonLegacyKey(t *testing.T) {
Expand Down Expand Up @@ -298,7 +298,7 @@ func TestAppIntegrationWithNonLegacyKey(t *testing.T) {
case <-time.After(time.Millisecond):
}
}
assert.Equal(t, `{"data":{"foo":"bar","trace.trace_id":"1"},"dataset":"dataset"}`+"\n", out.String())
assert.Equal(t, `{"data":{"foo":"bar","meta.refinery.original_sample_rate":1,"trace.trace_id":"1"},"dataset":"dataset"}`+"\n", out.String())
}

func TestPeerRouting(t *testing.T) {
Expand Down Expand Up @@ -348,23 +348,24 @@ func TestPeerRouting(t *testing.T) {
APIHost: "http://api.honeycomb.io",
Timestamp: now,
Data: map[string]interface{}{
"trace.trace_id": "1",
"trace.span_id": "0",
"trace.parent_id": "0000000000",
"key": "value",
"field0": float64(0),
"field1": float64(1),
"field2": float64(2),
"field3": float64(3),
"field4": float64(4),
"field5": float64(5),
"field6": float64(6),
"field7": float64(7),
"field8": float64(8),
"field9": float64(9),
"field10": float64(10),
"long": "this is a test of the emergency broadcast system",
"foo": "bar",
"trace.trace_id": "1",
"trace.span_id": "0",
"trace.parent_id": "0000000000",
"key": "value",
"field0": float64(0),
"field1": float64(1),
"field2": float64(2),
"field3": float64(3),
"field4": float64(4),
"field5": float64(5),
"field6": float64(6),
"field7": float64(7),
"field8": float64(8),
"field9": float64(9),
"field10": float64(10),
"long": "this is a test of the emergency broadcast system",
"meta.refinery.original_sample_rate": uint(2),
"foo": "bar",
},
Metadata: map[string]any{
"api_host": "http://api.honeycomb.io",
Expand Down Expand Up @@ -432,7 +433,7 @@ func TestHostMetadataSpanAdditions(t *testing.T) {
}
}

expectedSpan := `{"data":{"foo":"bar","meta.refinery.local_hostname":"%s","trace.trace_id":"1"},"dataset":"dataset"}` + "\n"
expectedSpan := `{"data":{"foo":"bar","meta.refinery.local_hostname":"%s","meta.refinery.original_sample_rate":1,"trace.trace_id":"1"},"dataset":"dataset"}` + "\n"
assert.Equal(t, fmt.Sprintf(expectedSpan, hostname), out.String())
}

Expand Down Expand Up @@ -488,8 +489,9 @@ func TestEventsEndpoint(t *testing.T) {
APIHost: "http://api.honeycomb.io",
Timestamp: now,
Data: map[string]interface{}{
"trace.trace_id": "1",
"foo": "bar",
"trace.trace_id": "1",
"foo": "bar",
"meta.refinery.original_sample_rate": uint(10),
},
Metadata: map[string]any{
"api_host": "http://api.honeycomb.io",
Expand Down Expand Up @@ -536,8 +538,9 @@ func TestEventsEndpoint(t *testing.T) {
APIHost: "http://api.honeycomb.io",
Timestamp: now,
Data: map[string]interface{}{
"trace.trace_id": "1",
"foo": "bar",
"trace.trace_id": "1",
"foo": "bar",
"meta.refinery.original_sample_rate": uint(10),
},
Metadata: map[string]any{
"api_host": "http://api.honeycomb.io",
Expand Down Expand Up @@ -604,8 +607,9 @@ func TestEventsEndpointWithNonLegacyKey(t *testing.T) {
APIHost: "http://api.honeycomb.io",
Timestamp: now,
Data: map[string]interface{}{
"trace.trace_id": "1",
"foo": "bar",
"trace.trace_id": "1",
"foo": "bar",
"meta.refinery.original_sample_rate": uint(10),
},
Metadata: map[string]any{
"api_host": "http://api.honeycomb.io",
Expand Down Expand Up @@ -652,8 +656,9 @@ func TestEventsEndpointWithNonLegacyKey(t *testing.T) {
APIHost: "http://api.honeycomb.io",
Timestamp: now,
Data: map[string]interface{}{
"trace.trace_id": "1",
"foo": "bar",
"trace.trace_id": "1",
"foo": "bar",
"meta.refinery.original_sample_rate": uint(10),
},
Metadata: map[string]any{
"api_host": "http://api.honeycomb.io",
Expand Down
7 changes: 3 additions & 4 deletions collect/collect.go
Original file line number Diff line number Diff line change
Expand Up @@ -595,10 +595,9 @@ func (i *InMemCollector) dealWithSentTrace(keep bool, sampleRate uint, spanCount

func mergeTraceAndSpanSampleRates(sp *types.Span, traceSampleRate uint, dryRunMode bool) {
tempSampleRate := sp.SampleRate
if traceSampleRate != 1 {
// When the sample rate from the trace is not 1 that means we are
// going to mangle the span sample rate. Write down the original sample
// rate so that that information is more easily recovered
if sp.SampleRate != 0 {
// Write down the original sample rate so that that information
// is more easily recovered
sp.Data["meta.refinery.original_sample_rate"] = sp.SampleRate
}

Expand Down
33 changes: 27 additions & 6 deletions collect/collect_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -158,7 +158,28 @@ func TestOriginalSampleRateIsNotedInMetaField(t *testing.T) {

transmission.Mux.RLock()
assert.Greater(t, len(transmission.Events), 0, "should be some events transmitted")
assert.Equal(t, uint(50), transmission.Events[0].Data["meta.refinery.original_sample_rate"], "metadata should be populated with original sample rate")
assert.Equal(t, uint(50), transmission.Events[0].Data["meta.refinery.original_sample_rate"],
"metadata should be populated with original sample rate")
transmission.Mux.RUnlock()

span := &types.Span{
TraceID: fmt.Sprintf("trace-%v", 1000),
Event: types.Event{
Dataset: "aoeu",
APIKey: legacyAPIKey,
SampleRate: 0,
Data: make(map[string]interface{}),
},
}

coll.AddSpan(span)

time.Sleep(conf.SendTickerVal * 2)

transmission.Mux.RLock()
assert.Equal(t, 2, len(transmission.Events), "should be some events transmitted")
assert.Nil(t, transmission.Events[1].Data["meta.refinery.original_sample_rate"],
"metadata should not be populated when zero")
transmission.Mux.RUnlock()
}

Expand Down Expand Up @@ -307,9 +328,9 @@ func TestDryRunMode(t *testing.T) {
GetSamplerTypeVal: &config.DeterministicSamplerConfig{
SampleRate: 10,
},
SendTickerVal: 2 * time.Millisecond,
DryRun: true,
DryRunFieldName: field,
SendTickerVal: 2 * time.Millisecond,
DryRun: true,
DryRunFieldName: field,
ParentIdFieldNames: []string{"trace.parent_id", "parentId"},
}
samplerFactory := &sample.SamplerFactory{
Expand Down Expand Up @@ -516,7 +537,7 @@ func TestSampleConfigReload(t *testing.T) {
GetTraceTimeoutVal: 60 * time.Second,
GetSamplerTypeVal: &config.DeterministicSamplerConfig{SampleRate: 1},
SendTickerVal: 2 * time.Millisecond,
ParentIdFieldNames: []string{"trace.parent_id", "parentId"},
ParentIdFieldNames: []string{"trace.parent_id", "parentId"},
GetInMemoryCollectorCacheCapacityVal: config.InMemoryCollectorCacheCapacity{CacheCapacity: 10},
}

Expand Down Expand Up @@ -689,7 +710,7 @@ func TestStableMaxAlloc(t *testing.T) {
GetSamplerTypeVal: &config.DeterministicSamplerConfig{SampleRate: 1},
SendTickerVal: 2 * time.Millisecond,
CacheOverrunStrategy: "impact",
ParentIdFieldNames: []string{"trace.parent_id", "parentId"},
ParentIdFieldNames: []string{"trace.parent_id", "parentId"},
}
coll := &InMemCollector{
Config: conf,
Expand Down

0 comments on commit 8c4e99b

Please sign in to comment.