Skip to content

Commit 4262fe6

Browse files
authored
support zerolog (#260)
support zerolog
1 parent 378ec22 commit 4262fe6

File tree

11 files changed

+269
-4
lines changed

11 files changed

+269
-4
lines changed

README.md

+3-2
Original file line numberDiff line numberDiff line change
@@ -82,7 +82,9 @@ Also there are several [**documents**](./docs) that you may find useful for eith
8282
| echo | https://github.com/labstack/echo | v4.0.0 | v4.12.0 |
8383
| elasticsearch | https://github.com/elastic/go-elasticsearch| v8.4.0 | v8.15.0 |
8484
| fasthttp | https://github.com/valyala/fasthttp | v1.45.0 | v1.57.0 |
85+
| fiber | https://github.com/gofiber/fiber | v2.43.0 | v2.52.5 |
8586
| gin | https://github.com/gin-gonic/gin | v1.7.0 | v1.10.0 |
87+
| gorestful | https://github.com/emicklei/go-restful | v3.7.0 | v3.12.1 |
8688
| go-redis | https://github.com/redis/go-redis | v9.0.5 | v9.5.1 |
8789
| go-redis v8 | https://github.com/redis/go-redis | v8.11.0 | v8.11.5 |
8890
| gorm | https://github.com/go-gorm/gorm | v1.22.0 | v1.25.9 |
@@ -98,8 +100,7 @@ Also there are several [**documents**](./docs) that you may find useful for eith
98100
| redigo | https://github.com/gomodule/redigo | v1.9.0 | v1.9.2 |
99101
| slog | https://pkg.go.dev/log/slog | - | - |
100102
| zap | https://github.com/uber-go/zap | v1.20.0 | v1.27.0 |
101-
| fiber | https://github.com/gofiber/fiber | v2.43.0 | v2.52.5 |
102-
| gorestful | https://github.com/emicklei/go-restful | v3.7.0 | v3.12.1 |
103+
| zerolog | https://github.com/rs/zerolog | v1.10.0 | v1.33.0 |
103104

104105
We are progressively open-sourcing the libraries we have supported, and your contributions are very welcome 💖!
105106

docs/supported-libraries.md

+3-2
Original file line numberDiff line numberDiff line change
@@ -6,7 +6,9 @@
66
| echo | https://github.com/labstack/echo | v4.0.0 | v4.12.0 |
77
| elasticsearch| https://github.com/elastic/go-elasticsearch| v8.4.0 | v8.15.0 |
88
| fasthttp | https://github.com/valyala/fasthttp | v1.45.0 | v1.57.0 |
9+
| fiber | https://github.com/gofiber/fiber | v2.43.0 | v2.52.5 |
910
| gin | https://github.com/gin-gonic/gin | v1.7.0 | v1.10.0 |
11+
| gorestful | https://github.com/emicklei/go-restful | v3.7.0 | v3.12.1 |
1012
| go-redis | https://github.com/redis/go-redis | v9.0.5 | v9.5.1 |
1113
| go-redis v8 | https://github.com/redis/go-redis | v8.11.0 | v8.11.5 |
1214
| gorm | https://github.com/go-gorm/gorm | v1.22.0 | v1.25.9 |
@@ -22,5 +24,4 @@
2224
| redigo | https://github.com/gomodule/redigo | v1.9.0 | v1.9.2 |
2325
| slog | https://pkg.go.dev/log/slog | - | - |
2426
| zap | https://github.com/uber-go/zap | v1.20.0 | v1.27.0 |
25-
| fiber | https://github.com/gofiber/fiber | v2.43.0 | v2.52.5 |
26-
27+
| zerolog | https://github.com/rs/zerolog | v1.10.0 | v1.33.0 |

go.mod

+1
Original file line numberDiff line numberDiff line change
@@ -25,6 +25,7 @@ require (
2525
github.com/labstack/echo/v4 v4.12.0
2626
github.com/prometheus/client_golang v1.16.0
2727
github.com/redis/go-redis/v9 v9.6.1
28+
github.com/rs/zerolog v1.10.0
2829
github.com/sirupsen/logrus v1.9.3
2930
github.com/testcontainers/testcontainers-go v0.34.0
3031
github.com/testcontainers/testcontainers-go/modules/mysql v0.34.0

go.sum

+2
Original file line numberDiff line numberDiff line change
@@ -352,6 +352,8 @@ github.com/rivo/uniseg v0.2.0/go.mod h1:J6wj4VEh+S6ZtnVlnTBMWIodfgj8LQOQFoIToxlJ
352352
github.com/rogpeppe/go-internal v1.3.0/go.mod h1:M8bDsm7K2OlrFYOpmOWEs/qY81heoFRclV5y23lUDJ4=
353353
github.com/rogpeppe/go-internal v1.13.1 h1:KvO1DLK/DRN07sQ1LQKScxyZJuNnedQ5/wKSR38lUII=
354354
github.com/rogpeppe/go-internal v1.13.1/go.mod h1:uMEvuHeurkdAXX61udpOXGD/AzZDWNMNyH2VO9fmH0o=
355+
github.com/rs/zerolog v1.10.0 h1:roFDW4AgYGbHnTOAMZ2K8mHJZ/7bSj7txPfvbABIj88=
356+
github.com/rs/zerolog v1.10.0/go.mod h1:YbFCdg8HfsridGWAh22vktObvhZbQsZXe4/zB0OKkWU=
355357
github.com/ruudk/golang-pdf417 v0.0.0-20181029194003-1af4ab5afa58/go.mod h1:6lfFZQK844Gfx8o5WFuvpxWRwnSoipWe/p622j1v06w=
356358
github.com/ruudk/golang-pdf417 v0.0.0-20201230142125-a7e3863a1245/go.mod h1:pQAZKsJ8yyVxGRWYNEm9oFB8ieLgKFnamEyDmSA0BRk=
357359
github.com/sergi/go-diff v1.2.0 h1:XU+rvMAioB0UC3q1MFrIQy4Vo5/4VsRDQQXHsEya6xQ=

pkg/data/default.json

+8
Original file line numberDiff line numberDiff line change
@@ -611,6 +611,14 @@
611611
"OnExit": "restContainerHandleOnExit",
612612
"Path": "github.com/alibaba/opentelemetry-go-auto-instrumentation/pkg/rules/gorestful"
613613
},
614+
{
615+
"Version": "[1.10.0,1.33.1)",
616+
"ImportPath": "github.com/rs/zerolog",
617+
"Function": "msg",
618+
"ReceiverType": "*Event",
619+
"OnEnter": "zeroLogWriteOnEnter",
620+
"Path": "github.com/alibaba/opentelemetry-go-auto-instrumentation/pkg/rules/zerolog"
621+
},
614622
{
615623
"Version": "[0.5.1,0.11.4)",
616624
"ImportPath": "github.com/cloudwego/kitex/client",

pkg/rules/zerolog/setup.go

+36
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,36 @@
1+
// Copyright (c) 2024 Alibaba Group Holding Ltd.
2+
//
3+
// Licensed under the Apache License, Version 2.0 (the "License");
4+
// you may not use this file except in compliance with the License.
5+
// You may obtain a copy of the License at
6+
//
7+
// http://www.apache.org/licenses/LICENSE-2.0
8+
//
9+
// Unless required by applicable law or agreed to in writing, software
10+
// distributed under the License is distributed on an "AS IS" BASIS,
11+
// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
12+
// See the License for the specific language governing permissions and
13+
// limitations under the License.
14+
15+
package zerolog
16+
17+
import (
18+
"github.com/alibaba/opentelemetry-go-auto-instrumentation/pkg/api"
19+
"github.com/alibaba/opentelemetry-go-auto-instrumentation/pkg/inst-api/instrumenter"
20+
"github.com/rs/zerolog"
21+
"go.opentelemetry.io/otel/sdk/trace"
22+
)
23+
24+
var zeroLogEnabler = instrumenter.NewDefaultInstrumentEnabler()
25+
26+
func zeroLogWriteOnEnter(call api.CallContext, ce *zerolog.Event, msg string) {
27+
if !zeroLogEnabler.Enable() {
28+
return
29+
}
30+
traceId, spanId := trace.GetTraceAndSpanId()
31+
if traceId != "" && spanId != "" {
32+
cer := ce.Str("trace_id", traceId).Str("span_id", spanId)
33+
call.SetParam(0, cer)
34+
}
35+
return
36+
}

test/helloworld/go.mod

+2
Original file line numberDiff line numberDiff line change
@@ -2,6 +2,8 @@ module helloworld
22

33
go 1.22
44

5+
toolchain go1.22.7
6+
57
replace github.com/alibaba/opentelemetry-go-auto-instrumentation => ../../../opentelemetry-go-auto-instrumentation
68

79
replace github.com/alibaba/opentelemetry-go-auto-instrumentation/test/verifier => ../../../opentelemetry-go-auto-instrumentation/test/verifier

test/zerolog/go.mod

+27
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,27 @@
1+
module zerolog
2+
3+
go 1.22
4+
5+
replace github.com/alibaba/opentelemetry-go-auto-instrumentation => ../../../opentelemetry-go-auto-instrumentation
6+
7+
replace github.com/alibaba/opentelemetry-go-auto-instrumentation/test/verifier => ../../../opentelemetry-go-auto-instrumentation/test/verifier
8+
9+
require (
10+
github.com/alibaba/opentelemetry-go-auto-instrumentation/test/verifier v0.0.0-00010101000000-000000000000
11+
go.opentelemetry.io/otel/sdk v1.31.0
12+
github.com/rs/zerolog v1.10.0
13+
)
14+
15+
require (
16+
github.com/go-logr/logr v1.4.2 // indirect
17+
github.com/go-logr/stdr v1.2.2 // indirect
18+
github.com/google/uuid v1.6.0 // indirect
19+
github.com/mohae/deepcopy v0.0.0-20170929034955-c48cc78d4826 // indirect
20+
go.opentelemetry.io/otel v1.31.0 // indirect
21+
go.opentelemetry.io/otel/metric v1.31.0 // indirect
22+
go.opentelemetry.io/otel/sdk/metric v1.30.0 // indirect
23+
go.opentelemetry.io/otel/trace v1.31.0 // indirect
24+
go.uber.org/atomic v1.7.0 // indirect
25+
go.uber.org/multierr v1.6.0 // indirect
26+
golang.org/x/sys v0.26.0 // indirect
27+
)

test/zerolog/http_server.go

+54
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,54 @@
1+
// Copyright (c) 2024 Alibaba Group Holding Ltd.
2+
//
3+
// Licensed under the Apache License, Version 2.0 (the "License");
4+
// you may not use this file except in compliance with the License.
5+
// You may obtain a copy of the License at
6+
//
7+
// http://www.apache.org/licenses/LICENSE-2.0
8+
//
9+
// Unless required by applicable law or agreed to in writing, software
10+
// distributed under the License is distributed on an "AS IS" BASIS,
11+
// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
12+
// See the License for the specific language governing permissions and
13+
// limitations under the License.
14+
15+
package main
16+
17+
import (
18+
"github.com/rs/zerolog"
19+
"io"
20+
"net/http"
21+
"os"
22+
"strconv"
23+
)
24+
25+
var logger = zerolog.New(os.Stdout).With().
26+
Timestamp().
27+
Str("role", "my-service").
28+
Str("host", "127.0.0.1").
29+
Logger()
30+
31+
var port int
32+
33+
func redirectHandler(w http.ResponseWriter, r *http.Request) {
34+
resp, err := http.Get("http://127.0.0.1:" + strconv.Itoa(port) + "/b")
35+
if err != nil {
36+
logger.Printf("request provider error: %v", err)
37+
w.WriteHeader(http.StatusInternalServerError)
38+
return
39+
}
40+
defer resp.Body.Close()
41+
_, err = io.ReadAll(resp.Body)
42+
if err != nil {
43+
logger.Print(err)
44+
w.WriteHeader(http.StatusInternalServerError)
45+
return
46+
}
47+
logger.Print("abcde")
48+
_, _ = w.Write([]byte("success"))
49+
}
50+
51+
func helloHandler(w http.ResponseWriter, r *http.Request) {
52+
w.WriteHeader(http.StatusOK)
53+
_, _ = w.Write([]byte("success"))
54+
}

test/zerolog/test_zerolog.go

+90
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,90 @@
1+
// Copyright (c) 2024 Alibaba Group Holding Ltd.
2+
//
3+
// Licensed under the Apache License, Version 2.0 (the "License");
4+
// you may not use this file except in compliance with the License.
5+
// You may obtain a copy of the License at
6+
//
7+
// http://www.apache.org/licenses/LICENSE-2.0
8+
//
9+
// Unless required by applicable law or agreed to in writing, software
10+
// distributed under the License is distributed on an "AS IS" BASIS,
11+
// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
12+
// See the License for the specific language governing permissions and
13+
// limitations under the License.
14+
15+
package main
16+
17+
import (
18+
"bytes"
19+
"github.com/alibaba/opentelemetry-go-auto-instrumentation/test/verifier"
20+
"go.opentelemetry.io/otel/sdk/trace/tracetest"
21+
"log"
22+
"net/http"
23+
"strconv"
24+
"time"
25+
)
26+
27+
func setupBasicHttp() {
28+
http.HandleFunc("/a", redirectHandler)
29+
http.HandleFunc("/b", helloHandler)
30+
var err error
31+
port, err = verifier.GetFreePort()
32+
if err != nil {
33+
panic(err)
34+
}
35+
err = http.ListenAndServe(":"+strconv.Itoa(port), nil)
36+
if err != nil {
37+
panic(err)
38+
}
39+
}
40+
41+
func main() {
42+
go setupBasicHttp()
43+
time.Sleep(1 * time.Second)
44+
_, err := http.Get("http://127.0.0.1:" + strconv.Itoa(port) + "/a")
45+
if err != nil {
46+
panic(err)
47+
}
48+
jsonData := []byte(`{"key1": "value1", "key2": "value2"}`)
49+
req, err := http.NewRequest("POST", "http://127.0.0.1:"+strconv.Itoa(port)+"/a", bytes.NewBuffer(jsonData))
50+
if err != nil {
51+
panic(err)
52+
}
53+
req.Header.Set("Content-Type", "application/json")
54+
client := http.Client{}
55+
resp, err := client.Do(req)
56+
if err != nil {
57+
panic(err)
58+
}
59+
defer resp.Body.Close()
60+
time.Sleep(1 * time.Second)
61+
verifier.WaitAndAssertTraces(func(stubs []tracetest.SpanStubs) {
62+
verifier.VerifyHttpClientAttributes(stubs[0][0], "GET", "GET", "http://127.0.0.1:"+strconv.Itoa(port)+"/a", "http", "1.1", "tcp", "ipv4", "", "127.0.0.1:"+strconv.Itoa(port), 200, 0, int64(port))
63+
verifier.VerifyHttpServerAttributes(stubs[0][1], "GET /a", "GET", "http", "tcp", "ipv4", "", "127.0.0.1:"+strconv.Itoa(port), "Go-http-client/1.1", "http", "/a", "", "/a", 200)
64+
verifier.VerifyHttpClientAttributes(stubs[0][2], "GET", "GET", "http://127.0.0.1:"+strconv.Itoa(port)+"/b", "http", "1.1", "tcp", "ipv4", "", "127.0.0.1:"+strconv.Itoa(port), 200, 0, int64(port))
65+
verifier.VerifyHttpServerAttributes(stubs[0][3], "GET /b", "GET", "http", "tcp", "ipv4", "", "127.0.0.1:"+strconv.Itoa(port), "Go-http-client/1.1", "http", "/b", "", "/b", 200)
66+
if stubs[0][1].Parent.TraceID().String() != stubs[0][0].SpanContext.TraceID().String() {
67+
log.Fatal("span 1 should be child of span 0")
68+
}
69+
if stubs[0][2].Parent.TraceID().String() != stubs[0][1].SpanContext.TraceID().String() {
70+
log.Fatal("span 2 should be child of span 1")
71+
}
72+
if stubs[0][3].Parent.TraceID().String() != stubs[0][2].SpanContext.TraceID().String() {
73+
log.Fatal("span 3 should be child of span 2")
74+
}
75+
76+
verifier.VerifyHttpClientAttributes(stubs[1][0], "POST", "POST", "http://127.0.0.1:"+strconv.Itoa(port)+"/a", "http", "1.1", "tcp", "ipv4", "", "127.0.0.1:"+strconv.Itoa(port), 200, 0, int64(port))
77+
verifier.VerifyHttpServerAttributes(stubs[1][1], "POST /a", "POST", "http", "tcp", "ipv4", "", "127.0.0.1:"+strconv.Itoa(port), "Go-http-client/1.1", "http", "/a", "", "/a", 200)
78+
verifier.VerifyHttpClientAttributes(stubs[1][2], "GET", "GET", "http://127.0.0.1:"+strconv.Itoa(port)+"/b", "http", "1.1", "tcp", "ipv4", "", "127.0.0.1:"+strconv.Itoa(port), 200, 0, int64(port))
79+
verifier.VerifyHttpServerAttributes(stubs[1][3], "GET /b", "GET", "http", "tcp", "ipv4", "", "127.0.0.1:"+strconv.Itoa(port), "Go-http-client/1.1", "http", "/b", "", "/b", 200)
80+
if stubs[1][1].Parent.TraceID().String() != stubs[1][0].SpanContext.TraceID().String() {
81+
log.Fatal("span 1 should be child of span 0")
82+
}
83+
if stubs[1][2].Parent.TraceID().String() != stubs[1][1].SpanContext.TraceID().String() {
84+
log.Fatal("span 2 should be child of span 1")
85+
}
86+
if stubs[1][3].Parent.TraceID().String() != stubs[1][2].SpanContext.TraceID().String() {
87+
log.Fatal("span 3 should be child of span 2")
88+
}
89+
}, 2)
90+
}

test/zerolog_tests.go

+43
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,43 @@
1+
// Copyright (c) 2024 Alibaba Group Holding Ltd.
2+
//
3+
// Licensed under the Apache License, Version 2.0 (the "License");
4+
// you may not use this file except in compliance with the License.
5+
// You may obtain a copy of the License at
6+
//
7+
// http://www.apache.org/licenses/LICENSE-2.0
8+
//
9+
// Unless required by applicable law or agreed to in writing, software
10+
// distributed under the License is distributed on an "AS IS" BASIS,
11+
// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
12+
// See the License for the specific language governing permissions and
13+
// limitations under the License.
14+
15+
package test
16+
17+
import (
18+
"bufio"
19+
"strings"
20+
"testing"
21+
)
22+
23+
func init() {
24+
TestCases = append(TestCases,
25+
NewGeneralTestCase("zerolog-test", "zerolog", "", "", "1.21", "", TestZeroLog),
26+
)
27+
}
28+
29+
func TestZeroLog(t *testing.T, env ...string) {
30+
UseApp("zerolog")
31+
RunGoBuild(t, "go", "build", "test_zerolog.go", "http_server.go")
32+
_, stderr := RunApp(t, "test_zerolog", env...)
33+
reader := strings.NewReader(stderr)
34+
scanner := bufio.NewScanner(reader)
35+
for scanner.Scan() {
36+
line := scanner.Text()
37+
if strings.Contains(line, "[test debugging]") {
38+
continue
39+
}
40+
ExpectContains(t, line, "trace_id")
41+
ExpectContains(t, line, "span_id")
42+
}
43+
}

0 commit comments

Comments
 (0)