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

support zerolog #260

Merged
merged 7 commits into from
Dec 23, 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
5 changes: 3 additions & 2 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -82,7 +82,9 @@ Also there are several [**documents**](./docs) that you may find useful for eith
| echo | https://github.com/labstack/echo | v4.0.0 | v4.12.0 |
| elasticsearch | https://github.com/elastic/go-elasticsearch| v8.4.0 | v8.15.0 |
| fasthttp | https://github.com/valyala/fasthttp | v1.45.0 | v1.57.0 |
| fiber | https://github.com/gofiber/fiber | v2.43.0 | v2.52.5 |
| gin | https://github.com/gin-gonic/gin | v1.7.0 | v1.10.0 |
| gorestful | https://github.com/emicklei/go-restful | v3.7.0 | v3.12.1 |
| go-redis | https://github.com/redis/go-redis | v9.0.5 | v9.5.1 |
| go-redis v8 | https://github.com/redis/go-redis | v8.11.0 | v8.11.5 |
| gorm | https://github.com/go-gorm/gorm | v1.22.0 | v1.25.9 |
Expand All @@ -98,8 +100,7 @@ Also there are several [**documents**](./docs) that you may find useful for eith
| redigo | https://github.com/gomodule/redigo | v1.9.0 | v1.9.2 |
| slog | https://pkg.go.dev/log/slog | - | - |
| zap | https://github.com/uber-go/zap | v1.20.0 | v1.27.0 |
| fiber | https://github.com/gofiber/fiber | v2.43.0 | v2.52.5 |
| gorestful | https://github.com/emicklei/go-restful | v3.7.0 | v3.12.1 |
| zerolog | https://github.com/rs/zerolog | v1.10.0 | v1.33.0 |

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

Expand Down
5 changes: 3 additions & 2 deletions docs/supported-libraries.md
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,9 @@
| echo | https://github.com/labstack/echo | v4.0.0 | v4.12.0 |
| elasticsearch| https://github.com/elastic/go-elasticsearch| v8.4.0 | v8.15.0 |
| fasthttp | https://github.com/valyala/fasthttp | v1.45.0 | v1.57.0 |
| fiber | https://github.com/gofiber/fiber | v2.43.0 | v2.52.5 |
| gin | https://github.com/gin-gonic/gin | v1.7.0 | v1.10.0 |
| gorestful | https://github.com/emicklei/go-restful | v3.7.0 | v3.12.1 |
| go-redis | https://github.com/redis/go-redis | v9.0.5 | v9.5.1 |
| go-redis v8 | https://github.com/redis/go-redis | v8.11.0 | v8.11.5 |
| gorm | https://github.com/go-gorm/gorm | v1.22.0 | v1.25.9 |
Expand All @@ -22,5 +24,4 @@
| redigo | https://github.com/gomodule/redigo | v1.9.0 | v1.9.2 |
| slog | https://pkg.go.dev/log/slog | - | - |
| zap | https://github.com/uber-go/zap | v1.20.0 | v1.27.0 |
| fiber | https://github.com/gofiber/fiber | v2.43.0 | v2.52.5 |

