Skip to content

Commit 784479a

Browse files
authored
Merge pull request #17 from snabble/add-tracing-to-our-log-infra
Add tracing to our log infra
2 parents dbb2c56 + 96d576b commit 784479a

File tree

12 files changed

+230
-290
lines changed

12 files changed

+230
-290
lines changed

.travis.yml

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
language: go
22
go:
3-
- 1.14.x
3+
- 1.17.x
44
script:
55
- go vet github.com/snabble/go-logging/...
66
- go test --cover github.com/snabble/go-logging/...

cookie.go

Lines changed: 0 additions & 61 deletions
This file was deleted.

cookie_test.go

Lines changed: 0 additions & 56 deletions
This file was deleted.

correlation_id.go

Lines changed: 0 additions & 50 deletions
This file was deleted.

go.mod

Lines changed: 14 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -4,15 +4,27 @@ go 1.17
44

55
require (
66
github.com/sirupsen/logrus v1.8.1
7-
github.com/stretchr/testify v1.3.0
7+
github.com/stretchr/testify v1.7.0
8+
github.com/uptrace/opentelemetry-go-extra/otellogrus v0.1.8
9+
go.opentelemetry.io/contrib/instrumentation/net/http/otelhttp v0.28.0
10+
go.opentelemetry.io/otel v1.3.0
11+
go.opentelemetry.io/otel/sdk v1.3.0
12+
go.opentelemetry.io/otel/trace v1.3.0
813
golang.org/x/xerrors v0.0.0-20200804184101-5ec99f83aff1
914
gorm.io/gorm v1.21.4
1015
)
1116

1217
require (
1318
github.com/davecgh/go-spew v1.1.1 // indirect
19+
github.com/felixge/httpsnoop v1.0.2 // indirect
20+
github.com/go-logr/logr v1.2.1 // indirect
21+
github.com/go-logr/stdr v1.2.0 // indirect
1422
github.com/jinzhu/inflection v1.0.0 // indirect
1523
github.com/jinzhu/now v1.1.2 // indirect
1624
github.com/pmezard/go-difflib v1.0.0 // indirect
17-
golang.org/x/sys v0.0.0-20210319071255-635bc2c9138d // indirect
25+
github.com/uptrace/opentelemetry-go-extra/otelutil v0.1.8 // indirect
26+
go.opentelemetry.io/otel/internal/metric v0.26.0 // indirect
27+
go.opentelemetry.io/otel/metric v0.26.0 // indirect
28+
golang.org/x/sys v0.0.0-20220114195835-da31bd327af9 // indirect
29+
gopkg.in/yaml.v3 v3.0.0-20200313102051-9f266ea9e77c // indirect
1830
)

go.sum

