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

[WIP]refactor:Soft connect logger to cwgo-pkg #59

Open
wants to merge 10 commits into
base: main
Choose a base branch
from
Open
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
26 changes: 15 additions & 11 deletions logging/logrus/go.mod
Original file line number Diff line number Diff line change
@@ -1,21 +1,25 @@
module github.com/hertz-contrib/obs-opentelemetry/logging/logrus

go 1.19
go 1.21

require (
github.com/cloudwego/hertz v0.9.0
github.com/hertz-contrib/logger/logrus v1.0.0
github.com/cloudwego-contrib/cwgo-pkg/telemetry/instrumentation/otellogrus v0.0.0-00010101000000-000000000000
github.com/cloudwego/hertz v0.9.2
github.com/sirupsen/logrus v1.9.3
go.opentelemetry.io/otel v1.19.0
go.opentelemetry.io/otel/exporters/stdout/stdouttrace v1.19.0
go.opentelemetry.io/otel/sdk v1.19.0
go.opentelemetry.io/otel/trace v1.19.0
go.opentelemetry.io/otel v1.28.0
go.opentelemetry.io/otel/exporters/stdout/stdouttrace v1.28.0
go.opentelemetry.io/otel/sdk v1.28.0
)

require (
github.com/go-logr/logr v1.3.0 // indirect
github.com/cloudwego-contrib/cwgo-pkg/log/logging/logrus v0.0.0-20241014044734-80a98dbe0b6a // indirect
github.com/cloudwego/kitex v0.11.3 // indirect
github.com/go-logr/logr v1.4.2 // indirect
github.com/go-logr/stdr v1.2.2 // indirect
github.com/google/go-cmp v0.6.0 // indirect
go.opentelemetry.io/otel/metric v1.19.0 // indirect
golang.org/x/sys v0.14.0 // indirect
github.com/google/uuid v1.6.0 // indirect
go.opentelemetry.io/otel/metric v1.28.0 // indirect
go.opentelemetry.io/otel/trace v1.28.0 // indirect
golang.org/x/sys v0.21.0 // indirect
)

