Skip to content

Commit

Permalink
Use the X-LaunchDarkly-User-Agent header for metrics if available (#65)
Browse files Browse the repository at this point in the history
The javascript client sets this header.
  • Loading branch information
ashanbrown authored Aug 22, 2018
1 parent a0c0004 commit 232e34f
Show file tree
Hide file tree
Showing 2 changed files with 27 additions and 4 deletions.
17 changes: 13 additions & 4 deletions relay.go
Original file line number Diff line number Diff line change
Expand Up @@ -46,7 +46,8 @@ const (
defaultMetricsPrefix = "launchdarkly_relay"
defaultFlushIntervalSecs = 5

userAgentHeader = "user-agent"
userAgentHeader = "user-agent"
ldUserAgentHeader = "X-LaunchDarkly-User-Agent"
)

var (
Expand Down Expand Up @@ -756,7 +757,7 @@ func bulkEventHandler(w http.ResponseWriter, req *http.Request) {
func withGauge(handler http.HandlerFunc, measure metrics.Measure) http.HandlerFunc {
return func(w http.ResponseWriter, req *http.Request) {
ctx := getClientContext(req)
userAgent := req.Header.Get(userAgentHeader)
userAgent := getUserAgent(req)
metrics.WithGauge(ctx.getMetricsCtx(), userAgent, func() {
handler.ServeHTTP(w, req)
}, measure)
Expand All @@ -766,7 +767,7 @@ func withGauge(handler http.HandlerFunc, measure metrics.Measure) http.HandlerFu
func withCount(handler http.HandlerFunc, measure metrics.Measure) http.HandlerFunc {
return func(w http.ResponseWriter, req *http.Request) {
ctx := getClientContext(req)
userAgent := req.Header.Get(userAgentHeader)
userAgent := getUserAgent(req)
metrics.WithCount(ctx.getMetricsCtx(), userAgent, func() {
handler.ServeHTTP(w, req)
}, measure)
Expand All @@ -789,7 +790,7 @@ func requestCountMiddleware(measure metrics.Measure) mux.MiddlewareFunc {
return func(next http.Handler) http.Handler {
return http.HandlerFunc(func(w http.ResponseWriter, req *http.Request) {
ctx := getClientContext(req)
userAgent := req.Header.Get(userAgentHeader)
userAgent := getUserAgent(req)
// Ignoring internal routing error that would have been ignored anyway
route, _ := mux.CurrentRoute(req).GetPathTemplate()
metrics.WithRouteCount(ctx.getMetricsCtx(), userAgent, route, req.Method, func() {
Expand Down Expand Up @@ -880,3 +881,11 @@ func obscureKey(key string) string {
}
return key
}

// getUserAgent returns the X-LaunchDarkly-User-Agent if available, falling back to the normal "User-Agent" header
func getUserAgent(req *http.Request) string {
if agent := req.Header.Get(ldUserAgentHeader); agent != "" {
return agent
}
return req.Header.Get(userAgentHeader)
}
14 changes: 14 additions & 0 deletions relay_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -757,6 +757,20 @@ SdkKey = "sdk-98e2b0b4-2688-4a59-9810-1e0e3d798989"
"expected sdk key to be used as sdk key when both api key and sdk key are set")
}

func TestGetUserAgent(t *testing.T) {
t.Run("X-LaunchDarkly-User-Agent takes precedence", func(t *testing.T) {
req, _ := http.NewRequest("GET", "/", nil)
req.Header.Set(ldUserAgentHeader, "my-agent")
req.Header.Set(userAgentHeader, "something-else")
assert.Equal(t, "my-agent", getUserAgent(req))
})
t.Run("User-Agent is the fallback", func(t *testing.T) {
req, _ := http.NewRequest("GET", "/", nil)
req.Header.Set(userAgentHeader, "my-agent")
assert.Equal(t, "my-agent", getUserAgent(req))
})
}

// jsonFind returns the nested entity at a path in a json obj
func jsonFind(obj map[string]interface{}, paths ...string) interface{} {
var value interface{} = obj
Expand Down

0 comments on commit 232e34f

Please sign in to comment.