Lines changed: 38 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,15 @@
11
github.com/davecgh/go-spew v1.1.0/go.mod h1:J7Y8YcW2NihsgmVo/mv3lAwl/skON4iLHjSsI+c5H38=
22
github.com/davecgh/go-spew v1.1.1 h1:vj9j/u1bqnvCEfJOwUhtlOARqs3+rkHYY13jYWTU97c=
33
github.com/davecgh/go-spew v1.1.1/go.mod h1:J7Y8YcW2NihsgmVo/mv3lAwl/skON4iLHjSsI+c5H38=
4+
github.com/felixge/httpsnoop v1.0.2 h1:+nS9g82KMXccJ/wp0zyRW9ZBHFETmMGtkk+2CTTrW4o=
5+
github.com/felixge/httpsnoop v1.0.2/go.mod h1:m8KPJKqk1gH5J9DgRY2ASl2lWCfGKXixSwevea8zH2U=
6+
github.com/go-logr/logr v1.2.0/go.mod h1:jdQByPbusPIv2/zmleS9BjJVeZ6kBagPoEUsqbVz/1A=
7+
github.com/go-logr/logr v1.2.1 h1:DX7uPQ4WgAWfoh+NGGlbJQswnYIVvz0SRlLS3rPZQDA=
8+
github.com/go-logr/logr v1.2.1/go.mod h1:jdQByPbusPIv2/zmleS9BjJVeZ6kBagPoEUsqbVz/1A=
9+
github.com/go-logr/stdr v1.2.0 h1:j4LrlVXgrbIWO83mmQUnK0Hi+YnbD+vzrE1z/EphbFE=
10+
github.com/go-logr/stdr v1.2.0/go.mod h1:YkVgnZu1ZjjL7xTxrfm/LLZBfkhTqSR1ydtm6jTKKwI=
11+
github.com/google/go-cmp v0.5.6 h1:BKbKCqvP6I+rmFHt06ZmyQtvB8xAkWdhFyr0ZUNZcxQ=
12+
github.com/google/go-cmp v0.5.6/go.mod h1:v8dTdLbMG2kIc/vJvl+f65V22dbkXbowE6jgT/gNBxE=
413
github.com/jinzhu/inflection v1.0.0 h1:K317FqzuhWc8YvSVlFMCCUb36O/S9MCKRDI7QkRKD/E=
514
github.com/jinzhu/inflection v1.0.0/go.mod h1:h+uFLlag+Qp1Va5pdKtLDYj+kHp5pxUVkryuEj+Srlc=
615
github.com/jinzhu/now v1.1.1/go.mod h1:d3SSVoowX0Lcu0IBviAWJpolVfI5UJVZZ7cO71lE/z8=
@@ -12,12 +21,37 @@ github.com/sirupsen/logrus v1.8.1 h1:dJKuHgqk1NNQlqoA6BTlM1Wf9DOH3NBjQyu0h9+AZZE
1221
github.com/sirupsen/logrus v1.8.1/go.mod h1:yWOB1SBYBC5VeMP7gHvWumXLIWorT60ONWic61uBYv0=
1322
github.com/stretchr/objx v0.1.0/go.mod h1:HFkY916IF+rwdDfMAkV7OtwuqBVzrE8GR6GFx+wExME=
1423
github.com/stretchr/testify v1.2.2/go.mod h1:a8OnRcib4nhh0OaRAV+Yts87kKdq0PP7pXfy6kDkUVs=
15-
github.com/stretchr/testify v1.3.0 h1:TivCn/peBQ7UY8ooIcPgZFpTNSz0Q2U6UrFlUfqbe0Q=
16-
github.com/stretchr/testify v1.3.0/go.mod h1:M5WIy9Dh21IEIfnGCwXGc5bZfKNJtfHm1UVUgZn+9EI=
24+
github.com/stretchr/testify v1.7.0 h1:nwc3DEeHmmLAfoZucVR881uASk0Mfjw8xYJ99tb5CcY=
25+
github.com/stretchr/testify v1.7.0/go.mod h1:6Fq8oRcR53rry900zMqJjRRixrwX3KX962/h/Wwjteg=
26+
github.com/uptrace/opentelemetry-go-extra/otellogrus v0.1.8 h1:E4FnHQzQbwBJheuLL6TAoYEbgAOebKlQXGJCP1OBwJo=
27+
github.com/uptrace/opentelemetry-go-extra/otellogrus v0.1.8/go.mod h1:Glauz9iTsKLgmKwAfHBEmzfNHmsatQOg7bsxuO8trG8=
28+
github.com/uptrace/opentelemetry-go-extra/otelutil v0.1.8 h1:AAv5TtXlQ1Xvu+Ug5gIugO7QHH45O+1SKbVGyK+gOZg=
29+
github.com/uptrace/opentelemetry-go-extra/otelutil v0.1.8/go.mod h1:Gd346GeBOOu+vrrO4NQGmt1jW8AwP2ezQhD3A/AeC5g=
30+
go.opentelemetry.io/contrib/instrumentation/net/http/otelhttp v0.28.0 h1:hpEoMBvKLC6CqFZogJypr9IHwwSNF3ayEkNzD502QAM=
31+
go.opentelemetry.io/contrib/instrumentation/net/http/otelhttp v0.28.0/go.mod h1:Ihno+mNBfZlT0Qot3XyRTdZ/9U/Cg2Pfgj75DTdIfq4=
32+
go.opentelemetry.io/otel v1.0.1/go.mod h1:OPEOD4jIT2SlZPMmwT6FqZz2C0ZNdQqiWcoK6M0SNFU=
33+
go.opentelemetry.io/otel v1.3.0 h1:APxLf0eiBwLl+SOXiJJCVYzA1OOJNyAoV8C5RNRyy7Y=
34+
go.opentelemetry.io/otel v1.3.0/go.mod h1:PWIKzi6JCp7sM0k9yZ43VX+T345uNbAkDKwHVjb2PTs=
35+
go.opentelemetry.io/otel/internal/metric v0.26.0 h1:dlrvawyd/A+X8Jp0EBT4wWEe4k5avYaXsXrBr4dbfnY=
36+
go.opentelemetry.io/otel/internal/metric v0.26.0/go.mod h1:CbBP6AxKynRs3QCbhklyLUtpfzbqCLiafV9oY2Zj1Jk=
37+
go.opentelemetry.io/otel/metric v0.26.0 h1:VaPYBTvA13h/FsiWfxa3yZnZEm15BhStD8JZQSA773M=
38+
go.opentelemetry.io/otel/metric v0.26.0/go.mod h1:c6YL0fhRo4YVoNs6GoByzUgBp36hBL523rECoZA5UWg=
39+
go.opentelemetry.io/otel/sdk v1.0.1/go.mod h1:HrdXne+BiwsOHYYkBE5ysIcv2bvdZstxzmCQhxTcZkI=
40+
go.opentelemetry.io/otel/sdk v1.3.0 h1:3278edCoH89MEJ0Ky8WQXVmDQv3FX4ZJ3Pp+9fJreAI=
41+
go.opentelemetry.io/otel/sdk v1.3.0/go.mod h1:rIo4suHNhQwBIPg9axF8V9CA72Wz2mKF1teNrup8yzs=
42+
go.opentelemetry.io/otel/trace v1.0.1/go.mod h1:5g4i4fKLaX2BQpSBsxw8YYcgKpMMSW3x7ZTuYBr3sUk=
43+
go.opentelemetry.io/otel/trace v1.3.0 h1:doy8Hzb1RJ+I3yFhtDmwNc7tIyw1tNMOIsyPzp1NOGY=
44+
go.opentelemetry.io/otel/trace v1.3.0/go.mod h1:c/VDhno8888bvQYmbYLqe41/Ldmr/KKunbvWM4/fEjk=
1745
golang.org/x/sys v0.0.0-20191026070338-33540a1f6037/go.mod h1:h1NjWce9XRLGQEsW7wpKNCjG9DtNlClVuFLEZdDNbEs=
18-
golang.org/x/sys v0.0.0-20210319071255-635bc2c9138d h1:jbzgAvDZn8aEnytae+4ou0J0GwFZoHR0hOrTg4qH8GA=
19-
golang.org/x/sys v0.0.0-20210319071255-635bc2c9138d/go.mod h1:h1NjWce9XRLGQEsW7wpKNCjG9DtNlClVuFLEZdDNbEs=
46+
golang.org/x/sys v0.0.0-20210423185535-09eb48e85fd7/go.mod h1:h1NjWce9XRLGQEsW7wpKNCjG9DtNlClVuFLEZdDNbEs=
47+
golang.org/x/sys v0.0.0-20220114195835-da31bd327af9 h1:XfKQ4OlFl8okEOr5UvAqFRVj8pY/4yfcXrddB8qAbU0=
48+
golang.org/x/sys v0.0.0-20220114195835-da31bd327af9/go.mod h1:oPkhp1MJrh7nUepCBck5+mAzfO9JrbApNNgaTdGDITg=
49+
golang.org/x/xerrors v0.0.0-20191204190536-9bdfabe68543/go.mod h1:I/5z698sn9Ka8TeJc9MKroUUfqBBauWjQqLJ2OPfmY0=
2050
golang.org/x/xerrors v0.0.0-20200804184101-5ec99f83aff1 h1:go1bK/D/BFZV2I8cIQd1NKEZ+0owSTG1fDTci4IqFcE=
2151
golang.org/x/xerrors v0.0.0-20200804184101-5ec99f83aff1/go.mod h1:I/5z698sn9Ka8TeJc9MKroUUfqBBauWjQqLJ2OPfmY0=
52+
gopkg.in/check.v1 v0.0.0-20161208181325-20d25e280405 h1:yhCVgyC4o1eVCa2tZl7eS0r+SDo693bJlVdllGtEeKM=
53+
gopkg.in/check.v1 v0.0.0-20161208181325-20d25e280405/go.mod h1:Co6ibVJAznAaIkqp8huTwlJQCZ016jof/cbN4VW5Yz0=
54+
gopkg.in/yaml.v3 v3.0.0-20200313102051-9f266ea9e77c h1:dUUwHk2QECo/6vqA44rthZ8ie2QXMNeKRTHCNY2nXvo=
55+
gopkg.in/yaml.v3 v3.0.0-20200313102051-9f266ea9e77c/go.mod h1:K4uyk7z7BCEPqu6E+C64Yfv1cQ7kz7rIZviUmN+EgEM=
2256
gorm.io/gorm v1.21.4 h1:J0xfPJMRfHgpVcYLrEAIqY/apdvTIkrltPQNHQLq9Qc=
2357
gorm.io/gorm v1.21.4/go.mod h1:0HFTzE/SqkGTzK6TlDPPQbAYCluiVvhzoA1+aVyzenw=