replace github.com/cloudwego-contrib/cwgo-pkg/telemetry/instrumentation/otellogrus => github.com/smx-Morgan/cwgo-pkg/telemetry/instrumentation/otellogrus v0.0.0-20241019002536-84cf43046703
50 changes: 29 additions & 21 deletions logging/logrus/go.sum
Original file line number Diff line number Diff line change
Expand Up @@ -5,29 +5,34 @@ github.com/bytedance/sonic v1.5.0/go.mod h1:ED5hyg4y6t3/9Ku1R6dU/4KyJ48DZ4jPhfY1
github.com/bytedance/sonic v1.8.1/go.mod h1:i736AoUSYt75HyZLoJW9ERYxcy6eaN6h4BZXU064P/U=
github.com/chenzhuoyu/base64x v0.0.0-20211019084208-fb5309c8db06/go.mod h1:DH46F32mSOjUmXrMHnKwZdA8wcEefY7UVqBKYGjpdQY=
github.com/chenzhuoyu/base64x v0.0.0-20221115062448-fe3a3abad311/go.mod h1:b583jCggY9gE99b6G5LEC39OIiVsWj+R97kbl5odCEk=
github.com/cloudwego/hertz v0.9.0 h1:vmgSMSBx3qgB+ZnqbuEwfy+BFMS1cMr1ZSddif9zZ3A=
github.com/cloudwego/hertz v0.9.0/go.mod h1:WliNtVbwihWHHgAaIQEbVXl0O3aWj0ks1eoPrcEAnjs=
github.com/cloudwego/netpoll v0.5.0/go.mod h1:xVefXptcyheopwNDZjDPcfU6kIjZXZ4nY550k1yH9eQ=
github.com/cloudwego-contrib/cwgo-pkg/log/logging/logrus v0.0.0-20241014044734-80a98dbe0b6a h1:kpu/RTVoMi2VloWZPo1SDpY/DedkGfMvLmiaMt40RUI=
github.com/cloudwego-contrib/cwgo-pkg/log/logging/logrus v0.0.0-20241014044734-80a98dbe0b6a/go.mod h1:PCebhPHtSuvo3D7bligLXjdsbYZLsKqNicdd9bl033w=
github.com/cloudwego/hertz v0.9.2 h1:VbqddZ5RuvcgxzfxvXcmTiRisGYoo0+WnHGeDJKhjqI=
github.com/cloudwego/hertz v0.9.2/go.mod h1:cs8dH6unM4oaJ5k9m6pqbgLBPqakGWMG0+cthsxitsg=
github.com/cloudwego/kitex v0.11.3 h1:Qy1GtyuNbygMpwnMw+Aj1iS7fSd0IO7CzxtpZrRJ+Jc=
github.com/cloudwego/kitex v0.11.3/go.mod h1:RHT9ERKFVppJjBfGvwJAPxCIzf4oN1yASW5S4pPZNu4=
github.com/cloudwego/netpoll v0.6.0/go.mod h1:xVefXptcyheopwNDZjDPcfU6kIjZXZ4nY550k1yH9eQ=
github.com/davecgh/go-spew v1.1.0/go.mod h1:J7Y8YcW2NihsgmVo/mv3lAwl/skON4iLHjSsI+c5H38=
github.com/davecgh/go-spew v1.1.1 h1:vj9j/u1bqnvCEfJOwUhtlOARqs3+rkHYY13jYWTU97c=
github.com/davecgh/go-spew v1.1.1/go.mod h1:J7Y8YcW2NihsgmVo/mv3lAwl/skON4iLHjSsI+c5H38=
github.com/davecgh/go-spew v1.1.2-0.20180830191138-d8f796af33cc h1:U9qPSI2PIWSS1VwoXQT9A3Wy9MM3WgvqSxFWenqJduM=
github.com/davecgh/go-spew v1.1.2-0.20180830191138-d8f796af33cc/go.mod h1:J7Y8YcW2NihsgmVo/mv3lAwl/skON4iLHjSsI+c5H38=
github.com/fsnotify/fsnotify v1.5.4/go.mod h1:OVB6XrOHzAwXMpEM7uPOzcehqUV2UqJxmVXmkdnm1bU=
github.com/go-logr/logr v1.2.2/go.mod h1:jdQByPbusPIv2/zmleS9BjJVeZ6kBagPoEUsqbVz/1A=
github.com/go-logr/logr v1.3.0 h1:2y3SDp0ZXuc6/cjLSZ+Q3ir+QB9T/iG5yYRXqsagWSY=
github.com/go-logr/logr v1.3.0/go.mod h1:9T104GzyrTigFIr8wt5mBrctHMim0Nb2HLGrmQ40KvY=
github.com/go-logr/logr v1.4.2 h1:6pFjapn8bFcIbiKo3XT4j/BhANplGihG6tvd+8rYgrY=
github.com/go-logr/logr v1.4.2/go.mod h1:9T104GzyrTigFIr8wt5mBrctHMim0Nb2HLGrmQ40KvY=
github.com/go-logr/stdr v1.2.2 h1:hSWxHoqTgW2S2qGc0LTAI563KZ5YKYRhT3MFKZMbjag=
github.com/go-logr/stdr v1.2.2/go.mod h1:mMo/vtBO5dYbehREoey6XUKy/eSumjCCveDpRre4VKE=
github.com/golang/protobuf v1.3.2/go.mod h1:6lQm79b+lXiMfvg/cZm0SGofjICqVBUtrP5yJMmIC1U=
github.com/golang/protobuf v1.5.0/go.mod h1:FsONVRAS9T7sI+LIUmWTfcYkHO4aIWwzhcaSAoJOfIk=
github.com/google/go-cmp v0.5.5/go.mod h1:v8dTdLbMG2kIc/vJvl+f65V22dbkXbowE6jgT/gNBxE=
github.com/google/go-cmp v0.6.0 h1:ofyhxvXcZhMsU5ulbFiLKl/XBFqE1GSq7atu8tAmTRI=
github.com/google/go-cmp v0.6.0/go.mod h1:17dUlkBOakJ0+DkrSSNjCkIjxS6bF9zb3elmeNGIjoY=
github.com/google/uuid v1.6.0 h1:NIvaJDMOsjHA8n1jAhLSgzrAzy1Hgr+hNrb57e+94F0=
github.com/google/uuid v1.6.0/go.mod h1:TIyPZe4MgqvfeYDBFedMoGGpEw/LqOeaOT+nhxU+yHo=
github.com/gopherjs/gopherjs v0.0.0-20181017120253-0766667cb4d1/go.mod h1:wJfORRmW1u3UXTncJ5qlYoELFm8eSnnEO6hX4iZ3EWY=
github.com/henrylee2cn/ameda v1.4.8/go.mod h1:liZulR8DgHxdK+MEwvZIylGnmcjzQ6N6f2PlWe7nEO4=
github.com/henrylee2cn/ameda v1.4.10/go.mod h1:liZulR8DgHxdK+MEwvZIylGnmcjzQ6N6f2PlWe7nEO4=
github.com/henrylee2cn/goutil v0.0.0-20210127050712-89660552f6f8/go.mod h1:Nhe/DM3671a5udlv2AdV2ni/MZzgfv2qrPL5nIi3EGQ=
github.com/hertz-contrib/logger/logrus v1.0.0 h1:OeyYIGtMcn38W5e6XilogTVRIJVHbbcNC9Uk/mRHpgU=
github.com/hertz-contrib/logger/logrus v1.0.0/go.mod h1:SqDYLwVq5hTItYqimgZQbFCYPOIGNvBTq0Ip2OQwMcY=
github.com/jtolds/gls v4.20.0+incompatible/go.mod h1:QJZ7F/aHp+rZTRtaJ1ow/lLfFfVYBRgL+9YlvaHOwJU=
github.com/klauspost/cpuid/v2 v2.0.9/go.mod h1:FInQzS24/EEf25PyTYn52gqo7WaD8xa0213Md/qVLRg=
github.com/nyaruka/phonenumbers v1.0.55/go.mod h1:sDaTZ/KPX5f8qyV9qN+hIm+4ZBARJrupC6LuhshJq1U=
Expand All @@ -37,6 +42,8 @@ github.com/sirupsen/logrus v1.9.3 h1:dueUQJ1C2q9oE3F7wvmSGAaVtTmUizReu6fjN8uqzbQ
github.com/sirupsen/logrus v1.9.3/go.mod h1:naHLuLoDiP4jHNo9R0sCBMtWGeIprob74mVsIT4qYEQ=
github.com/smartystreets/assertions v0.0.0-20180927180507-b2de0cb4f26d/go.mod h1:OnSkiWE9lh6wB0YB77sQom3nweQdgAjqCqsofrRNTgc=
github.com/smartystreets/goconvey v1.6.4/go.mod h1:syvi0/a8iFYH4r/RixwvyeAJjdLS9QV7WQ/tjFTllLA=
github.com/smx-Morgan/cwgo-pkg/telemetry/instrumentation/otellogrus v0.0.0-20241019002536-84cf43046703 h1:fqlJfkXRTHIannk6477XM2ulsmeUIgMN97/ydeUjhNM=
github.com/smx-Morgan/cwgo-pkg/telemetry/instrumentation/otellogrus v0.0.0-20241019002536-84cf43046703/go.mod h1:voYFtO4TbKnfi27TydnQD5FikH5T+3TuFx79FIJIwVQ=
github.com/stretchr/objx v0.1.0/go.mod h1:HFkY916IF+rwdDfMAkV7OtwuqBVzrE8GR6GFx+wExME=
github.com/stretchr/objx v0.4.0/go.mod h1:YvHI0jy2hoMjB+UWwv71VJQ9isScKT/TqJzVSSt89Yw=
github.com/stretchr/objx v0.5.0/go.mod h1:Yh+to48EsGEfYuaHDzXPcE3xhTkx73EhmCGUpEOglKo=
Expand All @@ -46,22 +53,23 @@ github.com/stretchr/testify v1.7.0/go.mod h1:6Fq8oRcR53rry900zMqJjRRixrwX3KX962/
github.com/stretchr/testify v1.7.1/go.mod h1:6Fq8oRcR53rry900zMqJjRRixrwX3KX962/h/Wwjteg=
github.com/stretchr/testify v1.8.0/go.mod h1:yNjHg4UonilssWZ8iaSj1OCr/vHnekPRkoO+kdMU+MU=
github.com/stretchr/testify v1.8.1/go.mod h1:w2LPCIKwWwSfY2zedu0+kehJoqGctiVI29o6fzry7u4=
github.com/stretchr/testify v1.8.4 h1:CcVxjf3Q8PM0mHUKJCdn+eZZtm5yQwehR5yeSVQQcUk=
github.com/stretchr/testify v1.9.0 h1:HtqpIVDClZ4nwg75+f6Lvsy/wHu+3BoSGCbBAcpTsTg=
github.com/stretchr/testify v1.9.0/go.mod h1:r2ic/lqez/lEtzL7wO/rwa5dbSLXVDPFyf8C91i36aY=
github.com/tidwall/gjson v1.9.3/go.mod h1:/wbyibRr2FHMks5tjHJ5F8dMZh3AcwJEMf5vlfC0lxk=
github.com/tidwall/gjson v1.14.4/go.mod h1:/wbyibRr2FHMks5tjHJ5F8dMZh3AcwJEMf5vlfC0lxk=
github.com/tidwall/match v1.1.1/go.mod h1:eRSPERbgtNPcGhD8UCthc6PmLEQXEWd3PRB5JTxsfmM=
github.com/tidwall/pretty v1.2.0/go.mod h1:ITEVvHYasfjBbM0u2Pg8T2nJnzm8xPwvNhhsoaGGjNU=
github.com/twitchyliquid64/golang-asm v0.15.1/go.mod h1:a1lVb/DtPvCB8fslRZhAngC2+aY1QWCk3Cedj/Gdt08=
go.opentelemetry.io/otel v1.19.0 h1:MuS/TNf4/j4IXsZuJegVzI1cwut7Qc00344rgH7p8bs=
go.opentelemetry.io/otel v1.19.0/go.mod h1:i0QyjOq3UPoTzff0PJB2N66fb4S0+rSbSB15/oyH9fY=
go.opentelemetry.io/otel/exporters/stdout/stdouttrace v1.19.0 h1:Nw7Dv4lwvGrI68+wULbcq7su9K2cebeCUrDjVrUJHxM=
go.opentelemetry.io/otel/exporters/stdout/stdouttrace v1.19.0/go.mod h1:1MsF6Y7gTqosgoZvHlzcaaM8DIMNZgJh87ykokoNH7Y=
go.opentelemetry.io/otel/metric v1.19.0 h1:aTzpGtV0ar9wlV4Sna9sdJyII5jTVJEvKETPiOKwvpE=
go.opentelemetry.io/otel/metric v1.19.0/go.mod h1:L5rUsV9kM1IxCj1MmSdS+JQAcVm319EUrDVLrt7jqt8=
go.opentelemetry.io/otel/sdk v1.19.0 h1:6USY6zH+L8uMH8L3t1enZPR3WFEmSTADlqldyHtJi3o=
go.opentelemetry.io/otel/sdk v1.19.0/go.mod h1:NedEbbS4w3C6zElbLdPJKOpJQOrGUJ+GfzpjUvI0v1A=
go.opentelemetry.io/otel/trace v1.19.0 h1:DFVQmlVbfVeOuBRrwdtaehRrWiL1JoVs9CPIQ1Dzxpg=
go.opentelemetry.io/otel/trace v1.19.0/go.mod h1:mfaSyvGyEJEI0nyV2I4qhNQnbBOUUmYZpYojqMnX2vo=
go.opentelemetry.io/otel v1.28.0 h1:/SqNcYk+idO0CxKEUOtKQClMK/MimZihKYMruSMViUo=
go.opentelemetry.io/otel v1.28.0/go.mod h1:q68ijF8Fc8CnMHKyzqL6akLO46ePnjkgfIMIjUIX9z4=
go.opentelemetry.io/otel/exporters/stdout/stdouttrace v1.28.0 h1:EVSnY9JbEEW92bEkIYOVMw4q1WJxIAGoFTrtYOzWuRQ=
go.opentelemetry.io/otel/exporters/stdout/stdouttrace v1.28.0/go.mod h1:Ea1N1QQryNXpCD0I1fdLibBAIpQuBkznMmkdKrapk1Y=
go.opentelemetry.io/otel/metric v1.28.0 h1:f0HGvSl1KRAU1DLgLGFjrwVyismPlnuU6JD6bOeuA5Q=
go.opentelemetry.io/otel/metric v1.28.0/go.mod h1:Fb1eVBFZmLVTMb6PPohq3TO9IIhUisDsbJoL/+uQW4s=
go.opentelemetry.io/otel/sdk v1.28.0 h1:b9d7hIry8yZsgtbmM0DKyPWMMUMlK9NEKuIG4aBqWyE=
go.opentelemetry.io/otel/sdk v1.28.0/go.mod h1:oYj7ClPUA7Iw3m+r7GeEjz0qckQRJK2B8zjcZEfu7Pg=
go.opentelemetry.io/otel/trace v1.28.0 h1:GhQ9cUuQGmNDd5BTCP2dAvv75RdMxEfTmYejp+lkx9g=
go.opentelemetry.io/otel/trace v1.28.0/go.mod h1:jPyXzNPg6da9+38HEwElrQiHlVMTnVfM3/yv2OlIHaI=
golang.org/x/arch v0.0.0-20201008161808-52c3e6f60cff/go.mod h1:flIaEI6LNU6xOCD5PaJvn9wGP0agmIOqjrtsKGRguv4=
golang.org/x/arch v0.0.0-20210923205945-b76863e36670/go.mod h1:5om86z9Hs0C8fWVUuoMHwpExlXzs5Tkyp9hOrfG7pp8=
golang.org/x/crypto v0.0.0-20190308221718-c2843e01d9a2/go.mod h1:djNgcEr1/C05ACkg1iLfiJU5Ep61QUkGW8qpdssI0+w=
Expand All @@ -71,8 +79,8 @@ golang.org/x/sys v0.0.0-20190215142949-d0b11bdaac8a/go.mod h1:STP8DvDyc/dI5b8T5h
golang.org/x/sys v0.0.0-20220110181412-a018aaa089fe/go.mod h1:oPkhp1MJrh7nUepCBck5+mAzfO9JrbApNNgaTdGDITg=
golang.org/x/sys v0.0.0-20220412211240-33da011f77ad/go.mod h1:oPkhp1MJrh7nUepCBck5+mAzfO9JrbApNNgaTdGDITg=
golang.org/x/sys v0.0.0-20220715151400-c0bba94af5f8/go.mod h1:oPkhp1MJrh7nUepCBck5+mAzfO9JrbApNNgaTdGDITg=
golang.org/x/sys v0.14.0 h1:Vz7Qs629MkJkGyHxUlRHizWJRG2j8fbQKjELVSNhy7Q=
golang.org/x/sys v0.14.0/go.mod h1:/VUhepiaJMQUp4+oa/7Zr1D23ma6VTLIYjOOTFZPUcA=
golang.org/x/sys v0.21.0 h1:rF+pYz3DAGSQAxAu1CbC7catZg4ebC4UIeIhKxBZvws=
golang.org/x/sys v0.21.0/go.mod h1:/VUhepiaJMQUp4+oa/7Zr1D23ma6VTLIYjOOTFZPUcA=
golang.org/x/text v0.3.0/go.mod h1:NqM8EUOU14njkJ3fqMW+pc6Ldnwhi/IjpwHt7yyuwOQ=
golang.org/x/tools v0.0.0-20190328211700-ab21143f2384/go.mod h1:LCzVGOaR6xXOjkQ3onu1FJEFr0SW1gC7cKk1uF8kGRs=
golang.org/x/xerrors v0.0.0-20191204190536-9bdfabe68543/go.mod h1:I/5z698sn9Ka8TeJc9MKroUUfqBBauWjQqLJ2OPfmY0=
Expand Down
48 changes: 8 additions & 40 deletions logging/logrus/hook.go
Original file line number Diff line number Diff line change
Expand Up @@ -15,19 +15,12 @@
package logrus

