Skip to content
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
44 changes: 22 additions & 22 deletions go.mod
Original file line number Diff line number Diff line change
Expand Up @@ -12,7 +12,7 @@ require (
github.com/go-resty/resty/v2 v2.17.2
github.com/golang-migrate/migrate/v4 v4.19.1
github.com/google/uuid v1.6.0
github.com/grpc-ecosystem/grpc-gateway/v2 v2.28.0
github.com/grpc-ecosystem/grpc-gateway/v2 v2.29.0
github.com/labstack/echo/v4 v4.15.1
github.com/lib/pq v1.12.0
github.com/nexus-rpc/sdk-go v0.6.0
Expand All @@ -24,21 +24,21 @@ require (
github.com/testcontainers/testcontainers-go/modules/mysql v0.40.0
github.com/testcontainers/testcontainers-go/modules/postgres v0.40.0
github.com/uptrace/opentelemetry-go-extra/otelgorm v0.3.2
go.opentelemetry.io/otel v1.42.0
go.opentelemetry.io/otel/exporters/otlp/otlplog/otlploghttp v0.18.0
go.opentelemetry.io/otel/log v0.18.0
go.opentelemetry.io/otel/metric v1.42.0
go.opentelemetry.io/otel/sdk v1.42.0
go.opentelemetry.io/otel/sdk/log v0.18.0
go.opentelemetry.io/otel/sdk/metric v1.42.0
go.opentelemetry.io/otel/trace v1.42.0
go.opentelemetry.io/otel v1.45.0
go.opentelemetry.io/otel/exporters/otlp/otlplog/otlploghttp v0.21.0
go.opentelemetry.io/otel/log v0.21.0
go.opentelemetry.io/otel/metric v1.45.0
go.opentelemetry.io/otel/sdk v1.45.0
go.opentelemetry.io/otel/sdk/log v0.21.0
go.opentelemetry.io/otel/sdk/metric v1.45.0
go.opentelemetry.io/otel/trace v1.45.0
go.temporal.io/api v1.62.6
go.temporal.io/sdk v1.41.1
go.temporal.io/sdk/contrib/opentelemetry v0.7.0
golang.org/x/crypto v0.49.0
golang.org/x/net v0.52.0
golang.org/x/crypto v0.54.0
golang.org/x/net v0.57.0
golang.org/x/time v0.15.0
google.golang.org/grpc v1.79.3
google.golang.org/grpc v1.83.0
google.golang.org/protobuf v1.36.11
gopkg.in/yaml.v3 v3.0.1
gorm.io/driver/mysql v1.6.0
Expand Down Expand Up @@ -84,8 +84,8 @@ require (
github.com/fxamacker/cbor/v2 v2.9.0 // indirect
github.com/gabriel-vasile/mimetype v1.4.11 // indirect
github.com/go-jose/go-jose/v3 v3.0.4 // indirect
github.com/go-jose/go-jose/v4 v4.1.3 // indirect
github.com/go-logr/logr v1.4.3 // indirect
github.com/go-jose/go-jose/v4 v4.1.4 // indirect
github.com/go-logr/logr v1.4.4 // indirect
github.com/go-logr/stdr v1.2.2 // indirect
github.com/go-ole/go-ole v1.3.0 // indirect
github.com/go-openapi/jsonpointer v0.22.3 // indirect
Expand Down Expand Up @@ -199,18 +199,18 @@ require (
go.opentelemetry.io/contrib/instrumentation/runtime v0.67.0 // indirect
go.opentelemetry.io/otel/exporters/otlp/otlpmetric/otlpmetricgrpc v1.42.0 // indirect
go.opentelemetry.io/otel/exporters/prometheus v0.64.0 // indirect
go.opentelemetry.io/proto/otlp v1.10.0 // indirect
go.opentelemetry.io/proto/otlp v1.11.0 // indirect
go.yaml.in/yaml/v2 v2.4.4 // indirect
go.yaml.in/yaml/v3 v3.0.4 // indirect
golang.org/x/exp v0.0.0-20251113190631-e25ba8c21ef6 // indirect
golang.org/x/oauth2 v0.35.0 // indirect
golang.org/x/sync v0.20.0 // indirect
golang.org/x/sys v0.42.0 // indirect
golang.org/x/term v0.41.0 // indirect
golang.org/x/text v0.35.0 // indirect
golang.org/x/oauth2 v0.36.0 // indirect
golang.org/x/sync v0.22.0 // indirect
golang.org/x/sys v0.47.0 // indirect
golang.org/x/term v0.45.0 // indirect
golang.org/x/text v0.40.0 // indirect
google.golang.org/genproto v0.0.0-20251111163417-95abcf5c77ba // indirect
google.golang.org/genproto/googleapis/api v0.0.0-20260319201613-d00831a3d3e7 // indirect
google.golang.org/genproto/googleapis/rpc v0.0.0-20260319201613-d00831a3d3e7 // indirect
google.golang.org/genproto/googleapis/api v0.0.0-20260803160001-6ac0973c030d // indirect
google.golang.org/genproto/googleapis/rpc v0.0.0-20260803160001-6ac0973c030d // indirect
gopkg.in/evanphx/json-patch.v4 v4.13.0 // indirect
gopkg.in/inf.v0 v0.9.1 // indirect
k8s.io/klog/v2 v2.130.1 // indirect
Expand Down
106 changes: 54 additions & 52 deletions go.sum

Large diffs are not rendered by default.

56 changes: 28 additions & 28 deletions grpc/otel_instrumentation.go
Original file line number Diff line number Diff line change
Expand Up @@ -288,23 +288,23 @@ func createGRPCLoggingInterceptor(cfg *pkgotel.Config) grpc.UnaryServerIntercept
st, _ := status.FromError(err)

// Create log attributes
attrs := []otellog.KeyValue{
otellog.String("rpc.system", "grpc"),
otellog.String("rpc.method", info.FullMethod),
otellog.String("rpc.service", extractServiceName(info.FullMethod)),
otellog.Int("rpc.grpc.status_code", int(st.Code())),
otellog.Int64("rpc.duration_ms", duration.Milliseconds()),
attrs := []attribute.KeyValue{
attribute.String("rpc.system", "grpc"),
attribute.String("rpc.method", info.FullMethod),
attribute.String("rpc.service", extractServiceName(info.FullMethod)),
attribute.Int("rpc.grpc.status_code", int(st.Code())),
attribute.Int64("rpc.duration_ms", duration.Milliseconds()),
}

if err != nil {
attrs = append(attrs, otellog.String("error", err.Error()))
attrs = append(attrs, attribute.String("error", err.Error()))
}

// Emit log record
var logRecord otellog.Record
logRecord.SetTimestamp(start)
logRecord.SetSeverity(severity)
logRecord.SetBody(otellog.StringValue(fmt.Sprintf("gRPC %s", info.FullMethod)))
logRecord.SetBody(attribute.StringValue(fmt.Sprintf("gRPC %s", info.FullMethod)))
logRecord.AddAttributes(attrs...)

logger.Emit(ctx, logRecord)
Expand Down Expand Up @@ -340,24 +340,24 @@ func createGRPCStreamLoggingInterceptor(cfg *pkgotel.Config) grpc.StreamServerIn

st, _ := status.FromError(err)

attrs := []otellog.KeyValue{
otellog.String("rpc.system", "grpc"),
otellog.String("rpc.method", info.FullMethod),
otellog.String("rpc.service", extractServiceName(info.FullMethod)),
otellog.Int("rpc.grpc.status_code", int(st.Code())),
otellog.Int64("rpc.duration_ms", duration.Milliseconds()),
otellog.Bool("rpc.is_client_stream", info.IsClientStream),
otellog.Bool("rpc.is_server_stream", info.IsServerStream),
attrs := []attribute.KeyValue{
attribute.String("rpc.system", "grpc"),
attribute.String("rpc.method", info.FullMethod),
attribute.String("rpc.service", extractServiceName(info.FullMethod)),
attribute.Int("rpc.grpc.status_code", int(st.Code())),
attribute.Int64("rpc.duration_ms", duration.Milliseconds()),
attribute.Bool("rpc.is_client_stream", info.IsClientStream),
attribute.Bool("rpc.is_server_stream", info.IsServerStream),
}

if err != nil {
attrs = append(attrs, otellog.String("error", err.Error()))
attrs = append(attrs, attribute.String("error", err.Error()))
}

var logRecord otellog.Record
logRecord.SetTimestamp(start)
logRecord.SetSeverity(severity)
logRecord.SetBody(otellog.StringValue(fmt.Sprintf("gRPC stream %s", info.FullMethod)))
logRecord.SetBody(attribute.StringValue(fmt.Sprintf("gRPC stream %s", info.FullMethod)))
logRecord.AddAttributes(attrs...)

logger.Emit(ss.Context(), logRecord)
Expand Down Expand Up @@ -582,25 +582,25 @@ func createHTTPGatewayLoggingMiddleware(cfg *pkgotel.Config) echo.MiddlewareFunc
}

// Create log attributes
attrs := []otellog.KeyValue{
otellog.String("http.method", req.Method),
otellog.String("http.route", c.Path()),
otellog.String("http.url", req.RequestURI),
otellog.Int("http.status_code", c.Response().Status),
otellog.Int64("http.request_size", req.ContentLength),
otellog.Int64("http.response_size", c.Response().Size),
otellog.Int64("http.duration_ms", duration.Milliseconds()),
attrs := []attribute.KeyValue{
attribute.String("http.method", req.Method),
attribute.String("http.route", c.Path()),
attribute.String("http.url", req.RequestURI),
attribute.Int("http.status_code", c.Response().Status),
attribute.Int64("http.request_size", req.ContentLength),
attribute.Int64("http.response_size", c.Response().Size),
attribute.Int64("http.duration_ms", duration.Milliseconds()),
}

if err != nil {
attrs = append(attrs, otellog.String("error", err.Error()))
attrs = append(attrs, attribute.String("error", err.Error()))
}

// Emit log record
var logRecord otellog.Record
logRecord.SetTimestamp(start)
logRecord.SetSeverity(severity)
logRecord.SetBody(otellog.StringValue(fmt.Sprintf("%s %s", req.Method, req.RequestURI)))
logRecord.SetBody(attribute.StringValue(fmt.Sprintf("%s %s", req.Method, req.RequestURI)))
logRecord.AddAttributes(attrs...)

logger.Emit(logCtx, logRecord)
Expand Down
7 changes: 4 additions & 3 deletions grpc/server.go
Original file line number Diff line number Diff line change
Expand Up @@ -17,9 +17,10 @@ import (
"github.com/grpc-ecosystem/grpc-gateway/v2/runtime"
"github.com/labstack/echo/v4"
"github.com/labstack/echo/v4/middleware"
"go.opentelemetry.io/otel/attribute"
otellog "go.opentelemetry.io/otel/log"
"golang.org/x/net/http2"
"golang.org/x/net/http2/h2c"
"golang.org/x/net/http2/h2c" //nolint:staticcheck // deprecated in x/net v0.57; http.Server.Protocols drops the HTTP/1.1 Upgrade path, so replacing it is a separate change
"golang.org/x/time/rate"
"google.golang.org/grpc"
"google.golang.org/grpc/keepalive"
Expand Down Expand Up @@ -74,7 +75,7 @@ func (s *Server) logInfo(msg string) {
logger := s.config.otelConfig.GetLogger("grpc.server")
var rec otellog.Record
rec.SetSeverity(otellog.SeverityInfo)
rec.SetBody(otellog.StringValue(msg))
rec.SetBody(attribute.StringValue(msg))
logger.Emit(context.Background(), rec)
return
}
Expand Down Expand Up @@ -415,7 +416,7 @@ func (s *Server) startH2CMode() error {
// IdleTimeout remain safe and are kept for slowloris/idle protection.
s.httpServer = &http.Server{
Addr: s.config.getGRPCAddress(),
Handler: h2c.NewHandler(mixedHandler, &http2.Server{}),
Handler: h2c.NewHandler(mixedHandler, &http2.Server{}), //nolint:staticcheck // see the h2c import
ReadHeaderTimeout: 5 * time.Second,
IdleTimeout: s.config.idleTimeout,
}
Expand Down
19 changes: 10 additions & 9 deletions otel/helper.go
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,7 @@ import (
"time"

"github.com/rs/zerolog"
"go.opentelemetry.io/otel/attribute"
"go.opentelemetry.io/otel/codes"
otellog "go.opentelemetry.io/otel/log"
"go.opentelemetry.io/otel/trace"
Expand Down Expand Up @@ -228,30 +229,30 @@ func (h *LogHelper) Error(err error, msg string, fields ...Field) {
func (h *LogHelper) emitOTel(severity otellog.Severity, msg string, fields ...Field) {
var record otellog.Record
record.SetTimestamp(time.Now())
record.SetBody(otellog.StringValue(msg))
record.SetBody(attribute.StringValue(msg))
record.SetSeverity(severity)
record.SetSeverityText(severityText(severity))

if h.function != "" {
record.AddAttributes(otellog.String("function", h.function))
record.AddAttributes(attribute.String("function", h.function))
}

for _, field := range fields {
switch v := field.Value.(type) {
case string:
record.AddAttributes(otellog.String(field.Key, v))
record.AddAttributes(attribute.String(field.Key, v))
case bool:
record.AddAttributes(otellog.Bool(field.Key, v))
record.AddAttributes(attribute.Bool(field.Key, v))
case int:
record.AddAttributes(otellog.Int64(field.Key, int64(v)))
record.AddAttributes(attribute.Int64(field.Key, int64(v)))
case int64:
record.AddAttributes(otellog.Int64(field.Key, v))
record.AddAttributes(attribute.Int64(field.Key, v))
case float64:
record.AddAttributes(otellog.Float64(field.Key, v))
record.AddAttributes(attribute.Float64(field.Key, v))
case time.Duration:
record.AddAttributes(otellog.String(field.Key, v.String()))
record.AddAttributes(attribute.String(field.Key, v.String()))
default:
record.AddAttributes(otellog.String(field.Key, fmt.Sprint(v)))
record.AddAttributes(attribute.String(field.Key, fmt.Sprint(v)))
}
}

Expand Down
27 changes: 12 additions & 15 deletions otel/logging.go
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,7 @@ import (
"time"

"github.com/rs/zerolog"
"go.opentelemetry.io/otel/attribute"
"go.opentelemetry.io/otel/exporters/otlp/otlplog/otlploghttp"
"go.opentelemetry.io/otel/log"
sdklog "go.opentelemetry.io/otel/sdk/log"
Expand Down Expand Up @@ -232,7 +233,7 @@ func (e *consoleExporter) Export(ctx context.Context, records []sdklog.Record) e
}
}

record.WalkAttributes(func(kv log.KeyValue) bool {
record.WalkAttributes(func(kv attribute.KeyValue) bool {
event = addAttributeToEvent(event, kv)
return true
})
Expand Down Expand Up @@ -297,26 +298,22 @@ func severityToZerologEvent(logger zerolog.Logger, severity log.Severity) *zerol
}

// addAttributeToEvent adds a log attribute to zerolog event
func addAttributeToEvent(event *zerolog.Event, kv log.KeyValue) *zerolog.Event {
key := kv.Key
func addAttributeToEvent(event *zerolog.Event, kv attribute.KeyValue) *zerolog.Event {
key := string(kv.Key)
value := kv.Value

switch value.Kind() {
case log.KindBool:
switch value.Type() {
case attribute.BOOL:
return event.Bool(key, value.AsBool())
case log.KindInt64:
case attribute.INT64:
return event.Int64(key, value.AsInt64())
case log.KindFloat64:
case attribute.FLOAT64:
return event.Float64(key, value.AsFloat64())
case log.KindString:
case attribute.STRING:
return event.Str(key, value.AsString())
case log.KindBytes:
return event.Bytes(key, value.AsBytes())
case log.KindSlice:
return event.Interface(key, value.AsSlice())
case log.KindMap:
return event.Interface(key, value.AsMap())
case attribute.BYTESLICE:
return event.Bytes(key, value.AsByteSlice())
default:
return event.Interface(key, fmt.Sprint(value))
return event.Interface(key, value.AsInterface())
}
}
27 changes: 14 additions & 13 deletions otel/logging_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -12,6 +12,7 @@ import (

"github.com/stretchr/testify/assert"
"github.com/stretchr/testify/require"
"go.opentelemetry.io/otel/attribute"
"go.opentelemetry.io/otel/log"
"go.opentelemetry.io/otel/log/noop"
sdklog "go.opentelemetry.io/otel/sdk/log"
Expand Down Expand Up @@ -46,17 +47,17 @@ func (e *memLogExporter) records() []sdklog.Record {
// recordAttrs collects a record's attributes into a map for easy assertions.
func recordAttrs(r *sdklog.Record) map[string]any {
out := make(map[string]any)
r.WalkAttributes(func(kv log.KeyValue) bool {
out[kv.Key] = kv.Value.AsString()
switch kv.Value.Kind() {
case log.KindBool:
out[kv.Key] = kv.Value.AsBool()
case log.KindInt64:
out[kv.Key] = kv.Value.AsInt64()
case log.KindFloat64:
out[kv.Key] = kv.Value.AsFloat64()
case log.KindString:
out[kv.Key] = kv.Value.AsString()
r.WalkAttributes(func(kv attribute.KeyValue) bool {
out[string(kv.Key)] = kv.Value.AsString()
switch kv.Value.Type() {
case attribute.BOOL:
out[string(kv.Key)] = kv.Value.AsBool()
case attribute.INT64:
out[string(kv.Key)] = kv.Value.AsInt64()
case attribute.FLOAT64:
out[string(kv.Key)] = kv.Value.AsFloat64()
case attribute.STRING:
out[string(kv.Key)] = kv.Value.AsString()
}
return true
})
Expand Down Expand Up @@ -189,7 +190,7 @@ func TestNewLoggerProviderWithOptions_OTLPEndpointURL(t *testing.T) {

logger := provider.Logger("test-scope")
var rec log.Record
rec.SetBody(log.StringValue("hello"))
rec.SetBody(attribute.StringValue("hello"))
rec.SetSeverity(log.SeverityInfo)
logger.Emit(context.Background(), rec)

Expand Down Expand Up @@ -417,7 +418,7 @@ func TestLoggerProvider_NoopComparison(t *testing.T) {

ctx := context.Background()
var record log.Record
record.SetBody(log.StringValue("test message"))
record.SetBody(attribute.StringValue("test message"))

assert.NotPanics(t, func() {
ourLogger.Emit(ctx, record)
Expand Down
Loading
Loading