helper.go

Lines changed: 34 additions & 21 deletions
Original file line numberDiff line numberDiff line change
@@ -10,6 +10,7 @@ import (
1010
"time"
1111

1212
"github.com/sirupsen/logrus"
13+
"github.com/uptrace/opentelemetry-go-extra/otellogrus"
1314
)
1415

1516
// AccessLogCookiesBlacklist The of cookies which should not be logged
@@ -18,24 +19,53 @@ var AccessLogCookiesBlacklist = []string{}
1819
var LifecycleEnvVars = []string{"BUILD_NUMBER", "BUILD_HASH", "BUILD_DATE"}
1920

2021
func init() {
21-
Set("info", false)
22+
_ = Set("info", false)
23+
}
24+
25+
var DefaultLogConfig = LogConfig{EnableTraces: false, EnableTextLogging: false}
26+
27+
type LogConfig struct {
28+
EnableTraces bool
29+
EnableTextLogging bool
2230
}
2331

2432
// Set creates a new Logger with the matching specification
2533
func Set(level string, textLogging bool) error {
34+
config := &LogConfig{EnableTraces: false, EnableTextLogging: textLogging}
35+
return SetWithConfig(level, config)
36+
}
37+
38+
// SetWithConfig creates a new Logger with the matching specification based on the config, pass nil to use
39+
// the defaults.
40+
func SetWithConfig(level string, config *LogConfig) error {
41+
if config == nil {
42+
config = &DefaultLogConfig
43+
}
44+
2645
l, err := logrus.ParseLevel(level)
2746
if err != nil {
2847
return err
2948
}
3049

3150
logger := logrus.New()
32-
if textLogging {
51+
if config.EnableTextLogging {
3352
logger.Formatter = &logrus.TextFormatter{DisableColors: true}
3453
} else {
3554
logger.Formatter = &LogstashFormatter{TimestampFormat: time.RFC3339Nano}
3655
}
56+
57+
if config.EnableTraces {
58+
logger.AddHook(otellogrus.NewHook(otellogrus.WithLevels(
59+
logrus.PanicLevel,
60+
logrus.FatalLevel,
61+
logrus.ErrorLevel,
62+
logrus.WarnLevel,
63+
logrus.InfoLevel,
64+
)))
65+
}
66+
3767
logger.Level = l
38-
Log = &Logger{logger}
68+
Log = &Logger{Logger: logger, config: config}
3969
return nil
4070
}
4171

@@ -90,8 +120,6 @@ func access(r *http.Request, start time.Time, statusCode int, err error) *Entry
90120
fields[logrus.ErrorKey] = err.Error()
91121
}
92122

