Skip to content

Commit 352e01e

Browse files
committed
fixes
1 parent 5b020f5 commit 352e01e

File tree

7 files changed

+262
-154
lines changed

7 files changed

+262
-154
lines changed

otelprovider/go.sum

Lines changed: 0 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -19,8 +19,6 @@ github.com/grpc-ecosystem/grpc-gateway/v2 v2.26.1 h1:e9Rjr40Z98/clHv5Yg79Is0Ntos
1919
github.com/grpc-ecosystem/grpc-gateway/v2 v2.26.1/go.mod h1:tIxuGz/9mpox++sgp9fJjHO0+q1X9/UOWd798aAm22M=
2020
github.com/pmezard/go-difflib v1.0.0 h1:4DBwDE0NGyQoBHbLQYPwSUPoCMWR5BEzIk/f1lZbAQM=
2121
github.com/pmezard/go-difflib v1.0.0/go.mod h1:iKH77koFhYxTK1pcRnkKkqfTogsbg7gZNVY4sRDYZ/4=
22-
github.com/spechtlabs/go-otel-utils/otelzap v0.0.3 h1:DJxv/e5sb1QhzYvLdq0bHVwHseT01tkchH6xZefsGaQ=
23-
github.com/spechtlabs/go-otel-utils/otelzap v0.0.3/go.mod h1:ZsbZlzbYMBgPoqlr/pDHUUHnjrGDTHkIKaCiYZ7zZnc=
2422
github.com/spechtlabs/go-otel-utils/otelzap v0.0.6 h1:nCSqGF0phhIZCtF53YUgwfGVUhkFvyZv42xztioNSyw=
2523
github.com/spechtlabs/go-otel-utils/otelzap v0.0.6/go.mod h1:wYDKgGeOKyK5PVB+tfvkZovHm9VP6zEn+osJdKmepX8=
2624
github.com/stretchr/testify v1.10.0 h1:Xv5erBjTwe/5IxqUQTdXv5kgmIvbHo3QQyRwhJsOfJA=

