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: Fix counting number of dropped attributes of log Record #5464

Merged
merged 9 commits into from
Jun 4, 2024
Merged
1 change: 1 addition & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -21,6 +21,7 @@ This project adheres to [Semantic Versioning](https://semver.org/spec/v2.0.0.htm
- Log a warning to the OpenTelemetry internal logger when a `Span` in `go.opentelemetry.io/otel/sdk/trace` drops an attribute, event, or link due to a limit being reached. (#5434)
- Document instrument name requirements in `go.opentelemetry.io/otel/metric`. (#5435)
- Prevent random number generation data-race for experimental rand exemplars in `go.opentelemetry.io/otel/sdk/metric`. (#5456)
- Fix counting number of dropped attributes of `Record` in `go.opentelemetry.io/otel/sdk/log`. (#5464)

## [1.27.0/0.49.0/0.3.0] 2024-05-21

Expand Down
6 changes: 2 additions & 4 deletions sdk/log/record.go
Original file line number Diff line number Diff line change
Expand Up @@ -275,8 +275,6 @@ func (r *Record) addAttrs(attrs []log.KeyValue) {

// SetAttributes sets (and overrides) attributes to the log record.
func (r *Record) SetAttributes(attrs ...log.KeyValue) {
// TODO: apply truncation to string and []string values.
// TODO: deduplicate map values.
var drop int
attrs, drop = dedup(attrs)
r.setDropped(drop)
Expand Down Expand Up @@ -391,12 +389,12 @@ func (r *Record) Clone() Record {
return res
}

func (r Record) applyAttrLimits(attr log.KeyValue) log.KeyValue {
func (r *Record) applyAttrLimits(attr log.KeyValue) log.KeyValue {
attr.Value = r.applyValueLimits(attr.Value)
return attr
}

func (r Record) applyValueLimits(val log.Value) log.Value {
func (r *Record) applyValueLimits(val log.Value) log.Value {
switch val.Kind() {
case log.KindString:
s := val.AsString()
Expand Down
14 changes: 14 additions & 0 deletions sdk/log/record_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -349,6 +349,8 @@ func TestApplyAttrLimitsDeduplication(t *testing.T) {
name string
limit int
input, want log.Value

droppedAttrs int
amanakin marked this conversation as resolved.
Show resolved Hide resolved
}{
{
// No de-duplication
Expand Down Expand Up @@ -419,6 +421,7 @@ func TestApplyAttrLimitsDeduplication(t *testing.T) {
log.String("g", "GG"),
log.String("h", "H"),
),
droppedAttrs: 10,
},
}

Expand All @@ -431,11 +434,13 @@ func TestApplyAttrLimitsDeduplication(t *testing.T) {
t.Run("AddAttributes", func(t *testing.T) {
r.AddAttributes(kv)
assertKV(t, r, log.KeyValue{Key: key, Value: tc.want})
require.Equal(t, tc.droppedAttrs, r.DroppedAttributes())
amanakin marked this conversation as resolved.
Show resolved Hide resolved
})

t.Run("SetAttributes", func(t *testing.T) {
r.SetAttributes(kv)
assertKV(t, r, log.KeyValue{Key: key, Value: tc.want})
require.Equal(t, tc.droppedAttrs, r.DroppedAttributes())
amanakin marked this conversation as resolved.
Show resolved Hide resolved
})
})
}
Expand Down Expand Up @@ -635,3 +640,12 @@ func TestTruncate(t *testing.T) {
})
}
}

func BenchmarkSetAddAttributes(b *testing.B) {
for i := 0; i < b.N; i++ {
kv := log.String("key", "value")
var r Record
r.SetAttributes(kv)
r.AddAttributes(kv)
}
amanakin marked this conversation as resolved.
Show resolved Hide resolved
}