Skip to content

Commit

Permalink
fix: error log event metadata (honeycombio#422)
Browse files Browse the repository at this point in the history
aligned metadata attached to events with what we attach to error logs
  • Loading branch information
vreynolds authored Mar 24, 2022
1 parent 7a4a510 commit a2e6182
Show file tree
Hide file tree
Showing 2 changed files with 35 additions and 5 deletions.
25 changes: 25 additions & 0 deletions app/app_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -363,6 +363,11 @@ func TestPeerRouting(t *testing.T) {
"long": "this is a test of the emergency broadcast system",
"foo": "bar",
},
Metadata: map[string]string{
"api_host": "http://api.honeycomb.io",
"dataset": "dataset",
"environment": "",
},
}
assert.Equal(t, expectedEvent, senders[0].Events()[0])

Expand Down Expand Up @@ -482,6 +487,11 @@ func TestEventsEndpoint(t *testing.T) {
"trace.trace_id": "1",
"foo": "bar",
},
Metadata: map[string]string{
"api_host": "http://api.honeycomb.io",
"dataset": "dataset",
"environment": "",
},
},
senders[0].Events()[0],
)
Expand Down Expand Up @@ -524,6 +534,11 @@ func TestEventsEndpoint(t *testing.T) {
"trace.trace_id": "1",
"foo": "bar",
},
Metadata: map[string]string{
"api_host": "http://api.honeycomb.io",
"dataset": "dataset",
"environment": "",
},
},
senders[1].Events()[0],
)
Expand Down Expand Up @@ -586,6 +601,11 @@ func TestEventsEndpointWithNonLegacyKey(t *testing.T) {
"trace.trace_id": "1",
"foo": "bar",
},
Metadata: map[string]string{
"api_host": "http://api.honeycomb.io",
"dataset": "dataset",
"environment": "test",
},
},
senders[0].Events()[0],
)
Expand Down Expand Up @@ -628,6 +648,11 @@ func TestEventsEndpointWithNonLegacyKey(t *testing.T) {
"trace.trace_id": "1",
"foo": "bar",
},
Metadata: map[string]string{
"api_host": "http://api.honeycomb.io",
"dataset": "dataset",
"environment": "test",
},
},
senders[1].Events()[0],
)
Expand Down
15 changes: 10 additions & 5 deletions transmit/transmit.go
Original file line number Diff line number Diff line change
Expand Up @@ -97,6 +97,12 @@ func (d *DefaultTransmission) EnqueueEvent(ev *types.Event) {
libhEv.Dataset = ev.Dataset
libhEv.SampleRate = ev.SampleRate
libhEv.Timestamp = ev.Timestamp
// metadata is used to make error logs more helpful when processing libhoney responses
libhEv.Metadata = map[string]string{
"api_host": ev.APIHost,
"dataset": ev.Dataset,
"environment": ev.Environment,
}

for k, v := range ev.Data {
libhEv.AddField(k, v)
Expand All @@ -110,6 +116,7 @@ func (d *DefaultTransmission) EnqueueEvent(ev *types.Event) {
WithField("request_id", ev.Context.Value(types.RequestIDContextKey{})).
WithString("dataset", ev.Dataset).
WithString("api_host", ev.APIHost).
WithString("environment", ev.Environment).
Logf("failed to enqueue event")
}
}
Expand Down Expand Up @@ -141,19 +148,17 @@ func (d *DefaultTransmission) processResponses(
select {
case r := <-responses:
if r.Err != nil || r.StatusCode > 202 {
var apiHost, dataset, evType, target string
var apiHost, dataset, environment string
if metadata, ok := r.Metadata.(map[string]string); ok {
apiHost = metadata["api_host"]
dataset = metadata["dataset"]
evType = metadata["type"]
target = metadata["target"]
environment = metadata["environment"]
}
log := d.Logger.Error().WithFields(map[string]interface{}{
"status_code": r.StatusCode,
"api_host": apiHost,
"dataset": dataset,
"event_type": evType,
"target": target,
"environment": environment,
})
if r.Err != nil {
log = log.WithField("error", r.Err.Error())
Expand Down

0 comments on commit a2e6182

Please sign in to comment.