Skip to content

Commit

Permalink
sdk/log/logtest: Add RecordBuilder
Browse files Browse the repository at this point in the history
  • Loading branch information
pellared committed Apr 19, 2024
1 parent 1f76264 commit 1bf980b
Show file tree
Hide file tree
Showing 3 changed files with 197 additions and 0 deletions.
3 changes: 3 additions & 0 deletions sdk/log/logtest/README.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
# Log Test SDK

[![PkgGoDev](https://pkg.go.dev/badge/go.opentelemetry.io/otel/sdk/log/logtest)](https://pkg.go.dev/go.opentelemetry.io/otel/sdk/log/logtest)
131 changes: 131 additions & 0 deletions sdk/log/logtest/builder.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,131 @@
// Copyright The OpenTelemetry Authors
// SPDX-License-Identifier: Apache-2.0

package logtest // import "go.opentelemetry.io/otel/sdk/log/logtest"

import (
"context"
"slices"
"time"

"go.opentelemetry.io/otel/log"
"go.opentelemetry.io/otel/sdk/instrumentation"
sdklog "go.opentelemetry.io/otel/sdk/log"
"go.opentelemetry.io/otel/sdk/resource"
)

// TODO: comment.
type RecordBuilder struct {
timestamp time.Time
// TODO: implement all fields
// observedTimestamp time.Time
// severity log.Severity
// severityText string
// body log.Value
attrs []log.KeyValue
// traceID trace.TraceID
// spanID trace.SpanID
// traceFlags trace.TraceFlags

resource *resource.Resource
scope instrumentation.Scope

dropped int
}

// TODO: comment.
func (b RecordBuilder) Record() sdklog.Record {
var record sdklog.Record
p := processor(func(r sdklog.Record) {
r.SetTimestamp(b.timestamp)
r.SetAttributes(b.attrs...)

// Generate dropped attributes.
for i := 0; i < b.dropped; i++ {
r.AddAttributes(log.KeyValue{})
}

record = r
})

attributeCountLimit := -1
if b.dropped > 0 {
// Make sure that we can generate dropped attributes.
attributeCountLimit = len(b.attrs)
}

provider := sdklog.NewLoggerProvider(
sdklog.WithResource(b.resource),
sdklog.WithAttributeCountLimit(attributeCountLimit),
sdklog.WithAttributeValueLengthLimit(-1),
sdklog.WithProcessor(p),
)

l := provider.Logger(b.scope.Name,
log.WithInstrumentationVersion(b.scope.Version),
log.WithSchemaURL(b.scope.SchemaURL),
)
l.Emit(context.Background(), log.Record{}) // This executes the processor function.
return record
}

// TODO: comment.
func (b RecordBuilder) SetTimestamp(t time.Time) RecordBuilder {
b.timestamp = t
return b
}

// TODO: comment.
func (b RecordBuilder) AddAttributes(attrs ...log.KeyValue) RecordBuilder {
b.attrs = slices.Clone(b.attrs)
b.attrs = append(b.attrs, attrs...)
return b
}

// TODO: comment.
func (b RecordBuilder) SetAttributes(attrs ...log.KeyValue) RecordBuilder {
b.attrs = slices.Clone(attrs)
return b
}

// TODO: comment.
func (b RecordBuilder) SetInstrumentationScope(scope instrumentation.Scope) RecordBuilder {
b.scope = scope
return b
}

// TODO: comment.
func (b RecordBuilder) SetResource(r *resource.Resource) RecordBuilder {
b.resource = r
return b
}

// TODO: comment.
//
// Notice: The returned record is going to have an attribute count limit.
// Therefore, it will not be possible to add additional attributes on the record
// returned by the builder that has dropped attributes set to a value greater than 0
// (they will be dropped).
func (b RecordBuilder) SetDroppedAttributes(n int) RecordBuilder {
b.dropped = n
return b
}

type processor func(r sdklog.Record)

func (p processor) OnEmit(ctx context.Context, r sdklog.Record) error {
p(r)
return nil
}

func (processor) Enabled(context.Context, sdklog.Record) bool {
return true

Check warning on line 122 in sdk/log/logtest/builder.go

View check run for this annotation

Codecov / codecov/patch

sdk/log/logtest/builder.go#L121-L122

Added lines #L121 - L122 were not covered by tests
}

func (processor) Shutdown(ctx context.Context) error {
return nil

Check warning on line 126 in sdk/log/logtest/builder.go

View check run for this annotation

Codecov / codecov/patch

sdk/log/logtest/builder.go#L125-L126

Added lines #L125 - L126 were not covered by tests
}

func (processor) ForceFlush(context.Context) error {
return nil

Check warning on line 130 in sdk/log/logtest/builder.go

View check run for this annotation

Codecov / codecov/patch

sdk/log/logtest/builder.go#L129-L130

Added lines #L129 - L130 were not covered by tests
}
63 changes: 63 additions & 0 deletions sdk/log/logtest/builder_test.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,63 @@
// Copyright The OpenTelemetry Authors
// SPDX-License-Identifier: Apache-2.0

package logtest

import (
"testing"
"time"

"github.com/stretchr/testify/assert"

"go.opentelemetry.io/otel/attribute"
"go.opentelemetry.io/otel/log"
"go.opentelemetry.io/otel/sdk/instrumentation"
"go.opentelemetry.io/otel/sdk/resource"
)

func TestRecordBuilder(t *testing.T) {
now := time.Now()
attrs := []log.KeyValue{
log.Int("int", 1),
log.String("str", "foo"),
log.Float64("flt", 3.14),
}
dropped := 3
scope := instrumentation.Scope{
Name: t.Name(),
}
r := resource.NewSchemaless(attribute.Bool("works", true))

b := RecordBuilder{}.
SetTimestamp(now).
SetAttributes(attrs...).
SetDroppedAttributes(dropped).
SetInstrumentationScope(scope).
SetResource(r)

got := b.Record()
var gotAttrs []log.KeyValue
got.WalkAttributes(func(kv log.KeyValue) bool {
gotAttrs = append(gotAttrs, kv)
return true
})

assert.Equal(t, now, got.Timestamp())
assert.Equal(t, attrs, gotAttrs)
assert.Equal(t, dropped, got.DroppedAttributes())
assert.Equal(t, scope, got.InstrumentationScope())
assert.Equal(t, *r, got.Resource())

got = b.AddAttributes(log.Bool("added", true)).Record()
gotAttrs = nil
got.WalkAttributes(func(kv log.KeyValue) bool {
gotAttrs = append(gotAttrs, kv)
return true
})

assert.Equal(t, now, got.Timestamp())
assert.Equal(t, append(attrs, log.Bool("added", true)), gotAttrs)
assert.Equal(t, dropped, got.DroppedAttributes())
assert.Equal(t, scope, got.InstrumentationScope())
assert.Equal(t, *r, got.Resource())
}

0 comments on commit 1bf980b

Please sign in to comment.