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

Handle structured body and attributes #7

Merged
merged 13 commits into from
Jan 24, 2024
154 changes: 77 additions & 77 deletions log/internal/bench_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -17,7 +17,6 @@ import (
"testing"
"time"

"go.opentelemetry.io/otel/attribute"
"go.opentelemetry.io/otel/log"
"go.opentelemetry.io/otel/log/noop"
"go.opentelemetry.io/otel/trace"
Expand All @@ -27,14 +26,15 @@ import (
)

var (
ctx = trace.ContextWithSpanContext(context.Background(), trace.NewSpanContext(trace.SpanContextConfig{TraceID: [16]byte{1}, SpanID: [8]byte{42}}))
testTimestamp = time.Date(1988, time.November, 17, 0, 0, 0, 0, time.UTC)
testBody = "log message"
testSeverity = log.SeverityInfo
testFloat = 1.2345
testString = "7e3b3b2aaeff56a7108fe11e154200dd/7819479873059528190"
testInt = 32768
testBool = true
ctx = trace.ContextWithSpanContext(context.Background(), trace.NewSpanContext(trace.SpanContextConfig{TraceID: [16]byte{1}, SpanID: [8]byte{42}}))
testTimestamp = time.Date(1988, time.November, 17, 0, 0, 0, 0, time.UTC)
testBodyString = "log message"
testBody = log.StringValue(testBodyString)
testSeverity = log.SeverityInfo
testFloat = 1.2345
testString = "7e3b3b2aaeff56a7108fe11e154200dd/7819479873059528190"
testInt = 32768
testBool = true
)

// WriterLogger is an optimistic version of a real logger, doing real-world
Expand Down Expand Up @@ -74,9 +74,9 @@ func BenchmarkEmit(b *testing.B) {
r.SetSeverity(testSeverity)
r.SetBody(testBody)
r.AddAttributes(
attribute.String("string", testString),
attribute.Float64("float", testFloat),
attribute.Int("int", testInt),
log.String("string", testString),
log.Float64("float", testFloat),
log.Int("int", testInt),
)
tc.logger.Emit(ctx, r)
},
Expand All @@ -93,11 +93,11 @@ func BenchmarkEmit(b *testing.B) {
r.SetSeverity(testSeverity)
r.SetBody(testBody)
r.AddAttributes(
attribute.String("string", testString),
attribute.Float64("float", testFloat),
attribute.Int("int", testInt),
attribute.Bool("bool", testBool),
attribute.String("string", testString),
log.String("string", testString),
log.Float64("float", testFloat),
log.Int("int", testInt),
log.Bool("bool", testBool),
log.String("string", testString),
)
tc.logger.Emit(ctx, r)
},
Expand All @@ -110,16 +110,16 @@ func BenchmarkEmit(b *testing.B) {
r.SetSeverity(testSeverity)
r.SetBody(testBody)
r.AddAttributes(
attribute.String("string", testString),
attribute.Float64("float", testFloat),
attribute.Int("int", testInt),
attribute.Bool("bool", testBool),
attribute.String("string", testString),
attribute.String("string", testString),
attribute.Float64("float", testFloat),
attribute.Int("int", testInt),
attribute.Bool("bool", testBool),
attribute.String("string", testString),
log.String("string", testString),
log.Float64("float", testFloat),
log.Int("int", testInt),
log.Bool("bool", testBool),
log.String("string", testString),
log.String("string", testString),
log.Float64("float", testFloat),
log.Int("int", testInt),
log.Bool("bool", testBool),
log.String("string", testString),
)
tc.logger.Emit(ctx, r)
},
Expand All @@ -132,46 +132,46 @@ func BenchmarkEmit(b *testing.B) {
r.SetSeverity(testSeverity)
r.SetBody(testBody)
r.AddAttributes(
attribute.String("string", testString),
attribute.Float64("float", testFloat),
attribute.Int("int", testInt),
attribute.Bool("bool", testBool),
attribute.String("string", testString),
attribute.String("string", testString),
attribute.Float64("float", testFloat),
attribute.Int("int", testInt),
attribute.Bool("bool", testBool),
attribute.String("string", testString),
attribute.String("string", testString),
attribute.Float64("float", testFloat),
attribute.Int("int", testInt),
attribute.Bool("bool", testBool),
attribute.String("string", testString),
attribute.String("string", testString),
attribute.Float64("float", testFloat),
attribute.Int("int", testInt),
attribute.Bool("bool", testBool),
attribute.String("string", testString),
attribute.String("string", testString),
attribute.Float64("float", testFloat),
attribute.Int("int", testInt),
attribute.Bool("bool", testBool),
attribute.String("string", testString),
attribute.String("string", testString),
attribute.Float64("float", testFloat),
attribute.Int("int", testInt),
attribute.Bool("bool", testBool),
attribute.String("string", testString),
attribute.String("string", testString),
attribute.Float64("float", testFloat),
attribute.Int("int", testInt),
attribute.Bool("bool", testBool),
attribute.String("string", testString),
attribute.String("string", testString),
attribute.Float64("float", testFloat),
attribute.Int("int", testInt),
attribute.Bool("bool", testBool),
attribute.String("string", testString),
log.String("string", testString),
log.Float64("float", testFloat),
log.Int("int", testInt),
log.Bool("bool", testBool),
log.String("string", testString),
log.String("string", testString),
log.Float64("float", testFloat),
log.Int("int", testInt),
log.Bool("bool", testBool),
log.String("string", testString),
log.String("string", testString),
log.Float64("float", testFloat),
log.Int("int", testInt),
log.Bool("bool", testBool),
log.String("string", testString),
log.String("string", testString),
log.Float64("float", testFloat),
log.Int("int", testInt),
log.Bool("bool", testBool),
log.String("string", testString),
log.String("string", testString),
log.Float64("float", testFloat),
log.Int("int", testInt),
log.Bool("bool", testBool),
log.String("string", testString),
log.String("string", testString),
log.Float64("float", testFloat),
log.Int("int", testInt),
log.Bool("bool", testBool),
log.String("string", testString),
log.String("string", testString),
log.Float64("float", testFloat),
log.Int("int", testInt),
log.Bool("bool", testBool),
log.String("string", testString),
log.String("string", testString),
log.Float64("float", testFloat),
log.Int("int", testInt),
log.Bool("bool", testBool),
log.String("string", testString),
)
tc.logger.Emit(ctx, r)
},
Expand Down Expand Up @@ -199,13 +199,13 @@ func BenchmarkSlog(b *testing.B) {
{
"no attrs",
func() {
logger.LogAttrs(ctx, slog.LevelInfo, testBody)
logger.LogAttrs(ctx, slog.LevelInfo, testBodyString)
},
},
{
"3 attrs",
func() {
logger.LogAttrs(ctx, slog.LevelInfo, testBody,
logger.LogAttrs(ctx, slog.LevelInfo, testBodyString,
slog.String("string", testString),
slog.Float64("float", testFloat),
slog.Int("int", testInt),
Expand All @@ -215,7 +215,7 @@ func BenchmarkSlog(b *testing.B) {
{
"5 attrs",
func() {
logger.LogAttrs(ctx, slog.LevelInfo, testBody,
logger.LogAttrs(ctx, slog.LevelInfo, testBodyString,
slog.String("string", testString),
slog.Float64("float", testFloat),
slog.Int("int", testInt),
Expand All @@ -227,7 +227,7 @@ func BenchmarkSlog(b *testing.B) {
{
"10 attrs",
func() {
logger.LogAttrs(ctx, slog.LevelInfo, testBody,
logger.LogAttrs(ctx, slog.LevelInfo, testBodyString,
slog.String("string", testString),
slog.Float64("float", testFloat),
slog.Int("int", testInt),
Expand All @@ -244,7 +244,7 @@ func BenchmarkSlog(b *testing.B) {
{
"40 attrs",
func() {
logger.LogAttrs(ctx, slog.LevelInfo, testBody,
logger.LogAttrs(ctx, slog.LevelInfo, testBodyString,
slog.String("string", testString),
slog.Float64("float", testFloat),
slog.Int("int", testInt),
Expand Down Expand Up @@ -309,13 +309,13 @@ func BenchmarkLogr(b *testing.B) {
{
"no attrs",
func() {
logger.Info(testBody)
logger.Info(testBodyString)
},
},
{
"3 attrs",
func() {
logger.Info(testBody,
logger.Info(testBodyString,
"string", testString,
"float", testFloat,
"int", testInt,
Expand All @@ -329,7 +329,7 @@ func BenchmarkLogr(b *testing.B) {
// should only be from strconv used in writerLogger.
"5 attrs",
func() {
logger.Info(testBody,
logger.Info(testBodyString,
"string", testString,
"float", testFloat,
"int", testInt,
Expand All @@ -341,7 +341,7 @@ func BenchmarkLogr(b *testing.B) {
{
"10 attrs",
func() {
logger.Info(testBody,
logger.Info(testBodyString,
"string", testString,
"float", testFloat,
"int", testInt,
Expand All @@ -358,7 +358,7 @@ func BenchmarkLogr(b *testing.B) {
{
"40 attrs",
func() {
logger.Info(testBody,
logger.Info(testBodyString,
"string", testString,
"float", testFloat,
"int", testInt,
Expand Down
2 changes: 1 addition & 1 deletion log/internal/go.mod
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,6 @@ go 1.20
require (
github.com/go-logr/logr v1.4.1
github.com/stretchr/testify v1.8.4
go.opentelemetry.io/otel v1.23.0-rc.1
go.opentelemetry.io/otel/log v0.0.0-00010101000000-000000000000
go.opentelemetry.io/otel/trace v1.23.0-rc.1
golang.org/x/exp v0.0.0-20231127185646-65229373498e
Expand All @@ -15,6 +14,7 @@ require (
github.com/davecgh/go-spew v1.1.1 // indirect
github.com/go-logr/stdr v1.2.2 // indirect
github.com/pmezard/go-difflib v1.0.0 // indirect
go.opentelemetry.io/otel v1.23.0-rc.1 // indirect
go.opentelemetry.io/otel/metric v1.23.0-rc.1 // indirect
gopkg.in/yaml.v3 v3.0.1 // indirect
)
Expand Down
13 changes: 6 additions & 7 deletions log/internal/logr.go
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,6 @@ import (

"github.com/go-logr/logr"

"go.opentelemetry.io/otel/attribute"
"go.opentelemetry.io/otel/log"
)

Expand All @@ -31,7 +30,7 @@ func (s *logrSink) Enabled(level int) bool {
func (s *logrSink) Info(level int, msg string, keysAndValues ...any) {
record := log.Record{}

record.SetBody(msg)
record.SetBody(log.StringValue(msg))

lvl := log.Severity(9 - level)
record.SetSeverity(lvl)
Expand Down Expand Up @@ -73,16 +72,16 @@ func (s *logrSink) WithName(name string) logr.LogSink {
return s
}

func convertKV(k string, v interface{}) attribute.KeyValue {
func convertKV(k string, v interface{}) log.KeyValue {
switch val := v.(type) {
case bool:
return attribute.Bool(k, val)
return log.Bool(k, val)
case float64:
return attribute.Float64(k, val)
return log.Float64(k, val)
case int:
return attribute.Int(k, val)
return log.Int(k, val)
case string:
return attribute.String(k, val)
return log.String(k, val)
default:
panic(fmt.Sprintf("unhandled value type: %T", val))
}
Expand Down
9 changes: 4 additions & 5 deletions log/internal/logr_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,6 @@ import (
"github.com/go-logr/logr"
"github.com/stretchr/testify/assert"

"go.opentelemetry.io/otel/attribute"
"go.opentelemetry.io/otel/log"
)

Expand All @@ -18,19 +17,19 @@ func TestLogrSink(t *testing.T) {

l := logr.New(&logrSink{spy})

l.Info(testBody, "string", testString, "ctx", ctx)
l.Info(testBodyString, "string", testString, "ctx", ctx)

want := log.Record{}
want.SetBody(testBody)
want.SetSeverity(log.SeverityInfo)
want.AddAttributes(attribute.String("string", testString))
want.AddAttributes(log.String("string", testString))

assert.Equal(t, testBody, spy.Record.Body())
assert.Equal(t, log.SeverityInfo, spy.Record.Severity())
assert.Equal(t, 1, spy.Record.AttributesLen())
spy.Record.WalkAttributes(func(kv attribute.KeyValue) bool {
spy.Record.WalkAttributes(func(kv log.KeyValue) bool {
assert.Equal(t, "string", string(kv.Key))
assert.Equal(t, testString, kv.Value.AsString())
assert.Equal(t, testString, kv.Value.String())
return true
})
}
Loading