Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

fix: Only set incoming user agent if not already present #1366

Merged
merged 1 commit into from
Oct 7, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
23 changes: 7 additions & 16 deletions app/app_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,6 @@ import (
"net/http"
"net/http/httptest"
"os"
"runtime"
"strconv"
"strings"
"sync"
Expand All @@ -28,7 +27,6 @@ import (

"github.com/honeycombio/libhoney-go"
"github.com/honeycombio/libhoney-go/transmission"
"github.com/honeycombio/libhoney-go/version"
"github.com/honeycombio/refinery/collect"
"github.com/honeycombio/refinery/config"
"github.com/honeycombio/refinery/internal/health"
Expand Down Expand Up @@ -223,6 +221,7 @@ func newStartedApp(
}

func post(t testing.TB, req *http.Request) {
req.Header.Set("User-Agent", "Test-Client")
resp, err := httpClient.Do(req)
assert.NoError(t, err)
assert.Equal(t, http.StatusOK, resp.StatusCode)
Expand Down Expand Up @@ -405,7 +404,7 @@ func TestPeerRouting(t *testing.T) {
"field10": float64(10),
"long": "this is a test of the emergency broadcast system",
"meta.refinery.original_sample_rate": uint(2),
"meta.refinery.incoming_user_agent": getLibhoneyUserAgent(),
"meta.refinery.incoming_user_agent": "Test-Client",
"foo": "bar",
},
Metadata: map[string]any{
Expand Down Expand Up @@ -538,7 +537,7 @@ func TestEventsEndpoint(t *testing.T) {
"trace.trace_id": "1",
"foo": "bar",
"meta.refinery.original_sample_rate": uint(10),
"meta.refinery.incoming_user_agent": getLibhoneyUserAgent(),
"meta.refinery.incoming_user_agent": "Test-Client",
},
Metadata: map[string]any{
"api_host": "http://api.honeycomb.io",
Expand Down Expand Up @@ -586,7 +585,7 @@ func TestEventsEndpoint(t *testing.T) {
"trace.trace_id": "1",
"foo": "bar",
"meta.refinery.original_sample_rate": uint(10),
"meta.refinery.incoming_user_agent": getLibhoneyUserAgent(),
"meta.refinery.incoming_user_agent": "Test-Client",
},
Metadata: map[string]any{
"api_host": "http://api.honeycomb.io",
Expand Down Expand Up @@ -661,7 +660,7 @@ func TestEventsEndpointWithNonLegacyKey(t *testing.T) {
"trace.trace_id": traceID,
"foo": "bar",
"meta.refinery.original_sample_rate": uint(10),
"meta.refinery.incoming_user_agent": getLibhoneyUserAgent(),
"meta.refinery.incoming_user_agent": "Test-Client",
},
Metadata: map[string]any{
"api_host": "http://api.honeycomb.io",
Expand Down Expand Up @@ -710,7 +709,7 @@ func TestEventsEndpointWithNonLegacyKey(t *testing.T) {
"trace.trace_id": traceID,
"foo": "bar",
"meta.refinery.original_sample_rate": uint(10),
"meta.refinery.incoming_user_agent": getLibhoneyUserAgent(),
"meta.refinery.incoming_user_agent": "Test-Client",
},
Metadata: map[string]any{
"api_host": "http://api.honeycomb.io",
Expand Down Expand Up @@ -780,7 +779,7 @@ func TestPeerRouting_TraceLocalityDisabled(t *testing.T) {
"meta.refinery.min_span": true,
"meta.annotation_type": types.SpanAnnotationTypeUnknown,
"meta.refinery.root": false,
"meta.refinery.span_data_size": 175,
"meta.refinery.span_data_size": 168,
},
Metadata: map[string]any{
"api_host": "http://localhost:17001",
Expand Down Expand Up @@ -1002,11 +1001,3 @@ func BenchmarkDistributedTraces(b *testing.B) {
sender.waitForCount(b, b.N)
})
}

// ideally we should get this from libhoney, but we don't have a way to get it yet
// this can be removed if libhoney does provide it
func getLibhoneyUserAgent() string {
baseUserAgent := fmt.Sprintf("libhoney-go/%s", version.Version)
runtimeInfo := fmt.Sprintf("%s (%s/%s)", strings.Replace(runtime.Version(), "go", "go/", 1), runtime.GOOS, runtime.GOARCH)
return fmt.Sprintf("%s %s", baseUserAgent, runtimeInfo)
}
2 changes: 1 addition & 1 deletion route/route.go
Original file line number Diff line number Diff line change
Expand Up @@ -1063,7 +1063,7 @@ func getUserAgentFromRequest(req *http.Request) string {
}

func addIncomingUserAgent(ev *types.Event, userAgent string) {
if userAgent != "" {
if userAgent != "" && ev.Data["meta.refinery.incoming_user_agent"] == nil {
ev.Data["meta.refinery.incoming_user_agent"] = userAgent
}
}
22 changes: 22 additions & 0 deletions route/route_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -774,3 +774,25 @@ func TestIsRootSpan(t *testing.T) {
})
}
}

func TestAddIncomingUserAgent(t *testing.T) {
t.Run("no incoming user agent", func(t *testing.T) {
event := &types.Event{
Data: map[string]interface{}{},
}

addIncomingUserAgent(event, "test-agent")
require.Equal(t, "test-agent", event.Data["meta.refinery.incoming_user_agent"])
})

t.Run("existing incoming user agent", func(t *testing.T) {
event := &types.Event{
Data: map[string]interface{}{
"meta.refinery.incoming_user_agent": "test-agent",
},
}

addIncomingUserAgent(event, "another-test-agent")
require.Equal(t, "test-agent", event.Data["meta.refinery.incoming_user_agent"])
})
}
Loading