Skip to content

Commit

Permalink
Merge branch 'main' into main
Browse files Browse the repository at this point in the history
  • Loading branch information
Victorsesan authored Oct 16, 2024
2 parents bf006b8 + 856523b commit 6ef0aeb
Show file tree
Hide file tree
Showing 101 changed files with 1,497 additions and 348 deletions.
22 changes: 14 additions & 8 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,17 @@ This project adheres to [Semantic Versioning](https://semver.org/spec/v2.0.0.htm

## [Unreleased]

### Fixed

- Transform nil attribute values to `log.Value` zero value instead of panicking in `go.opentelemetry.io/contrib/bridges/otellogrus`. (#6237)
- Transform nil attribute values to `log.Value` zero value instead of panicking in `go.opentelemetry.io/contrib/bridges/otelzap`. (#6237)
- Transform nil attribute values to `log.Value` zero value instead of `log.StringValue("<nil>")` in `go.opentelemetry.io/contrib/bridges/otelslog`. (#6246)

<!-- Released section -->
<!-- Don't change this section unless doing release -->

## [1.31.0/0.56.0/0.25.0/0.11.0/0.6.0/0.4.0/0.3.0] - 2024-10-14

### Added

- The `Severitier` and `SeverityVar` types are added to `go.opentelemetry.io/contrib/processors/minsev` allowing dynamic configuration of the severity used by the `LogProcessor`. (#6116)
Expand All @@ -19,6 +30,7 @@ This project adheres to [Semantic Versioning](https://semver.org/spec/v2.0.0.htm
### Changed

- The function signature of `NewLogProcessor` in `go.opentelemetry.io/contrib/processors/minsev` has changed to accept the added `Severitier` interface instead of a `log.Severity`. (#6116)
- Updated `go.opentelemetry.io/contrib/config` to use the [v0.3.0](https://github.com/open-telemetry/opentelemetry-configuration/releases/tag/v0.3.0) release of schema which includes backwards incompatible changes. (#6126)
- `NewSDK` in `go.opentelemetry.io/contrib/config` now returns a no-op SDK if `disabled` is set to `true`. (#6185)
- The deprecated `go.opentelemetry.io/contrib/instrumentation/github.com/labstack/echo/otelecho` package has found a Code Owner.
The package is no longer deprecated. (#6207)
Expand All @@ -35,13 +47,6 @@ This project adheres to [Semantic Versioning](https://semver.org/spec/v2.0.0.htm
- The deprecated `go.opentelemetry.io/contrib/instrumentation/gopkg.in/macaron.v1/otelmacaron` package is removed. (#6186)
- The deprecated `go.opentelemetry.io/contrib/samplers/aws/xray` package is removed. (#6187)

### Changed

- Updated `go.opentelemetry.io/contrib/config` to use the [v0.3.0](https://github.com/open-telemetry/opentelemetry-configuration/releases/tag/v0.3.0) release of schema which includes backwards incompatible changes. (#6126)

<!-- Released section -->
<!-- Don't change this section unless doing release -->

## [1.30.0/0.55.0/0.24.0/0.10.0/0.5.0/0.3.0/0.2.0] - 2024-09-10

### Added
Expand Down Expand Up @@ -1140,7 +1145,8 @@ First official tagged release of `contrib` repository.
- Prefix support for dogstatsd (#34)
- Update Go Runtime package to use batch observer (#44)

[Unreleased]: https://github.com/open-telemetry/opentelemetry-go-contrib/compare/v1.30.0...HEAD
[Unreleased]: https://github.com/open-telemetry/opentelemetry-go-contrib/compare/v1.31.0...HEAD
[1.31.0/0.56.0/0.25.0/0.11.0/0.6.0/0.4.0/0.3.0]: https://github.com/open-telemetry/opentelemetry-go-contrib/releases/tag/v1.31.0
[1.30.0/0.55.0/0.24.0/0.10.0/0.5.0/0.3.0/0.2.0]: https://github.com/open-telemetry/opentelemetry-go-contrib/releases/tag/v1.30.0
[1.29.0/0.54.0/0.23.0/0.9.0/0.4.0/0.2.0/0.1.0]: https://github.com/open-telemetry/opentelemetry-go-contrib/releases/tag/v1.29.0
[1.28.0/0.53.0/0.22.0/0.8.0/0.3.0/0.1.0]: https://github.com/open-telemetry/opentelemetry-go-contrib/releases/tag/v1.28.0
Expand Down
41 changes: 4 additions & 37 deletions bridges/otellogr/convert.go
Original file line number Diff line number Diff line change
@@ -1,10 +1,12 @@
// Code created by gotmpl. DO NOT MODIFY.
// source: internal/shared/logutil/convert.go.tmpl

// Copyright The OpenTelemetry Authors
// SPDX-License-Identifier: Apache-2.0

package otellogr // import "go.opentelemetry.io/contrib/bridges/otellogr"

import (
"context"
"fmt"
"math"
"reflect"
Expand All @@ -14,42 +16,7 @@ import (
"go.opentelemetry.io/otel/log"
)

// convertKVs converts a list of key-value pairs to a list of [log.KeyValue].
// The last [context.Context] value is returned as the context.
// If no context is found, the original context is returned.
func convertKVs(ctx context.Context, keysAndValues ...any) (context.Context, []log.KeyValue) {
if len(keysAndValues) == 0 {
return ctx, nil
}
if len(keysAndValues)%2 != 0 {
// Ensure an odd number of items here does not corrupt the list.
keysAndValues = append(keysAndValues, nil)
}

kvs := make([]log.KeyValue, 0, len(keysAndValues)/2)
for i := 0; i < len(keysAndValues); i += 2 {
k, ok := keysAndValues[i].(string)
if !ok {
// Ensure that the key is a string.
k = fmt.Sprintf("%v", keysAndValues[i])
}

v := keysAndValues[i+1]
if vCtx, ok := v.(context.Context); ok {
// Special case when a field is of context.Context type.
ctx = vCtx
continue
}

kvs = append(kvs, log.KeyValue{
Key: k,
Value: convertValue(v),
})
}

return ctx, kvs
}

// convertValue converts various types to log.Value.
func convertValue(v any) log.Value {
// Handling the most common types without reflect is a small perf win.
switch val := v.(type) {
Expand Down
80 changes: 16 additions & 64 deletions bridges/otellogr/convert_test.go
Original file line number Diff line number Diff line change
@@ -1,3 +1,6 @@
// Code created by gotmpl. DO NOT MODIFY.
// source: internal/shared/logutil/convert_test.go.tmpl

// Copyright The OpenTelemetry Authors
// SPDX-License-Identifier: Apache-2.0

Expand All @@ -15,70 +18,6 @@ import (
"go.opentelemetry.io/otel/log"
)

func TestConvertKVs(t *testing.T) {
ctx := context.WithValue(context.Background(), "key", "value") // nolint: revive,staticcheck // test context

for _, tt := range []struct {
name string
kvs []any
wantKVs []log.KeyValue
wantCtx context.Context
}{
{
name: "empty",
kvs: []any{},
},
{
name: "single_value",
kvs: []any{"key", "value"},
wantKVs: []log.KeyValue{
log.String("key", "value"),
},
},
{
name: "multiple_values",
kvs: []any{"key1", "value1", "key2", "value2"},
wantKVs: []log.KeyValue{
log.String("key1", "value1"),
log.String("key2", "value2"),
},
},
{
name: "missing_value",
kvs: []any{"key1", "value1", "key2"},
wantKVs: []log.KeyValue{
log.String("key1", "value1"),
{Key: "key2", Value: log.Value{}},
},
},
{
name: "key_not_string",
kvs: []any{42, "value"},
wantKVs: []log.KeyValue{
log.String("42", "value"),
},
},
{
name: "context",
kvs: []any{"ctx", ctx, "key", "value"},
wantKVs: []log.KeyValue{log.String("key", "value")},
wantCtx: ctx,
},
{
name: "last_context",
kvs: []any{"key", context.Background(), "ctx", ctx},
wantKVs: []log.KeyValue{},
wantCtx: ctx,
},
} {
t.Run(tt.name, func(t *testing.T) {
ctx, kvs := convertKVs(nil, tt.kvs...) // nolint: staticcheck // pass nil context
assert.Equal(t, tt.wantKVs, kvs)
assert.Equal(t, tt.wantCtx, ctx)
})
}
}

func TestConvertValue(t *testing.T) {
for _, tt := range []struct {
name string
Expand Down Expand Up @@ -293,11 +232,24 @@ func TestConvertValue(t *testing.T) {
},
wantValue: log.StringValue("{Name:John Age:42}"),
},
{
name: "nil_struct_ptr",
value: (*struct {
Name string
Age int
})(nil),
wantValue: log.Value{},
},
{
name: "ctx",
value: context.Background(),
wantValue: log.StringValue("context.Background"),
},
{
name: "unhandled type",
value: chan int(nil),
wantValue: log.StringValue("unhandled: (chan int) <nil>"),
},
} {
t.Run(tt.name, func(t *testing.T) {
assert.Equal(t, tt.wantValue, convertValue(tt.value))
Expand Down
8 changes: 8 additions & 0 deletions bridges/otellogr/gen.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,8 @@
// Copyright The OpenTelemetry Authors
// SPDX-License-Identifier: Apache-2.0

package otellogr // import "go.opentelemetry.io/contrib/bridges/otellogr"

// Generate convert:
//go:generate gotmpl --body=../../internal/shared/logutil/convert_test.go.tmpl "--data={ \"pkg\": \"otellogr\" }" --out=convert_test.go
//go:generate gotmpl --body=../../internal/shared/logutil/convert.go.tmpl "--data={ \"pkg\": \"otellogr\" }" --out=convert.go
37 changes: 37 additions & 0 deletions bridges/otellogr/logsink.go
Original file line number Diff line number Diff line change
Expand Up @@ -45,6 +45,7 @@ package otellogr // import "go.opentelemetry.io/contrib/bridges/otellogr"

import (
"context"
"fmt"

"github.com/go-logr/logr"

Expand Down Expand Up @@ -198,3 +199,39 @@ func (l LogSink) WithValues(keysAndValues ...any) logr.LogSink {
l.ctx = ctx
return &l
}

// convertKVs converts a list of key-value pairs to a list of [log.KeyValue].
// The last [context.Context] value is returned as the context.
// If no context is found, the original context is returned.
func convertKVs(ctx context.Context, keysAndValues ...any) (context.Context, []log.KeyValue) {
if len(keysAndValues) == 0 {
return ctx, nil
}
if len(keysAndValues)%2 != 0 {
// Ensure an odd number of items here does not corrupt the list.
keysAndValues = append(keysAndValues, nil)
}

kvs := make([]log.KeyValue, 0, len(keysAndValues)/2)
for i := 0; i < len(keysAndValues); i += 2 {
k, ok := keysAndValues[i].(string)
if !ok {
// Ensure that the key is a string.
k = fmt.Sprintf("%v", keysAndValues[i])
}

v := keysAndValues[i+1]
if vCtx, ok := v.(context.Context); ok {
// Special case when a field is of context.Context type.
ctx = vCtx
continue
}

kvs = append(kvs, log.KeyValue{
Key: k,
Value: convertValue(v),
})
}

return ctx, kvs
}
80 changes: 80 additions & 0 deletions bridges/otellogr/logsink_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@
package otellogr

import (
"context"
"testing"
"time"

Expand Down Expand Up @@ -216,6 +217,21 @@ func TestLogSink(t *testing.T) {
},
},
},
{
name: "info_with_normal_attr_and_nil_pointer_attr",
f: func(l *logr.Logger) {
var p *int
l.WithValues("key", "value", "nil_pointer", p).Info("info message with attrs")
},
wantRecords: map[string][]log.Record{
name: {
buildRecord(log.StringValue("info message with attrs"), time.Time{}, log.SeverityInfo, []log.KeyValue{
log.String("key", "value"),
log.Empty("nil_pointer"),
}),
},
},
},
} {
t.Run(tt.name, func(t *testing.T) {
rec := logtest.NewRecorder()
Expand Down Expand Up @@ -263,3 +279,67 @@ func assertRecords(t *testing.T, want, got []logtest.EmittedRecord) {
logtest.AssertRecordEqual(t, j.Record, got[i].Record)
}
}

func TestConvertKVs(t *testing.T) {
ctx := context.WithValue(context.Background(), "key", "value") // nolint: revive,staticcheck // test context

for _, tt := range []struct {
name string
kvs []any
wantKVs []log.KeyValue
wantCtx context.Context
}{
{
name: "empty",
kvs: []any{},
},
{
name: "single_value",
kvs: []any{"key", "value"},
wantKVs: []log.KeyValue{
log.String("key", "value"),
},
},
{
name: "multiple_values",
kvs: []any{"key1", "value1", "key2", "value2"},
wantKVs: []log.KeyValue{
log.String("key1", "value1"),
log.String("key2", "value2"),
},
},
{
name: "missing_value",
kvs: []any{"key1", "value1", "key2"},
wantKVs: []log.KeyValue{
log.String("key1", "value1"),
{Key: "key2", Value: log.Value{}},
},
},
{
name: "key_not_string",
kvs: []any{42, "value"},
wantKVs: []log.KeyValue{
log.String("42", "value"),
},
},
{
name: "context",
kvs: []any{"ctx", ctx, "key", "value"},
wantKVs: []log.KeyValue{log.String("key", "value")},
wantCtx: ctx,
},
{
name: "last_context",
kvs: []any{"key", context.Background(), "ctx", ctx},
wantKVs: []log.KeyValue{},
wantCtx: ctx,
},
} {
t.Run(tt.name, func(t *testing.T) {
ctx, kvs := convertKVs(nil, tt.kvs...) // nolint: staticcheck // pass nil context
assert.Equal(t, tt.wantKVs, kvs)
assert.Equal(t, tt.wantCtx, ctx)
})
}
}
Loading

0 comments on commit 6ef0aeb

Please sign in to comment.