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

sdk/log: Add TestRecord #5200

Closed
wants to merge 7 commits into from
Closed
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
1 change: 1 addition & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -12,6 +12,7 @@ This project adheres to [Semantic Versioning](https://semver.org/spec/v2.0.0.htm

- Add `Recorder` in `go.opentelemetry.io/otel/log/logtest` to facilitate testing the log bridge implementations. (#5134)
- Add span flags to OTLP spans and links exported by `go.opentelemetry.io/otel/exporters/otlp/otlptrace`. (#5194)
- Add `TestRecord` in `go.opentelemetry.io/otel/sdk/log` to facilitate testing the `Processor` and `Exporter` implementations. (#5200)

### Changed

Expand Down
13 changes: 9 additions & 4 deletions exporters/stdout/stdoutlog/exporter_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -14,8 +14,11 @@ 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/sdk/instrumentation"
sdklog "go.opentelemetry.io/otel/sdk/log"
"go.opentelemetry.io/otel/sdk/resource"
"go.opentelemetry.io/otel/trace"
)

Expand Down Expand Up @@ -174,7 +177,7 @@ func TestExporterExport(t *testing.T) {
func getJSON(now time.Time) string {
serializedNow, _ := json.Marshal(now)

return "{\"Timestamp\":" + string(serializedNow) + ",\"ObservedTimestamp\":" + string(serializedNow) + ",\"Severity\":9,\"SeverityText\":\"INFO\",\"Body\":{},\"Attributes\":[{\"Key\":\"key\",\"Value\":{}},{\"Key\":\"key2\",\"Value\":{}},{\"Key\":\"key3\",\"Value\":{}},{\"Key\":\"key4\",\"Value\":{}},{\"Key\":\"key5\",\"Value\":{}},{\"Key\":\"bool\",\"Value\":{}}],\"TraceID\":\"0102030405060708090a0b0c0d0e0f10\",\"SpanID\":\"0102030405060708\",\"TraceFlags\":\"01\",\"Resource\":{},\"Scope\":{\"Name\":\"\",\"Version\":\"\",\"SchemaURL\":\"\"},\"AttributeValueLengthLimit\":0,\"AttributeCountLimit\":0}\n"
return "{\"Timestamp\":" + string(serializedNow) + ",\"ObservedTimestamp\":" + string(serializedNow) + ",\"Severity\":9,\"SeverityText\":\"INFO\",\"Body\":{},\"Attributes\":[{\"Key\":\"key\",\"Value\":{}},{\"Key\":\"key2\",\"Value\":{}},{\"Key\":\"key3\",\"Value\":{}},{\"Key\":\"key4\",\"Value\":{}},{\"Key\":\"key5\",\"Value\":{}},{\"Key\":\"bool\",\"Value\":{}}],\"TraceID\":\"0102030405060708090a0b0c0d0e0f10\",\"SpanID\":\"0102030405060708\",\"TraceFlags\":\"01\",\"Resource\":{},\"Scope\":{\"Name\":\"testing\",\"Version\":\"\",\"SchemaURL\":\"\"},\"AttributeValueLengthLimit\":0,\"AttributeCountLimit\":0}\n"
}

func getJSONs(now time.Time) string {
Expand Down Expand Up @@ -221,7 +224,7 @@ func getPrettyJSON(now time.Time) string {
"TraceFlags": "01",
"Resource": {},
"Scope": {
"Name": "",
"Name": "testing",
"Version": "",
"SchemaURL": ""
},
Expand Down Expand Up @@ -254,7 +257,7 @@ func getRecord(now time.Time) sdklog.Record {
spanID, _ := trace.SpanIDFromHex("0102030405060708")

// Setup records
record := sdklog.Record{}
record := sdklog.TestRecord{}
record.SetTimestamp(now)
record.SetObservedTimestamp(now)
record.SetSeverity(log.SeverityInfo1)
Expand All @@ -272,8 +275,10 @@ func getRecord(now time.Time) sdklog.Record {
record.SetTraceID(traceID)
record.SetSpanID(spanID)
record.SetTraceFlags(trace.FlagsSampled)
record.SetInstrumentationScope(instrumentation.Scope{Name: "testing"})
record.SetResource(resource.NewSchemaless(attribute.Bool("key", true)))

return record
return record.Record
}

func TestExporterConcurrentSafe(t *testing.T) {
Expand Down
2 changes: 1 addition & 1 deletion exporters/stdout/stdoutlog/go.mod
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@ go 1.21

require (
github.com/stretchr/testify v1.9.0
go.opentelemetry.io/otel v1.25.0
go.opentelemetry.io/otel/log v0.1.0-alpha
go.opentelemetry.io/otel/sdk v1.25.0
go.opentelemetry.io/otel/sdk/log v0.0.0
Expand All @@ -15,7 +16,6 @@ require (
github.com/go-logr/logr v1.4.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.25.0 // indirect
go.opentelemetry.io/otel/metric v1.25.0 // indirect
golang.org/x/sys v0.19.0 // indirect
gopkg.in/yaml.v3 v3.0.1 // indirect
Expand Down
17 changes: 17 additions & 0 deletions sdk/log/record.go
Original file line number Diff line number Diff line change
Expand Up @@ -233,3 +233,20 @@ func (r *Record) Clone() Record {
res.back = slices.Clone(r.back)
return res
}

// TestRecord is a log record for testing.
// You can use it for unit testing [Processor] and [Exporter] implementations.
// Do not use TestRecord in production code.
type TestRecord struct {
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

How do I get a Record from a TestRecord so I can use it for testing?

Copy link
Member Author

@pellared pellared Apr 13, 2024

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

.Record. it is used in stdoutlog test in this PR

Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

This seems to be polluting the API with testing types.

It also is trying to use a Test prefix here to prevent semantic API use. I would rather explore options that use the semantics of Go actually make the restriction by design.

Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I propose this as an intermediate solution as noted in the description.

Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I would rather we work on an permanent and intentional solution instead of a stop gap.

Record
}

// SetResource sets resource for testing.
func (r *TestRecord) SetResource(res *resource.Resource) {
r.resource = res
}

// SetInstrumentationScope sets the scope for testing.
func (r *TestRecord) SetInstrumentationScope(s instrumentation.Scope) {
r.scope = &s
}
19 changes: 19 additions & 0 deletions sdk/log/record_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -204,3 +204,22 @@ func TestRecordClone(t *testing.T) {
return assert.Truef(t, kv.Equal(attr1), "%v != %v", kv, attr1)
})
}

func TestTestRecordResource(t *testing.T) {
r := new(TestRecord)
assert.NotPanics(t, func() { r.Resource() })

res := resource.NewSchemaless(attribute.Bool("key", true))
r.SetResource(res)
got := r.Resource()
assert.True(t, res.Equal(&got))
}

func TestTestRecordInstrumentationScope(t *testing.T) {
r := new(TestRecord)
assert.NotPanics(t, func() { r.InstrumentationScope() })

scope := instrumentation.Scope{Name: "testing"}
r.SetInstrumentationScope(scope)
assert.Equal(t, scope, r.InstrumentationScope())
}