93-
setCorrelationIds(fields, r.Header)
94-
95123
cookies := map[string]string{}
96124
for _, c := range r.Cookies() {
97125
if !contains(AccessLogCookiesBlacklist, c.Name) {
@@ -102,7 +130,7 @@ func access(r *http.Request, start time.Time, statusCode int, err error) *Entry
102130
fields["cookies"] = cookies
103131
}
104132

105-
return Log.WithFields(fields)
133+
return Log.WithContext(r.Context()).WithFields(fields)
106134
}
107135

108136
// Call logs the result of an outgoing call
@@ -133,8 +161,6 @@ func fieldsForCall(r *http.Request, resp *http.Response, start time.Time, err er
133161
"duration": time.Since(start).Nanoseconds() / 1000000,
134162
}
135163

136-
setCorrelationIds(fields, r.Header)
137-
138164
if err != nil {
139165
fields[logrus.ErrorKey] = err.Error()
140166
}
@@ -193,7 +219,6 @@ func Application(h http.Header) *Entry {
193219
fields := logrus.Fields{
194220
"type": "application",
195221
}
196-
setCorrelationIds(fields, h)
197222
return Log.WithFields(fields)
198223
}
199224

@@ -302,18 +327,6 @@ func getRemoteIp(r *http.Request) string {
302327
return host
303328
}
304329

305-
func setCorrelationIds(fields logrus.Fields, h http.Header) {
306-
correlationId := GetCorrelationId(h)
307-
if correlationId != "" {
308-
fields["correlation_id"] = correlationId
309-
}
310-
311-
userCorrelationId := GetUserCorrelationId(h)
312-
if userCorrelationId != "" {
313-
fields["user_correlation_id"] = userCorrelationId
314-
}
315-
}
316-
317330
func contains(s []string, e string) bool {
318331
for _, a := range s {
319332
if a == e {

0 commit comments

Comments
 (0)