Skip to content
This repository was archived by the owner on Jun 12, 2024. It is now read-only.

Commit 93d323a

Browse files
authored
Use jaeger env for configuration and add chi path method (#8)
**What** - Remove server parameter from the tracing configuration, this value will now be set by Jaeger's own method via the env - Add a new path cleaning method that uses Chi mux route lookup so that we can support name based resolution on its own Signed-off-by: Lucas Roesler <[email protected]>
1 parent 9ad5404 commit 93d323a

File tree

8 files changed

+87
-68
lines changed

8 files changed

+87
-68
lines changed

go.mod

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -3,12 +3,15 @@ module github.com/contiamo/go-base
33
require (
44
github.com/bakins/net-http-recover v0.0.0-20141007104922-6cba69d01459
55
github.com/codahale/hdrhistogram v0.0.0-20161010025455-3a0bb77429bd // indirect
6+
github.com/go-chi/chi v4.0.2+incompatible
67
github.com/golang/protobuf v1.3.2
78
github.com/google/uuid v1.1.1
89
github.com/gorilla/websocket v1.4.0
910
github.com/konsorten/go-windows-terminal-sequences v1.0.2 // indirect
1011
github.com/kr/pretty v0.1.0 // indirect
1112
github.com/lib/pq v1.1.1
13+
github.com/modern-go/concurrent v0.0.0-20180306012644-bacd9c7ef1dd // indirect
14+
github.com/modern-go/reflect2 v1.0.1 // indirect
1215
github.com/opentracing/opentracing-go v1.1.0
1316
github.com/pkg/errors v0.8.1 // indirect
1417
github.com/prometheus/client_golang v1.1.0
@@ -23,6 +26,7 @@ require (
2326
go.uber.org/atomic v1.4.0 // indirect
2427
golang.org/x/lint v0.0.0-20190409202823-959b441ac422
2528
golang.org/x/tools v0.0.0-20190606124116-d0a3d012864b // indirect
29+
google.golang.org/appengine v1.4.0 // indirect
2630
google.golang.org/genproto v0.0.0-20190716160619-c506a9f90610 // indirect
2731
google.golang.org/grpc v1.22.0
2832
gopkg.in/check.v1 v1.0.0-20180628173108-788fd7840127 // indirect

go.sum

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -14,6 +14,8 @@ github.com/codahale/hdrhistogram v0.0.0-20161010025455-3a0bb77429bd/go.mod h1:sE
1414
github.com/davecgh/go-spew v1.1.0/go.mod h1:J7Y8YcW2NihsgmVo/mv3lAwl/skON4iLHjSsI+c5H38=
1515
github.com/davecgh/go-spew v1.1.1 h1:vj9j/u1bqnvCEfJOwUhtlOARqs3+rkHYY13jYWTU97c=
1616
github.com/davecgh/go-spew v1.1.1/go.mod h1:J7Y8YcW2NihsgmVo/mv3lAwl/skON4iLHjSsI+c5H38=
17+
github.com/go-chi/chi v4.0.2+incompatible h1:maB6vn6FqCxrpz4FqWdh4+lwpyZIQS7YEAUcHlgXVRs=
18+
github.com/go-chi/chi v4.0.2+incompatible/go.mod h1:eB3wogJHnLi3x/kFX2A+IbTBlXxmMeXJVKy9tTv1XzQ=
1719
github.com/go-kit/kit v0.8.0/go.mod h1:xBxKIO96dXMWWy0MnWVtmwkA9/13aqxPnvrjFYMA2as=
1820
github.com/go-logfmt/logfmt v0.3.0/go.mod h1:Qt1PoO58o5twSAckw1HlFXLmHsOX5/0LbT9GBnD5lWE=
1921
github.com/go-logfmt/logfmt v0.4.0/go.mod h1:3RMwSq7FuexP4Kalkev3ejPJsZTpXXBr9+V4qmtdjCk=

pkg/http/middleware/metrics.go

Lines changed: 0 additions & 17 deletions
Original file line numberDiff line numberDiff line change
@@ -6,7 +6,6 @@ import (
66
"strings"
77
"time"
88

9-
uuid "github.com/google/uuid"
109
"github.com/urfave/negroni"
1110

1211
server "github.com/contiamo/go-base/pkg/http"
@@ -95,19 +94,3 @@ func (opt *metricsOption) WrapHandler(handler http.Handler) http.Handler {
9594

9695
return mw
9796
}
98-
99-
// PathWithCleanID replace string values that look like ids (uuids and int) with "*"
100-
func PathWithCleanID(r *http.Request) string {
101-
pathParts := strings.Split(r.URL.Path, "/")
102-
for i, part := range pathParts {
103-
if _, err := uuid.Parse(part); err == nil {
104-
pathParts[i] = "*"
105-
continue
106-
}
107-
if _, err := strconv.Atoi(part); err == nil {
108-
pathParts[i] = "*"
109-
}
110-
111-
}
112-
return strings.Join(pathParts, "/")
113-
}

pkg/http/middleware/path.go

Lines changed: 48 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,48 @@
1+
package middleware
2+
3+
import (
4+
"net/http"
5+
"strconv"
6+
"strings"
7+
8+
"github.com/go-chi/chi"
9+
"github.com/google/uuid"
10+
)
11+
12+
// PathWithCleanID replace string values that look like ids (uuids and int) with "*"
13+
func PathWithCleanID(r *http.Request) string {
14+
pathParts := strings.Split(r.URL.Path, "/")
15+
for i, part := range pathParts {
16+
if _, err := uuid.Parse(part); err == nil {
17+
pathParts[i] = "*"
18+
continue
19+
}
20+
if _, err := strconv.Atoi(part); err == nil {
21+
pathParts[i] = "*"
22+
}
23+
24+
}
25+
return strings.Join(pathParts, "/")
26+
}
27+
28+
// MethodAndPathCleanID replace string values that look like ids (uuids and int)
29+
// with "*"
30+
func MethodAndPathCleanID(r *http.Request) string {
31+
pathParts := strings.Split(r.URL.Path, "/")
32+
for i, part := range pathParts {
33+
if _, err := uuid.Parse(part); err == nil {
34+
pathParts[i] = "*"
35+
continue
36+
}
37+
if _, err := strconv.Atoi(part); err == nil {
38+
pathParts[i] = "*"
39+
}
40+
41+
}
42+
return "HTTP " + r.Method + " " + strings.Join(pathParts, "/")
43+
}
44+
45+
// ChiRouteName replace route parameters from the Chi route mux with "*"
46+
func ChiRouteName(r *http.Request) string {
47+
return "HTTP " + r.Method + " " + chi.RouteContext(r.Context()).RoutePattern()
48+
}

pkg/http/middleware/tracing.go

Lines changed: 7 additions & 27 deletions
Original file line numberDiff line numberDiff line change
@@ -2,35 +2,32 @@ package middleware
22

33
import (
44
"net/http"
5-
"strconv"
6-
"strings"
75

8-
uuid "github.com/google/uuid"
96
opentracing "github.com/opentracing/opentracing-go"
107
"github.com/opentracing/opentracing-go/ext"
118
"github.com/urfave/negroni"
129

13-
"github.com/contiamo/go-base/pkg/tracing"
1410
server "github.com/contiamo/go-base/pkg/http"
11+
"github.com/contiamo/go-base/pkg/tracing"
1512
)
1613

1714
// mainly from "github.com/opentracing-contrib/go-stdlib/nethttp"
1815

1916
// WithTracing configures tracing for that server; if configuration fails, WithTracing will panic
20-
func WithTracing(server, app string, tags map[string]string, opNameFunc func(r *http.Request) string) server.Option {
17+
func WithTracing(app string, tags map[string]string, opNameFunc func(r *http.Request) string) server.Option {
2118
if opNameFunc == nil {
2219
opNameFunc = MethodAndPathCleanID
2320
}
24-
if err := tracing.InitJaeger(server, app); err != nil {
21+
if err := tracing.InitJaeger(app); err != nil {
2522
panic(err)
2623
}
27-
return &tracingOption{server, app, tags, opNameFunc}
24+
return &tracingOption{app, tags, opNameFunc}
2825
}
2926

3027
type tracingOption struct {
31-
server, app string
32-
tags map[string]string
33-
opNameFunc func(r *http.Request) string
28+
app string
29+
tags map[string]string
30+
opNameFunc func(r *http.Request) string
3431
}
3532

3633
func (opt *tracingOption) WrapHandler(handler http.Handler) http.Handler {
@@ -68,23 +65,6 @@ func operationNameFunc(f func(r *http.Request) string) mwOption {
6865
}
6966
}
7067

71-
// MethodAndPathCleanID replace string values that look like ids (uuids and int)
72-
// with "*"
73-
func MethodAndPathCleanID(r *http.Request) string {
74-
pathParts := strings.Split(r.URL.Path, "/")
75-
for i, part := range pathParts {
76-
if _, err := uuid.Parse(part); err == nil {
77-
pathParts[i] = "*"
78-
continue
79-
}
80-
if _, err := strconv.Atoi(part); err == nil {
81-
pathParts[i] = "*"
82-
}
83-
84-
}
85-
return "HTTP " + r.Method + " " + strings.Join(pathParts, "/")
86-
}
87-
8868
// mwSpanObserver returns a MWOption that observe the span
8969
// for the server-side span.
9070
func mwSpanObserver(f func(span opentracing.Span, r *http.Request)) mwOption {

pkg/http/middleware/tracing_test.go

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -18,7 +18,7 @@ func Test_TracingMiddleware(t *testing.T) {
1818
opentracing.SetGlobalTracer(tracer)
1919

2020
t.Run("should be possible to setup tracing", func(t *testing.T) {
21-
srv, err := createServer([]server.Option{WithTracing("localhost:test", "test", nil, nil)})
21+
srv, err := createServer([]server.Option{WithTracing("test", nil, nil)})
2222
require.NoError(t, err)
2323

2424
ts := httptest.NewServer(srv.Handler)
@@ -34,7 +34,7 @@ func Test_TracingMiddleware(t *testing.T) {
3434
t.Run("should be possible to set additional tags", func(t *testing.T) {
3535
tagName := "testTag"
3636
tagValue := "something to find"
37-
srv, err := createServer([]server.Option{WithTracing("localhost:test", "test", map[string]string{tagName: tagValue}, nil)})
37+
srv, err := createServer([]server.Option{WithTracing("test", map[string]string{tagName: tagValue}, nil)})
3838
require.NoError(t, err)
3939

4040
ts := httptest.NewServer(srv.Handler)
@@ -51,7 +51,7 @@ func Test_TracingMiddleware(t *testing.T) {
5151
})
5252

5353
t.Run("should replace uuid values with *", func(t *testing.T) {
54-
srv, err := createServer([]server.Option{WithTracing("localhost:test", "test", nil, nil)})
54+
srv, err := createServer([]server.Option{WithTracing("test", nil, nil)})
5555
require.NoError(t, err)
5656

5757
ts := httptest.NewServer(srv.Handler)
@@ -68,7 +68,7 @@ func Test_TracingMiddleware(t *testing.T) {
6868
})
6969

7070
t.Run("should allow websockets", func(t *testing.T) {
71-
srv, err := createServer([]server.Option{WithTracing("localhost:test", "test", nil, nil)})
71+
srv, err := createServer([]server.Option{WithTracing("test", nil, nil)})
7272
require.NoError(t, err)
7373

7474
ts := httptest.NewServer(srv.Handler)

pkg/tracing/jaeger.go

Lines changed: 4 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -14,14 +14,14 @@ var defaultJaegerServer = fmt.Sprintf("%s:%d", jaeger.DefaultUDPSpanServerHost,
1414
// InitJaeger asserts that the global tracer is initialized.
1515
//
1616
// This will read the configuration from the "JAEGER_*"" environment variables.
17-
// Overriding the empty values with the supplied server and app value. If a
17+
// Overriding the empty values with the supplied app value. If a
1818
// sampler type is not configured via the environment variables, then InitJaeger
1919
// will be configured with the constant sampler.
20-
func InitJaeger(server, app string) error {
20+
func InitJaeger(app string) error {
2121
global := opentracing.GlobalTracer()
2222
if _, ok := global.(opentracing.NoopTracer); ok {
2323

24-
cfg, err := getConfig(server, app)
24+
cfg, err := getConfig(app)
2525
if err != nil {
2626
return err
2727
}
@@ -34,7 +34,7 @@ func InitJaeger(server, app string) error {
3434
return nil
3535
}
3636

37-
func getConfig(server, app string) (*config.Configuration, error) {
37+
func getConfig(app string) (*config.Configuration, error) {
3838
cfg, err := config.FromEnv()
3939
if err != nil {
4040
return nil, err
@@ -52,9 +52,5 @@ func getConfig(server, app string) (*config.Configuration, error) {
5252
cfg.Reporter.BufferFlushInterval = 1 * time.Second
5353
}
5454

55-
if server != "" && (cfg.Reporter.LocalAgentHostPort == "" || cfg.Reporter.LocalAgentHostPort == defaultJaegerServer) {
56-
cfg.Reporter.LocalAgentHostPort = server
57-
}
58-
5955
return cfg, nil
6056
}

pkg/tracing/jaeger_test.go

Lines changed: 18 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -12,29 +12,35 @@ func Test_getConfig(t *testing.T) {
1212
cases := []struct {
1313
name string
1414
server string
15-
serverEnv string
15+
varname string
1616
expectedServer string
1717
}{
18-
{"set server from config", "example.com:6831", "", "example.com:6831"},
19-
{"set server from ENV", "", "example.com", "example.com:6831"},
18+
{"set local agent server from ENV", "example.com", "JAEGER_AGENT_HOST", "example.com:6831"},
19+
{"set remote agent server from ENV", "http://example.com", "JAEGER_ENDPOINT", "http://example.com"},
20+
{"use default local agent from env", "", "", defaultJaegerServer},
2021
}
2122

23+
currentEnvEndpoint := os.Getenv("JAEGER_ENDPOINT")
2224
currentEnvHost := os.Getenv("JAEGER_AGENT_HOST")
23-
currentEnvPort := os.Getenv("JAEGER_AGENT_PORT")
25+
defer os.Setenv("JAEGER_ENDPOINT", currentEnvEndpoint)
2426
defer os.Setenv("JAEGER_AGENT_HOST", currentEnvHost)
25-
defer os.Setenv("JAEGER_AGENT_PORT", currentEnvPort)
2627

2728
for _, tc := range cases {
28-
os.Unsetenv("JAEGER_AGENT_HOST")
29-
os.Unsetenv("JAEGER_AGENT_PORT")
30-
3129
t.Run(tc.name, func(t *testing.T) {
32-
os.Setenv("JAEGER_AGENT_HOST", tc.serverEnv)
33-
cfg, err := getConfig(tc.server, "test-app")
30+
if tc.varname != "" {
31+
os.Setenv(tc.varname, tc.server)
32+
defer os.Unsetenv(tc.varname)
33+
}
34+
35+
cfg, err := getConfig("test-app")
3436
require.NoError(t, err)
3537

36-
cfg.Reporter.LocalAgentHostPort = tc.expectedServer
37-
cfg.ServiceName = "test-app"
38+
require.Equal(t, cfg.ServiceName, "test-app")
39+
if tc.varname == "JAEGER_ENDPOINT" {
40+
require.Equal(t, tc.expectedServer, cfg.Reporter.CollectorEndpoint)
41+
} else {
42+
require.Equal(t, tc.expectedServer, cfg.Reporter.LocalAgentHostPort)
43+
}
3844
})
3945
}
4046
}

0 commit comments

Comments
 (0)