import (
"errors"
"github.com/cloudwego-contrib/cwgo-pkg/telemetry/instrumentation/otellogrus"

"github.com/sirupsen/logrus"
"go.opentelemetry.io/otel/codes"
"go.opentelemetry.io/otel/trace"
)

// Ref to https://github.com/open-telemetry/opentelemetry-specification/blob/main/specification/logs/overview.md#json-formats
const (
traceIDKey = "trace_id"
spanIDKey = "span_id"
traceFlagsKey = "trace_flags"
)

var _ logrus.Hook = (*TraceHook)(nil)

Expand All @@ -40,48 +33,23 @@ type TraceHookConfig struct {

// TraceHook trace hook
type TraceHook struct {
cfg *TraceHookConfig
hook otellogrus.TraceHook
}

// NewTraceHook create trace hook
func NewTraceHook(cfg *TraceHookConfig) *TraceHook {
return &TraceHook{cfg: cfg}
return &TraceHook{hook: *otellogrus.NewTraceHook(otellogrus.NewTraceHookConfig(
cfg.recordStackTraceInSpan,
cfg.enableLevels,
cfg.errorSpanLevel))}
}

// Levels get levels
func (h *TraceHook) Levels() []logrus.Level {
return h.cfg.enableLevels
return h.hook.Levels()
}

// Fire logrus hook fire
func (h *TraceHook) Fire(entry *logrus.Entry) error {
if entry.Context == nil {
return nil
}

span := trace.SpanFromContext(entry.Context)

// check span context
spanContext := span.SpanContext()
if !spanContext.IsValid() {
return nil
}

// attach span context to log entry data fields
entry.Data[traceIDKey] = spanContext.TraceID()
entry.Data[spanIDKey] = spanContext.SpanID()
entry.Data[traceFlagsKey] = spanContext.TraceFlags()

// non recording spans do not support modifying
if !span.IsRecording() {
return nil
}

// set span status
if entry.Level <= h.cfg.errorSpanLevel {
span.SetStatus(codes.Error, "")
span.RecordError(errors.New(entry.Message), trace.WithStackTrace(h.cfg.recordStackTraceInSpan))
}

return nil
return h.hook.Fire(entry)
}
23 changes: 3 additions & 20 deletions logging/logrus/logger.go
Original file line number Diff line number Diff line change
Expand Up @@ -15,30 +15,13 @@
package logrus

import (
hertzlogrus "github.com/hertz-contrib/logger/logrus"
"github.com/cloudwego-contrib/cwgo-pkg/telemetry/instrumentation/otellogrus"
)

// Logger an alias to github.com/hertz-contrib/logger/logrus Logger
type Logger = hertzlogrus.Logger
type Logger = otellogrus.Logger

// NewLogger create logger with otel hook
func NewLogger(opts ...Option) *Logger {
cfg := defaultConfig()

// apply options
for _, opt := range opts {
opt.apply(cfg)
}

// default trace hooks
cfg.hooks = append(cfg.hooks, NewTraceHook(cfg.traceHookConfig))

// attach hook
for _, hook := range cfg.hooks {
cfg.logger.AddHook(hook)
}

return hertzlogrus.NewLogger(
hertzlogrus.WithLogger(cfg.logger),
)
return otellogrus.NewLogger(opts...)
}
62 changes: 11 additions & 51 deletions logging/logrus/option.go
Original file line number Diff line number Diff line change
Expand Up @@ -15,82 +15,42 @@
package logrus

import (
"github.com/cloudwego-contrib/cwgo-pkg/telemetry/instrumentation/otellogrus"
"github.com/sirupsen/logrus"
)

// Option logrus hook option
type Option interface {
apply(cfg *config)
}

type option func(cfg *config)

func (fn option) apply(cfg *config) {
fn(cfg)
}

type config struct {
logger *logrus.Logger
hooks []logrus.Hook

traceHookConfig *TraceHookConfig
}

func defaultConfig() *config {
// std logger
stdLogger := logrus.StandardLogger()
// default json format
stdLogger.SetFormatter(new(logrus.JSONFormatter))

return &config{
logger: logrus.StandardLogger(),
hooks: []logrus.Hook{},
traceHookConfig: &TraceHookConfig{
recordStackTraceInSpan: true,
enableLevels: logrus.AllLevels,
errorSpanLevel: logrus.ErrorLevel,
},
}
}
type Option = otellogrus.Option

// WithLogger configures logger
func WithLogger(logger *logrus.Logger) Option {
return option(func(cfg *config) {
cfg.logger = logger
})
return otellogrus.WithLogger(logger)
}

// WithHook configures hook
func WithHook(hook logrus.Hook) Option {
return option(func(cfg *config) {
cfg.hooks = append(cfg.hooks, hook)
})
return otellogrus.WithHook(hook)
}

// WithTraceHookConfig configures trace hook config
func WithTraceHookConfig(hookConfig *TraceHookConfig) Option {
return option(func(cfg *config) {
cfg.traceHookConfig = hookConfig
})
return otellogrus.WithTraceHookConfig(otellogrus.NewTraceHookConfig(
hookConfig.recordStackTraceInSpan,
hookConfig.enableLevels,
hookConfig.errorSpanLevel))
}

// WithTraceHookLevels configures hook levels
func WithTraceHookLevels(levels []logrus.Level) Option {
return option(func(cfg *config) {
cfg.traceHookConfig.enableLevels = levels
})
return otellogrus.WithTraceHookLevels(levels)
}

// WithTraceHookErrorSpanLevel configures trace hook error span level
func WithTraceHookErrorSpanLevel(level logrus.Level) Option {
return option(func(cfg *config) {
cfg.traceHookConfig.errorSpanLevel = level
})
return otellogrus.WithTraceHookErrorSpanLevel(level)
}

// WithRecordStackTraceInSpan configures whether record stack trace in span
func WithRecordStackTraceInSpan(recordStackTraceInSpan bool) Option {
return option(func(cfg *config) {
cfg.traceHookConfig.recordStackTraceInSpan = recordStackTraceInSpan
})
return otellogrus.WithRecordStackTraceInSpan(recordStackTraceInSpan)
}
33 changes: 17 additions & 16 deletions logging/zap/go.mod
Original file line number Diff line number Diff line change
@@ -1,29 +1,30 @@
module github.com/hertz-contrib/obs-opentelemetry/logging/zap

go 1.19
go 1.21

require (
github.com/cloudwego/hertz v0.9.0
github.com/hertz-contrib/logger/zap v1.1.0
github.com/stretchr/testify v1.8.4
go.opentelemetry.io/otel v1.19.0
go.opentelemetry.io/otel/exporters/stdout/stdouttrace v1.19.0
go.opentelemetry.io/otel/sdk v1.19.0
go.opentelemetry.io/otel/trace v1.19.0
github.com/cloudwego-contrib/cwgo-pkg/log/logging/zap v0.0.0-20241014044734-80a98dbe0b6a
github.com/cloudwego-contrib/cwgo-pkg/telemetry/instrumentation/otelzap v0.0.0-00010101000000-000000000000
github.com/cloudwego/hertz v0.9.2
github.com/stretchr/testify v1.9.0
go.opentelemetry.io/otel v1.28.0
go.opentelemetry.io/otel/exporters/stdout/stdouttrace v1.28.0
go.opentelemetry.io/otel/sdk v1.28.0
go.uber.org/zap v1.27.0
)

require (
github.com/davecgh/go-spew v1.1.1 // indirect
github.com/go-logr/logr v1.3.0 // indirect
github.com/cloudwego/kitex v0.11.3 // indirect
github.com/davecgh/go-spew v1.1.2-0.20180830191138-d8f796af33cc // indirect
github.com/go-logr/logr v1.4.2 // indirect
github.com/go-logr/stdr v1.2.2 // indirect
github.com/google/go-cmp v0.6.0 // indirect
github.com/kr/pretty v0.3.1 // indirect
github.com/google/uuid v1.6.0 // indirect
github.com/pmezard/go-difflib v1.0.0 // indirect
github.com/rogpeppe/go-internal v1.10.0 // indirect
go.opentelemetry.io/otel/metric v1.19.0 // indirect
go.opentelemetry.io/otel/metric v1.28.0 // indirect
go.opentelemetry.io/otel/trace v1.28.0 // indirect
go.uber.org/multierr v1.11.0 // indirect
golang.org/x/sys v0.14.0 // indirect
gopkg.in/check.v1 v1.0.0-20201130134442-10cb98267c6c // indirect
golang.org/x/sys v0.21.0 // indirect
gopkg.in/yaml.v3 v3.0.1 // indirect
)

replace github.com/cloudwego-contrib/cwgo-pkg/telemetry/instrumentation/otelzap => github.com/smx-Morgan/cwgo-pkg/telemetry/instrumentation/otelzap v0.0.0-20241019002536-84cf43046703
Loading
Loading