Skip to content

Commit

Permalink
sdk/log: Fix counting number of dropped attributes of log.Record (#5464)
Browse files Browse the repository at this point in the history
Applying attribute limits in `Record` uses value receiver.
But it should add count of dropped attrs.
In this PR I add using of pointer receiver.

Also it's slightly faster with pointer receiver:

```
goos: darwin
goarch: arm64
pkg: go.opentelemetry.io/otel/sdk/log
                    │   old.txt   │              new.txt               │
                    │   sec/op    │   sec/op     vs base               │
SetAddAttributes-10   175.7n ± 3%   159.8n ± 4%  -9.08% (p=0.000 n=10)

                    │  old.txt   │            new.txt             │
                    │    B/op    │    B/op     vs base            │
SetAddAttributes-10   48.00 ± 0%   48.00 ± 0%  ~ (p=1.000 n=10) ¹
¹ all samples are equal

                    │  old.txt   │            new.txt             │
                    │ allocs/op  │ allocs/op   vs base            │
SetAddAttributes-10   1.000 ± 0%   1.000 ± 0%  ~ (p=1.000 n=10) ¹
¹ all samples are equal
```
  • Loading branch information
amanakin authored Jun 4, 2024
1 parent 0ba6336 commit 4fc7162
Show file tree
Hide file tree
Showing 3 changed files with 22 additions and 7 deletions.
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
22 changes: 19 additions & 3 deletions sdk/log/record_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -346,9 +346,10 @@ func TestRecordAttrDeduplication(t *testing.T) {

func TestApplyAttrLimitsDeduplication(t *testing.T) {
testcases := []struct {
name string
limit int
input, want log.Value
name string
limit int
input, want log.Value
wantDroppedAttrs int
}{
{
// No de-duplication
Expand Down Expand Up @@ -419,6 +420,7 @@ func TestApplyAttrLimitsDeduplication(t *testing.T) {
log.String("g", "GG"),
log.String("h", "H"),
),
wantDroppedAttrs: 10,
},
}

Expand All @@ -431,11 +433,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})
assert.Equal(t, tc.wantDroppedAttrs, r.DroppedAttributes())
})

t.Run("SetAttributes", func(t *testing.T) {
r.SetAttributes(kv)
assertKV(t, r, log.KeyValue{Key: key, Value: tc.want})
assert.Equal(t, tc.wantDroppedAttrs, r.DroppedAttributes())
})
})
}
Expand Down Expand Up @@ -635,3 +639,15 @@ func TestTruncate(t *testing.T) {
})
}
}

func BenchmarkSetAddAttributes(b *testing.B) {
kv := log.String("key", "value")
records := make([]Record, b.N)

b.ReportAllocs()
b.ResetTimer()
for i := 0; i < b.N; i++ {
records[i].SetAttributes(kv)
records[i].AddAttributes(kv)
}
}

0 comments on commit 4fc7162

Please sign in to comment.