1
0
mirror of https://github.com/open-telemetry/opentelemetry-go.git synced 2025-07-15 01:04:25 +02:00

The stdoutlog exporter prints DroppedAttributes field instead of Limits fields (#5272)

* Add tests for resource, scope, dropped attributes

* Update CHANGELOG

* Apply suggestions from code review

---------

Co-authored-by: Tyler Yahn <MrAlias@users.noreply.github.com>
This commit is contained in:
Sam Xie
2024-04-30 10:26:26 -07:00
committed by GitHub
parent 1980e0e356
commit d5945a9dba
4 changed files with 62 additions and 41 deletions

View File

@ -21,6 +21,7 @@ This project adheres to [Semantic Versioning](https://semver.org/spec/v2.0.0.htm
- The `ForceFlush` and `Shutdown` methods of the exporter returned by `New` in `go.opentelemetry.io/otel/exporters/stdout/stdoutmetric` ignore the context cancellation and always return `nil`. (#5189) - The `ForceFlush` and `Shutdown` methods of the exporter returned by `New` in `go.opentelemetry.io/otel/exporters/stdout/stdoutmetric` ignore the context cancellation and always return `nil`. (#5189)
- Apply the value length limits to `Record` attributes in `go.opentelemetry.io/otel/sdk/log`. (#5230) - Apply the value length limits to `Record` attributes in `go.opentelemetry.io/otel/sdk/log`. (#5230)
- De-duplicate map attributes added to a `Record` in `go.opentelemetry.io/otel/sdk/log`. (#5230) - De-duplicate map attributes added to a `Record` in `go.opentelemetry.io/otel/sdk/log`. (#5230)
- The `go.opentelemetry.io/otel/exporters/stdout/stdoutlog` exporter won't print `AttributeValueLengthLimit` and `AttributeCountLimit` fields now, instead it prints the `DroppedAttributes` field. (#5272)
## [1.26.0/0.48.0/0.2.0-alpha] 2024-04-24 ## [1.26.0/0.48.0/0.2.0-alpha] 2024-04-24

View File

@ -11,6 +11,11 @@ import (
"testing" "testing"
"time" "time"
"go.opentelemetry.io/otel/attribute"
"go.opentelemetry.io/otel/sdk/instrumentation"
"go.opentelemetry.io/otel/sdk/log/logtest"
"go.opentelemetry.io/otel/sdk/resource"
"github.com/stretchr/testify/assert" "github.com/stretchr/testify/assert"
"github.com/stretchr/testify/require" "github.com/stretchr/testify/require"
@ -178,7 +183,7 @@ func getJSON(now *time.Time) string {
timestamps = "\"Timestamp\":" + string(serializedNow) + ",\"ObservedTimestamp\":" + string(serializedNow) + "," timestamps = "\"Timestamp\":" + string(serializedNow) + ",\"ObservedTimestamp\":" + string(serializedNow) + ","
} }
return "{" + timestamps + "\"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\":null,\"Scope\":{\"Name\":\"\",\"Version\":\"\",\"SchemaURL\":\"\"},\"AttributeValueLengthLimit\":0,\"AttributeCountLimit\":0}\n" return "{" + timestamps + "\"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\":[{\"Key\":\"foo\",\"Value\":{\"Type\":\"STRING\",\"Value\":\"bar\"}}],\"Scope\":{\"Name\":\"name\",\"Version\":\"version\",\"SchemaURL\":\"https://example.com/custom-schema\"},\"DroppedAttributes\":10}\n"
} }
func getJSONs(now *time.Time) string { func getJSONs(now *time.Time) string {
@ -225,14 +230,21 @@ func getPrettyJSON(now *time.Time) string {
"TraceID": "0102030405060708090a0b0c0d0e0f10", "TraceID": "0102030405060708090a0b0c0d0e0f10",
"SpanID": "0102030405060708", "SpanID": "0102030405060708",
"TraceFlags": "01", "TraceFlags": "01",
"Resource": null, "Resource": [
{
"Key": "foo",
"Value": {
"Type": "STRING",
"Value": "bar"
}
}
],
"Scope": { "Scope": {
"Name": "", "Name": "name",
"Version": "", "Version": "version",
"SchemaURL": "" "SchemaURL": "https://example.com/custom-schema"
}, },
"AttributeValueLengthLimit": 0, "DroppedAttributes": 10
"AttributeCountLimit": 0
} }
` `
} }
@ -259,14 +271,13 @@ func getRecord(now time.Time) sdklog.Record {
traceID, _ := trace.TraceIDFromHex("0102030405060708090a0b0c0d0e0f10") traceID, _ := trace.TraceIDFromHex("0102030405060708090a0b0c0d0e0f10")
spanID, _ := trace.SpanIDFromHex("0102030405060708") spanID, _ := trace.SpanIDFromHex("0102030405060708")
// Setup records rf := logtest.RecordFactory{
record := sdklog.Record{} Timestamp: now,
record.SetTimestamp(now) ObservedTimestamp: now,
record.SetObservedTimestamp(now) Severity: log.SeverityInfo1,
record.SetSeverity(log.SeverityInfo1) SeverityText: "INFO",
record.SetSeverityText("INFO") Body: log.StringValue("test"),
record.SetBody(log.StringValue("test")) Attributes: []log.KeyValue{
record.SetAttributes([]log.KeyValue{
// More than 5 attributes to test back slice // More than 5 attributes to test back slice
log.String("key", "value"), log.String("key", "value"),
log.String("key2", "value"), log.String("key2", "value"),
@ -274,12 +285,20 @@ func getRecord(now time.Time) sdklog.Record {
log.String("key4", "value"), log.String("key4", "value"),
log.String("key5", "value"), log.String("key5", "value"),
log.Bool("bool", true), log.Bool("bool", true),
}...) },
record.SetTraceID(traceID) TraceID: traceID,
record.SetSpanID(spanID) SpanID: spanID,
record.SetTraceFlags(trace.FlagsSampled) TraceFlags: trace.FlagsSampled,
return record Resource: resource.NewWithAttributes(
"https://example.com/custom-resource-schema",
attribute.String("foo", "bar"),
),
InstrumentationScope: instrumentation.Scope{Name: "name", Version: "version", SchemaURL: "https://example.com/custom-schema"},
DroppedAttributes: 10,
}
return rf.NewRecord()
} }
func TestExporterConcurrentSafe(t *testing.T) { func TestExporterConcurrentSafe(t *testing.T) {

View File

@ -4,6 +4,7 @@ go 1.21
require ( require (
github.com/stretchr/testify v1.9.0 github.com/stretchr/testify v1.9.0
go.opentelemetry.io/otel v1.26.0
go.opentelemetry.io/otel/log v0.2.0-alpha go.opentelemetry.io/otel/log v0.2.0-alpha
go.opentelemetry.io/otel/sdk v1.26.0 go.opentelemetry.io/otel/sdk v1.26.0
go.opentelemetry.io/otel/sdk/log v0.2.0-alpha go.opentelemetry.io/otel/sdk/log v0.2.0-alpha
@ -15,7 +16,6 @@ require (
github.com/go-logr/logr v1.4.1 // indirect github.com/go-logr/logr v1.4.1 // indirect
github.com/go-logr/stdr v1.2.2 // indirect github.com/go-logr/stdr v1.2.2 // indirect
github.com/pmezard/go-difflib v1.0.0 // indirect github.com/pmezard/go-difflib v1.0.0 // indirect
go.opentelemetry.io/otel v1.26.0 // indirect
go.opentelemetry.io/otel/metric v1.26.0 // indirect go.opentelemetry.io/otel/metric v1.26.0 // indirect
golang.org/x/sys v0.19.0 // indirect golang.org/x/sys v0.19.0 // indirect
gopkg.in/yaml.v3 v3.0.1 // indirect gopkg.in/yaml.v3 v3.0.1 // indirect

View File

@ -26,8 +26,7 @@ type recordJSON struct {
TraceFlags trace.TraceFlags TraceFlags trace.TraceFlags
Resource *resource.Resource Resource *resource.Resource
Scope instrumentation.Scope Scope instrumentation.Scope
AttributeValueLengthLimit int DroppedAttributes int
AttributeCountLimit int
} }
func (e *Exporter) newRecordJSON(r sdklog.Record) recordJSON { func (e *Exporter) newRecordJSON(r sdklog.Record) recordJSON {
@ -45,6 +44,8 @@ func (e *Exporter) newRecordJSON(r sdklog.Record) recordJSON {
Resource: &res, Resource: &res,
Scope: r.InstrumentationScope(), Scope: r.InstrumentationScope(),
DroppedAttributes: r.DroppedAttributes(),
} }
r.WalkAttributes(func(kv log.KeyValue) bool { r.WalkAttributes(func(kv log.KeyValue) bool {