Skip to content

Commit

Permalink
fix: forward the sample rate from the message (#146)
Browse files Browse the repository at this point in the history
If the app that we're forwarding telemetry for is doing it's own
sampling, we want to honor that

Closes #145
  • Loading branch information
cursedquail authored Nov 5, 2024
1 parent 4d54d9d commit 185fa4e
Show file tree
Hide file tree
Showing 2 changed files with 84 additions and 3 deletions.
26 changes: 25 additions & 1 deletion logsapi/server.go
Original file line number Diff line number Diff line change
Expand Up @@ -85,6 +85,7 @@ func handler(client eventCreator) http.HandlerFunc {
// data is not a map, so treat the record as flat JSON adding all keys as fields
event.Add(jsonRecord)
}
event.SampleRate = parseSampleRate(jsonRecord)
}
default:
// In the case of platform.start and platform.report messages, msg.Record
Expand All @@ -93,7 +94,7 @@ func handler(client eventCreator) http.HandlerFunc {
event.Add(msg.Record)
}
event.Metadata, _ = event.Fields()["name"]
event.Send()
event.SendPresampled()
log.Debug("handler - event enqueued")
}
}
Expand All @@ -111,6 +112,29 @@ func parseMessageTimestamp(event *libhoney.Event, msg LogMessage) time.Time {
return ts
}

func parseSampleRate(body map[string]interface{}) uint {
rate, ok := body["samplerate"]
var foundRate int

if ok {
// samplerate may be a float (e.g. 43.23), integer (e.g. 54) or a string (e.g. "43")
switch sampleRate := rate.(type) {
case float64:
foundRate = int(sampleRate)
case int64:
foundRate = int(sampleRate)
case string:
if d, err := strconv.Atoi(sampleRate); err == nil {
foundRate = d
}
}
}
if foundRate < 1 {
return 1
}
return uint(foundRate)
}

// parseFunctionTimestamp is a helper function that will return a timestamp for a function log message.
// There are some precedence rules:
//
Expand Down
61 changes: 59 additions & 2 deletions logsapi/server_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -9,9 +9,8 @@ import (
"testing"
"time"

"github.com/honeycombio/libhoney-go/transmission"

libhoney "github.com/honeycombio/libhoney-go"
"github.com/honeycombio/libhoney-go/transmission"
"github.com/stretchr/testify/assert"
)

Expand Down Expand Up @@ -213,3 +212,61 @@ func TestTimestampsFunctionMessageWithDuration(t *testing.T) {
assert.Equal(t, ts.String(), event.Timestamp.String())

}

func TestLibhoneyEventWithSampleRate(t *testing.T) {
t.Run("integer", func(t *testing.T) {
events := postMessages(t, []LogMessage{{
Time: epochTimestamp,
Type: "function",
Record: `{"time": "2020-12-25T12:34:56.789Z", "samplerate": 5, "data": {"foo": "bar", "duration_ms": 54} }`,
}})
event := events[0]
assert.EqualValues(t, 5, event.SampleRate)
})
t.Run("float", func(t *testing.T) {
events := postMessages(t, []LogMessage{{
Time: epochTimestamp,
Type: "function",
Record: `{"time": "2020-12-25T12:34:56.789Z", "samplerate": 10.1, "data": {"foo": "bar", "duration_ms": 54} }`,
}})
event := events[0]
// we round downwards
assert.EqualValues(t, 10, event.SampleRate)
})
t.Run("string", func(t *testing.T) {
events := postMessages(t, []LogMessage{{
Time: epochTimestamp,
Type: "function",
Record: `{"time": "2020-12-25T12:34:56.789Z", "samplerate": "11", "data": {"foo": "bar", "duration_ms": 54} }`,
}})
event := events[0]
assert.EqualValues(t, 11, event.SampleRate)
})
t.Run("string without a number", func(t *testing.T) {
events := postMessages(t, []LogMessage{{
Time: epochTimestamp,
Type: "function",
Record: `{"time": "2020-12-25T12:34:56.789Z", "samplerate": "hello", "data": {"foo": "bar", "duration_ms": 54} }`,
}})
event := events[0]
assert.EqualValues(t, 1, event.SampleRate)
})
t.Run("bool", func(t *testing.T) {
events := postMessages(t, []LogMessage{{
Time: epochTimestamp,
Type: "function",
Record: `{"time": "2020-12-25T12:34:56.789Z", "samplerate": true, "data": {"foo": "bar", "duration_ms": 54} }`,
}})
event := events[0]
assert.EqualValues(t, 1, event.SampleRate)
})
t.Run("negative number", func(t *testing.T) {
events := postMessages(t, []LogMessage{{
Time: epochTimestamp,
Type: "function",
Record: `{"time": "2020-12-25T12:34:56.789Z", "samplerate": -12, "data": {"foo": "bar", "duration_ms": 54} }`,
}})
event := events[0]
assert.EqualValues(t, 1, event.SampleRate)
})
}

0 comments on commit 185fa4e

Please sign in to comment.