otelzap/go.mod

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -6,6 +6,7 @@ require (
66
github.com/aws/smithy-go v1.22.3
77
github.com/sierrasoftworks/humane-errors-go v0.0.0-20241125132722-d032d7dd359e
88
github.com/spechtlabs/go-otel-utils/otelprovider v0.0.8
9+
github.com/stretchr/testify v1.10.0
910
go.opentelemetry.io/otel v1.35.0
1011
go.opentelemetry.io/otel/log v0.11.0
1112
go.opentelemetry.io/otel/trace v1.35.0
@@ -14,10 +15,12 @@ require (
1415

1516
require (
1617
github.com/cenkalti/backoff/v4 v4.3.0 // indirect
18+
github.com/davecgh/go-spew v1.1.1 // indirect
1719
github.com/go-logr/logr v1.4.2 // indirect
1820
github.com/go-logr/stdr v1.2.2 // indirect
1921
github.com/google/uuid v1.6.0 // indirect
2022
github.com/grpc-ecosystem/grpc-gateway/v2 v2.26.1 // indirect
23+
github.com/pmezard/go-difflib v1.0.0 // indirect
2124
go.opentelemetry.io/auto/sdk v1.1.0 // indirect
2225
go.opentelemetry.io/otel/exporters/otlp/otlplog/otlploggrpc v0.11.0 // indirect
2326
go.opentelemetry.io/otel/exporters/otlp/otlplog/otlploghttp v0.11.0 // indirect
@@ -36,4 +39,5 @@ require (
3639
google.golang.org/genproto/googleapis/rpc v0.0.0-20250218202821-56aae31c358a // indirect
3740
google.golang.org/grpc v1.71.0 // indirect
3841
google.golang.org/protobuf v1.36.5 // indirect
42+
gopkg.in/yaml.v3 v3.0.1 // indirect
3943
)

otelzap/go.sum

Lines changed: 9 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -17,14 +17,16 @@ github.com/google/uuid v1.6.0 h1:NIvaJDMOsjHA8n1jAhLSgzrAzy1Hgr+hNrb57e+94F0=
1717
github.com/google/uuid v1.6.0/go.mod h1:TIyPZe4MgqvfeYDBFedMoGGpEw/LqOeaOT+nhxU+yHo=
1818
github.com/grpc-ecosystem/grpc-gateway/v2 v2.26.1 h1:e9Rjr40Z98/clHv5Yg79Is0NtosR5LXRvdr7o/6NwbA=
1919
github.com/grpc-ecosystem/grpc-gateway/v2 v2.26.1/go.mod h1:tIxuGz/9mpox++sgp9fJjHO0+q1X9/UOWd798aAm22M=
20+
github.com/kr/pretty v0.3.1 h1:flRD4NNwYAUpkphVc1HcthR4KEIFJ65n8Mw5qdRn3LE=
21+
github.com/kr/pretty v0.3.1/go.mod h1:hoEshYVHaxMs3cyo3Yncou5ZscifuDolrwPKZanG3xk=
22+
github.com/kr/text v0.2.0 h1:5Nx0Ya0ZqY2ygV366QzturHI13Jq95ApcVaJBhpS+AY=
23+
github.com/kr/text v0.2.0/go.mod h1:eLer722TekiGuMkidMxC/pM04lWEeraHUUmBw8l2grE=
2024
github.com/pmezard/go-difflib v1.0.0 h1:4DBwDE0NGyQoBHbLQYPwSUPoCMWR5BEzIk/f1lZbAQM=
2125
github.com/pmezard/go-difflib v1.0.0/go.mod h1:iKH77koFhYxTK1pcRnkKkqfTogsbg7gZNVY4sRDYZ/4=
26+
github.com/rogpeppe/go-internal v1.13.1 h1:KvO1DLK/DRN07sQ1LQKScxyZJuNnedQ5/wKSR38lUII=
27+
github.com/rogpeppe/go-internal v1.13.1/go.mod h1:uMEvuHeurkdAXX61udpOXGD/AzZDWNMNyH2VO9fmH0o=
2228
github.com/sierrasoftworks/humane-errors-go v0.0.0-20241125132722-d032d7dd359e h1:TRjhbclA0br24WOCSltVovqcwOLTKRLRzQmuSBY5mQc=
2329
github.com/sierrasoftworks/humane-errors-go v0.0.0-20241125132722-d032d7dd359e/go.mod h1:j7jIbd4vmh7c8kUuM4wNOqqEaMcG+UWVyem6d26pai4=
24-
github.com/spechtlabs/go-otel-utils/otelprovider v0.0.3 h1:4oOesPI0xzLxlXkUJXbFWTU11sixh7fN8w9MYji89Jc=
25-
github.com/spechtlabs/go-otel-utils/otelprovider v0.0.3/go.mod h1:g4aR+dN5kWbCCOQW8VuO6XV/uN+lGSwi59Dk7fRtchk=
26-
github.com/spechtlabs/go-otel-utils/otelprovider v0.0.6 h1:ibQrJxWzcF0Ql9RyON9eDJzOVcHl/ImZ4lolXRXYn+s=
27-
github.com/spechtlabs/go-otel-utils/otelprovider v0.0.6/go.mod h1:KYCRZw2eZJIUBd66Q0peZy3UwX+OBWEAN8mEIU9Xguo=
2830
github.com/spechtlabs/go-otel-utils/otelprovider v0.0.8 h1:QE5n0ReT+dpMGyJH1z8RD5GrzkxF3ZsYwNcrUJw6nPA=
2931
github.com/spechtlabs/go-otel-utils/otelprovider v0.0.8/go.mod h1:eXwaYnplhrfg/WXkIQ8ziC68Xa4wR7C0RilMBVpU9nM=
3032
github.com/stretchr/testify v1.10.0 h1:Xv5erBjTwe/5IxqUQTdXv5kgmIvbHo3QQyRwhJsOfJA=
@@ -77,5 +79,8 @@ google.golang.org/grpc v1.71.0 h1:kF77BGdPTQ4/JZWMlb9VpJ5pa25aqvVqogsxNHHdeBg=
7779
google.golang.org/grpc v1.71.0/go.mod h1:H0GRtasmQOh9LkFoCPDu3ZrwUtD1YGE+b2vYBYd/8Ec=
7880
google.golang.org/protobuf v1.36.5 h1:tPhr+woSbjfYvY6/GPufUoYizxw1cF/yFoxJ2fmpwlM=
7981
google.golang.org/protobuf v1.36.5/go.mod h1:9fA7Ob0pmnwhb644+1+CVWFRbNajQ6iRojtC/QF5bRE=
82+
gopkg.in/check.v1 v0.0.0-20161208181325-20d25e280405/go.mod h1:Co6ibVJAznAaIkqp8huTwlJQCZ016jof/cbN4VW5Yz0=
83+
gopkg.in/check.v1 v1.0.0-20201130134442-10cb98267c6c h1:Hei/4ADfdWqJk1ZMxUNpqntNwaWcugrBjAiHlqqRiVk=
84+
gopkg.in/check.v1 v1.0.0-20201130134442-10cb98267c6c/go.mod h1:JHkPIbrfpd72SG/EVd6muEfDQjcINNoR0C8j2r3qZ4Q=
8085
gopkg.in/yaml.v3 v3.0.1 h1:fxVm/GzAzEWqLHuvctI91KS9hhNmmWOoWu0XTYJS7CA=
8186
gopkg.in/yaml.v3 v3.0.1/go.mod h1:K4uyk7z7BCEPqu6E+C64Yfv1cQ7kz7rIZviUmN+EgEM=

otelzap/logger.go

Lines changed: 96 additions & 100 deletions
Original file line numberDiff line numberDiff line change
@@ -4,14 +4,11 @@ import (
44
"context"
55
"errors"
66
"fmt"
7-
"runtime"
87

98
"github.com/aws/smithy-go/logging"
109
"github.com/sierrasoftworks/humane-errors-go"
11-
"go.opentelemetry.io/otel/codes"
1210
"go.opentelemetry.io/otel/log"
1311
"go.opentelemetry.io/otel/log/global"
14-
"go.opentelemetry.io/otel/trace"
1512
"go.uber.org/zap"
1613
"go.uber.org/zap/zapcore"
1714
)
@@ -95,42 +92,28 @@ func (l *Logger) WithOptions(opts ...zap.Option) *Logger {
9592
// humane.New("foo", "bar")
9693
// )
9794
func (l *Logger) WithError(err error) *Logger {
98-
var e humane.Error
99-
if ok := errors.As(err, &e); !ok {
100-
return l
101-
}
95+
zapFields := make([]zap.Field, 0)
96+
zapFields = append(zapFields, zap.Error(err))
10297

103-
ctx := struct {
104-
Advice []string
105-
Causes []error
106-
}{
107-
Advice: e.Advice(),
108-
Causes: []error{},
109-
}
98+
advice := make([]string, 0)
99+
causes := make([]error, 0)
100+
for err != nil {
101+
var herr humane.Error
102+
if ok := errors.As(err, &herr); ok {
103+
causes = append(causes, err)
104+
advice = append(advice, herr.Advice()...)
105+
}
110106

111-
var cause error
112-
if e, ok := err.(interface {
113-
Cause() error
114-
}); ok {
115-
cause = e.Cause()
107+
err = errors.Unwrap(err)
116108
}
117109

118-
for cause != nil {
119-
ctx.Causes = append(ctx.Causes, cause)
120-
121-
if cause, ok := e.Cause().(interface {
122-
Advice() []string
123-
}); ok {
124-
ctx.Advice = append(cause.Advice(), ctx.Advice...)
125-
}
126-
127-
cause = errors.Unwrap(cause)
110+
if len(advice) > 0 {
111+
zapFields = append(zapFields, zap.Strings("error_advice", advice))
128112
}
129113

130-
zapFields := make([]zap.Field, 0)
131-
zapFields = append(zapFields, zap.Errors("causes", ctx.Causes))
132-
zapFields = append(zapFields, zap.Strings("advice", ctx.Advice))
133-
zapFields = append(zapFields, zap.Error(err))
114+
if len(causes) > 1 {
115+
zapFields = append(zapFields, zap.Errors("error_causes", causes[1:]))
116+
}
134117

135118
return l.With(zapFields...)
136119
}
@@ -169,41 +152,106 @@ func (l *Logger) Ctx(ctx context.Context) LoggerWithCtx {
169152
}
170153
}
171154

172-
func (l *Logger) DebugContext(ctx context.Context, msg string, fields ...zapcore.Field) {
173-
fields = l.logFields(ctx, zap.DebugLevel, msg, fields)
155+
// Log logs a message at the specified level. The message includes any fields
156+
// passed at the log site, as well as any fields accumulated on the logger.
157+
// Any Fields that require evaluation (such as Objects) are evaluated upon
158+
// invocation of Log.
159+
func (l *Logger) Log(lvl zapcore.Level, msg string, fields ...zapcore.Field) {
160+
fields = l.logFields(fields)
161+
l.skipCaller.Log(lvl, msg, fields...)
162+
}
163+
164+
// Debug logs a message at DebugLevel. The message includes any fields passed
165+
// at the log site, as well as any fields accumulated on the logger.
166+
func (l *Logger) Debug(msg string, fields ...zapcore.Field) {
167+
fields = l.logFields(fields)
174168
l.skipCaller.Debug(msg, fields...)
175169
}
176170

177-
func (l *Logger) InfoContext(ctx context.Context, msg string, fields ...zapcore.Field) {
178-
fields = l.logFields(ctx, zap.InfoLevel, msg, fields)
171+
// Info logs a message at InfoLevel. The message includes any fields passed
172+
// at the log site, as well as any fields accumulated on the logger.
173+
func (l *Logger) Info(msg string, fields ...zapcore.Field) {
174+
fields = l.logFields(fields)
179175
l.skipCaller.Info(msg, fields...)
180176
}
181177

182-
func (l *Logger) WarnContext(ctx context.Context, msg string, fields ...zapcore.Field) {
183-
fields = l.logFields(ctx, zap.WarnLevel, msg, fields)
178+
// Warn logs a message at WarnLevel. The message includes any fields passed
179+
// at the log site, as well as any fields accumulated on the logger.
180+
func (l *Logger) Warn(msg string, fields ...zapcore.Field) {
181+
fields = l.logFields(fields)
184182
l.skipCaller.Warn(msg, fields...)
185183
}
186184

187-
func (l *Logger) ErrorContext(ctx context.Context, msg string, fields ...zapcore.Field) {
188-
fields = l.logFields(ctx, zap.ErrorLevel, msg, fields)
185+
// Error logs a message at ErrorLevel. The message includes any fields passed
186+
// at the log site, as well as any fields accumulated on the logger.
187+
func (l *Logger) Error(msg string, fields ...zapcore.Field) {
188+
fields = l.logFields(fields)
189189
l.skipCaller.Error(msg, fields...)
190190
}
191191

192-
func (l *Logger) DPanicContext(ctx context.Context, msg string, fields ...zapcore.Field) {
193-
fields = l.logFields(ctx, zap.DPanicLevel, msg, fields)
192+
// DPanic logs a message at DPanicLevel. The message includes any fields
193+
// passed at the log site, as well as any fields accumulated on the logger.
194+
//
195+
// If the logger is in development mode, it then panics (DPanic means
196+
// "development panic"). This is useful for catching errors that are
197+
// recoverable, but shouldn't ever happen.
198+
func (l *Logger) DPanic(msg string, fields ...zapcore.Field) {
199+
fields = l.logFields(fields)
194200
l.skipCaller.DPanic(msg, fields...)
195201
}
196202

197-
func (l *Logger) PanicContext(ctx context.Context, msg string, fields ...zapcore.Field) {
198-
fields = l.logFields(ctx, zap.PanicLevel, msg, fields)
203+
// Panic logs a message at PanicLevel. The message includes any fields passed
204+
// at the log site, as well as any fields accumulated on the logger.
205+
//
206+
// The logger then panics, even if logging at PanicLevel is disabled.
207+
func (l *Logger) Panic(msg string, fields ...zapcore.Field) {
208+
fields = l.logFields(fields)
199209
l.skipCaller.Panic(msg, fields...)
200210
}
201211

202-
func (l *Logger) FatalContext(ctx context.Context, msg string, fields ...zapcore.Field) {
203-
fields = l.logFields(ctx, zap.FatalLevel, msg, fields)
212+
// Fatal logs a message at FatalLevel. The message includes any fields passed
213+
// at the log site, as well as any fields accumulated on the logger.
214+
//
215+
// The logger then calls os.Exit(1), even if logging at FatalLevel is
216+
// disabled.
217+
func (l *Logger) Fatal(msg string, fields ...zapcore.Field) {
218+
fields = l.logFields(fields)
204219
l.skipCaller.Fatal(msg, fields...)
205220
}
206221

222+
func (l *Logger) LogContext(ctx context.Context, lvl zapcore.Level, msg string, fields ...zapcore.Field) {
223+
fields = l.logFields(fields)
224+
l.Ctx(ctx).l.skipCaller.Log(lvl, msg, fields...)
225+
}
226+
227+
func (l *Logger) DebugContext(ctx context.Context, msg string, fields ...zapcore.Field) {
228+
l.Ctx(ctx).l.skipCaller.Debug(msg, fields...)
229+
}
230+
231+
func (l *Logger) InfoContext(ctx context.Context, msg string, fields ...zapcore.Field) {
232+
l.Ctx(ctx).l.skipCaller.Info(msg, fields...)
233+
}
234+
235+
func (l *Logger) WarnContext(ctx context.Context, msg string, fields ...zapcore.Field) {
236+
l.Ctx(ctx).l.skipCaller.Warn(msg, fields...)
237+
}
238+
239+
func (l *Logger) ErrorContext(ctx context.Context, msg string, fields ...zapcore.Field) {
240+
l.Ctx(ctx).l.skipCaller.Error(msg, fields...)
241+
}
242+
243+
func (l *Logger) DPanicContext(ctx context.Context, msg string, fields ...zapcore.Field) {
244+
l.Ctx(ctx).l.skipCaller.DPanic(msg, fields...)
245+
}
246+
247+
func (l *Logger) PanicContext(ctx context.Context, msg string, fields ...zapcore.Field) {
248+
l.Ctx(ctx).l.skipCaller.Panic(msg, fields...)
249+
}
250+
251+
func (l *Logger) FatalContext(ctx context.Context, msg string, fields ...zapcore.Field) {
252+
l.Ctx(ctx).l.skipCaller.Fatal(msg, fields...)
253+
}
254+
207255
func (l *Logger) Logf(classification logging.Classification, format string, fields ...interface{}) {
208256
msg := fmt.Sprintf(format, fields...)
209257

@@ -219,9 +267,7 @@ func (l *Logger) Logf(classification logging.Classification, format string, fiel
219267
}
220268
}
221269

222-
func (l *Logger) logFields(
223-
ctx context.Context, lvl zapcore.Level, msg string, fields []zapcore.Field,
224-
) []zapcore.Field {
270+
func (l *Logger) logFields(fields []zapcore.Field) []zapcore.Field {
225271
if len(l.extraFields) > 0 {
226272
fields = append(fields, l.extraFields...)
227273
}
@@ -231,55 +277,5 @@ func (l *Logger) logFields(
231277
l.extraFieldsOnce = make([]zap.Field, 0)
232278
}
233279

234-
if lvl >= l.minLevel {
235-
l.log(ctx, lvl, msg, convertFields(fields))
236-
}
237280
return fields
238281
}
239-
240-
func (l *Logger) log(
241-
ctx context.Context, lvl zapcore.Level, msg string, kvs []log.KeyValue,
242-
) {
243-
if lvl >= l.minAnnotateLevel || lvl >= l.errorStatusLevel {
244-
if span := trace.SpanFromContext(ctx); span.IsRecording() {
245-
if lvl >= l.minAnnotateLevel {
246-
for _, kv := range kvs {
247-
span.SetAttributes(Attribute(kv.Key, kv.Value))
248-
}
249-
}
250-
251-
if lvl >= l.errorStatusLevel {
252-
span.SetStatus(codes.Error, msg)
253-
span.RecordError(fmt.Errorf("%s", msg))
254-
}
255-
}
256-
}
257-
258-
record := log.Record{}
259-
record.SetBody(log.StringValue(msg))
260-
record.SetSeverity(convertLevel(lvl))
261-
262-
if l.caller {
263-
if fn, file, line, ok := runtimeCaller(4 + l.callerDepth); ok {
264-
if fn != "" {
265-
kvs = append(kvs, log.String("code.function", fn))
266-
}
267-
if file != "" {
268-
kvs = append(kvs, log.String("code.filepath", file))
269-
kvs = append(kvs, log.Int("code.lineno", line))
270-
}
271-
}
272-
}
273-
274-
if l.stackTrace {
275-
stackTrace := make([]byte, 2048)
276-
n := runtime.Stack(stackTrace, false)
277-
kvs = append(kvs, log.String("exception.stacktrace", string(stackTrace[:n])))
278-
}
279-
280-
if len(kvs) > 0 {
281-
record.AddAttributes(kvs...)
282-
}
283-
284-
l.otelLogger.Emit(ctx, record)
285-
}

0 commit comments

Comments
 (0)