| zerolog | https://github.com/rs/zerolog | v1.10.0 | v1.33.0 |
1 change: 1 addition & 0 deletions go.mod
Original file line number Diff line number Diff line change
Expand Up @@ -25,6 +25,7 @@ require (
github.com/labstack/echo/v4 v4.12.0
github.com/prometheus/client_golang v1.16.0
github.com/redis/go-redis/v9 v9.6.1
github.com/rs/zerolog v1.10.0
github.com/sirupsen/logrus v1.9.3
github.com/testcontainers/testcontainers-go v0.34.0
github.com/testcontainers/testcontainers-go/modules/mysql v0.34.0
Expand Down
2 changes: 2 additions & 0 deletions go.sum
Original file line number Diff line number Diff line change
Expand Up @@ -352,6 +352,8 @@ github.com/rivo/uniseg v0.2.0/go.mod h1:J6wj4VEh+S6ZtnVlnTBMWIodfgj8LQOQFoIToxlJ
github.com/rogpeppe/go-internal v1.3.0/go.mod h1:M8bDsm7K2OlrFYOpmOWEs/qY81heoFRclV5y23lUDJ4=
github.com/rogpeppe/go-internal v1.13.1 h1:KvO1DLK/DRN07sQ1LQKScxyZJuNnedQ5/wKSR38lUII=
github.com/rogpeppe/go-internal v1.13.1/go.mod h1:uMEvuHeurkdAXX61udpOXGD/AzZDWNMNyH2VO9fmH0o=
github.com/rs/zerolog v1.10.0 h1:roFDW4AgYGbHnTOAMZ2K8mHJZ/7bSj7txPfvbABIj88=
github.com/rs/zerolog v1.10.0/go.mod h1:YbFCdg8HfsridGWAh22vktObvhZbQsZXe4/zB0OKkWU=
github.com/ruudk/golang-pdf417 v0.0.0-20181029194003-1af4ab5afa58/go.mod h1:6lfFZQK844Gfx8o5WFuvpxWRwnSoipWe/p622j1v06w=
github.com/ruudk/golang-pdf417 v0.0.0-20201230142125-a7e3863a1245/go.mod h1:pQAZKsJ8yyVxGRWYNEm9oFB8ieLgKFnamEyDmSA0BRk=
github.com/sergi/go-diff v1.2.0 h1:XU+rvMAioB0UC3q1MFrIQy4Vo5/4VsRDQQXHsEya6xQ=
Expand Down
8 changes: 8 additions & 0 deletions pkg/data/default.json
Original file line number Diff line number Diff line change
Expand Up @@ -611,6 +611,14 @@
"OnExit": "restContainerHandleOnExit",
"Path": "github.com/alibaba/opentelemetry-go-auto-instrumentation/pkg/rules/gorestful"
},
{
"Version": "[1.10.0,1.33.1)",
"ImportPath": "github.com/rs/zerolog",
"Function": "msg",
"ReceiverType": "*Event",
"OnEnter": "zeroLogWriteOnEnter",
"Path": "github.com/alibaba/opentelemetry-go-auto-instrumentation/pkg/rules/zerolog"
},
{
"Version": "[0.5.1,0.11.4)",
"ImportPath": "github.com/cloudwego/kitex/client",
Expand Down
36 changes: 36 additions & 0 deletions pkg/rules/zerolog/setup.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,36 @@
// Copyright (c) 2024 Alibaba Group Holding Ltd.
//
// Licensed under the Apache License, Version 2.0 (the "License");
// you may not use this file except in compliance with the License.
// You may obtain a copy of the License at
//
// http://www.apache.org/licenses/LICENSE-2.0
//
// Unless required by applicable law or agreed to in writing, software
// distributed under the License is distributed on an "AS IS" BASIS,
// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
// See the License for the specific language governing permissions and
// limitations under the License.

package zerolog

import (
"github.com/alibaba/opentelemetry-go-auto-instrumentation/pkg/api"
"github.com/alibaba/opentelemetry-go-auto-instrumentation/pkg/inst-api/instrumenter"
"github.com/rs/zerolog"
"go.opentelemetry.io/otel/sdk/trace"
)

var zeroLogEnabler = instrumenter.NewDefaultInstrumentEnabler()

func zeroLogWriteOnEnter(call api.CallContext, ce *zerolog.Event, msg string) {
if !zeroLogEnabler.Enable() {
return
}
traceId, spanId := trace.GetTraceAndSpanId()
if traceId != "" && spanId != "" {
cer := ce.Str("trace_id", traceId).Str("span_id", spanId)
call.SetParam(0, cer)
}
return
}
2 changes: 2 additions & 0 deletions test/helloworld/go.mod
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,8 @@ module helloworld

go 1.22

toolchain go1.22.7

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

replace github.com/alibaba/opentelemetry-go-auto-instrumentation/test/verifier => ../../../opentelemetry-go-auto-instrumentation/test/verifier
Expand Down
27 changes: 27 additions & 0 deletions test/zerolog/go.mod
Original file line number Diff line number Diff line change
@@ -0,0 +1,27 @@
module zerolog

go 1.22

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

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

require (
github.com/alibaba/opentelemetry-go-auto-instrumentation/test/verifier v0.0.0-00010101000000-000000000000
go.opentelemetry.io/otel/sdk v1.31.0
github.com/rs/zerolog v1.10.0
)

require (
github.com/go-logr/logr v1.4.2 // indirect
github.com/go-logr/stdr v1.2.2 // indirect
github.com/google/uuid v1.6.0 // indirect
github.com/mohae/deepcopy v0.0.0-20170929034955-c48cc78d4826 // indirect
go.opentelemetry.io/otel v1.31.0 // indirect
go.opentelemetry.io/otel/metric v1.31.0 // indirect
go.opentelemetry.io/otel/sdk/metric v1.30.0 // indirect
go.opentelemetry.io/otel/trace v1.31.0 // indirect
go.uber.org/atomic v1.7.0 // indirect
go.uber.org/multierr v1.6.0 // indirect
golang.org/x/sys v0.26.0 // indirect
)
54 changes: 54 additions & 0 deletions test/zerolog/http_server.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,54 @@
// Copyright (c) 2024 Alibaba Group Holding Ltd.
//
// Licensed under the Apache License, Version 2.0 (the "License");
// you may not use this file except in compliance with the License.
// You may obtain a copy of the License at
//
// http://www.apache.org/licenses/LICENSE-2.0
//
// Unless required by applicable law or agreed to in writing, software
// distributed under the License is distributed on an "AS IS" BASIS,
// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
// See the License for the specific language governing permissions and
// limitations under the License.

package main

import (
"github.com/rs/zerolog"
"io"
"net/http"
"os"
"strconv"
)

var logger = zerolog.New(os.Stdout).With().
Timestamp().
Str("role", "my-service").
Str("host", "127.0.0.1").
Logger()

var port int

func redirectHandler(w http.ResponseWriter, r *http.Request) {
resp, err := http.Get("http://127.0.0.1:" + strconv.Itoa(port) + "/b")
if err != nil {
logger.Printf("request provider error: %v", err)
w.WriteHeader(http.StatusInternalServerError)
return
}
defer resp.Body.Close()
_, err = io.ReadAll(resp.Body)
if err != nil {
logger.Print(err)
w.WriteHeader(http.StatusInternalServerError)
return
}
logger.Print("abcde")
_, _ = w.Write([]byte("success"))
}

func helloHandler(w http.ResponseWriter, r *http.Request) {
w.WriteHeader(http.StatusOK)
_, _ = w.Write([]byte("success"))
}
90 changes: 90 additions & 0 deletions test/zerolog/test_zerolog.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,90 @@
// Copyright (c) 2024 Alibaba Group Holding Ltd.
//
// Licensed under the Apache License, Version 2.0 (the "License");
// you may not use this file except in compliance with the License.
// You may obtain a copy of the License at
//
// http://www.apache.org/licenses/LICENSE-2.0
//
// Unless required by applicable law or agreed to in writing, software
// distributed under the License is distributed on an "AS IS" BASIS,
// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
// See the License for the specific language governing permissions and
// limitations under the License.

package main

import (
"bytes"
"github.com/alibaba/opentelemetry-go-auto-instrumentation/test/verifier"
"go.opentelemetry.io/otel/sdk/trace/tracetest"
"log"
"net/http"
"strconv"
"time"
)

func setupBasicHttp() {
http.HandleFunc("/a", redirectHandler)
http.HandleFunc("/b", helloHandler)
var err error
port, err = verifier.GetFreePort()
if err != nil {
panic(err)
}
err = http.ListenAndServe(":"+strconv.Itoa(port), nil)
if err != nil {
panic(err)
}
}

func main() {
go setupBasicHttp()
time.Sleep(1 * time.Second)
_, err := http.Get("http://127.0.0.1:" + strconv.Itoa(port) + "/a")
if err != nil {
panic(err)
}
jsonData := []byte(`{"key1": "value1", "key2": "value2"}`)
req, err := http.NewRequest("POST", "http://127.0.0.1:"+strconv.Itoa(port)+"/a", bytes.NewBuffer(jsonData))
if err != nil {
panic(err)
}
req.Header.Set("Content-Type", "application/json")
client := http.Client{}
resp, err := client.Do(req)
if err != nil {
panic(err)
}
defer resp.Body.Close()
time.Sleep(1 * time.Second)
verifier.WaitAndAssertTraces(func(stubs []tracetest.SpanStubs) {
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))
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)
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))
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)
if stubs[0][1].Parent.TraceID().String() != stubs[0][0].SpanContext.TraceID().String() {
log.Fatal("span 1 should be child of span 0")
}
if stubs[0][2].Parent.TraceID().String() != stubs[0][1].SpanContext.TraceID().String() {
log.Fatal("span 2 should be child of span 1")
}
if stubs[0][3].Parent.TraceID().String() != stubs[0][2].SpanContext.TraceID().String() {
log.Fatal("span 3 should be child of span 2")
}

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))
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)
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))
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)
if stubs[1][1].Parent.TraceID().String() != stubs[1][0].SpanContext.TraceID().String() {
log.Fatal("span 1 should be child of span 0")
}
if stubs[1][2].Parent.TraceID().String() != stubs[1][1].SpanContext.TraceID().String() {
log.Fatal("span 2 should be child of span 1")
}
if stubs[1][3].Parent.TraceID().String() != stubs[1][2].SpanContext.TraceID().String() {
log.Fatal("span 3 should be child of span 2")
}
}, 2)
}
43 changes: 43 additions & 0 deletions test/zerolog_tests.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,43 @@
// Copyright (c) 2024 Alibaba Group Holding Ltd.
//
// Licensed under the Apache License, Version 2.0 (the "License");
// you may not use this file except in compliance with the License.
// You may obtain a copy of the License at
//
// http://www.apache.org/licenses/LICENSE-2.0
//
// Unless required by applicable law or agreed to in writing, software
// distributed under the License is distributed on an "AS IS" BASIS,
// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
// See the License for the specific language governing permissions and
// limitations under the License.

package test

import (
"bufio"
"strings"
"testing"
)

func init() {
TestCases = append(TestCases,
NewGeneralTestCase("zerolog-test", "zerolog", "", "", "1.21", "", TestZeroLog),
)
}

func TestZeroLog(t *testing.T, env ...string) {
UseApp("zerolog")
RunGoBuild(t, "go", "build", "test_zerolog.go", "http_server.go")
_, stderr := RunApp(t, "test_zerolog", env...)
reader := strings.NewReader(stderr)
scanner := bufio.NewScanner(reader)
for scanner.Scan() {
line := scanner.Text()
if strings.Contains(line, "[test debugging]") {
continue
}
ExpectContains(t, line, "trace_id")
ExpectContains(t, line, "span_id")
}
}
Loading