Skip to content

Commit 3495d74

Browse files
paivagustavojmacd
authored andcommitted
specialize simplelru(span's lruMap) for attributes only (#328)
* copy of simplelru for attributes without excessive usage of interface{} This also make sure we only add attributes/links if isRecording. Move method to export the attributes list to attributeMap. * run make precommit to update mod files.
1 parent eb3b31e commit 3495d74

File tree

12 files changed

+99
-75
lines changed

12 files changed

+99
-75
lines changed

example/basic/go.sum

-2
Original file line numberDiff line numberDiff line change
@@ -95,8 +95,6 @@ github.com/gostaticanalysis/analysisutil v0.0.3/go.mod h1:eEOZF4jCKGi+aprrirO9e7
9595
github.com/grpc-ecosystem/go-grpc-middleware v1.0.0/go.mod h1:FiyG127CGDf3tlThmgyCl78X/SZQqEOJBCDaAfeWzPs=
9696
github.com/grpc-ecosystem/go-grpc-prometheus v1.2.0/go.mod h1:8NvIoxWQoOIhqOTXgfV/d3M/q6VIi02HzZEHgUlZvzk=
9797
github.com/grpc-ecosystem/grpc-gateway v1.9.0/go.mod h1:vNeuVxBJEsws4ogUvrchl83t/GYV9WGTSLVdBhOQFDY=
98-
github.com/hashicorp/golang-lru v0.5.3 h1:YPkqC67at8FYaadspW/6uE0COsBxS2656RLEr8Bppgk=
99-
github.com/hashicorp/golang-lru v0.5.3/go.mod h1:iADmTwqILo4mZ8BN3D2Q6+9jd8WM5uGBxy+E8yxSoD4=
10098
github.com/hashicorp/hcl v1.0.0/go.mod h1:E5yfLk+7swimpb2L/Alb/PJmXilQ/rhwaUYs4T20WEQ=
10199
github.com/hpcloud/tail v1.0.0/go.mod h1:ab1qPbhIpdTxEkNHXyeSf5vhxWSCs/tWer42PpOxQnU=
102100
github.com/inconshreveable/mousetrap v1.0.0/go.mod h1:PxqpIevigyE2G7u3NXJIT2ANytuPF1OarO4DADm73n8=

example/http-stackdriver/go.sum

+1-2
Original file line numberDiff line numberDiff line change
@@ -118,9 +118,8 @@ github.com/grpc-ecosystem/go-grpc-middleware v1.0.0/go.mod h1:FiyG127CGDf3tlThmg
118118
github.com/grpc-ecosystem/go-grpc-prometheus v1.2.0/go.mod h1:8NvIoxWQoOIhqOTXgfV/d3M/q6VIi02HzZEHgUlZvzk=
119119
github.com/grpc-ecosystem/grpc-gateway v1.9.0/go.mod h1:vNeuVxBJEsws4ogUvrchl83t/GYV9WGTSLVdBhOQFDY=
120120
github.com/hashicorp/golang-lru v0.5.0/go.mod h1:/m3WP610KZHVQ1SGc6re/UDhFvYD7pJ4Ao+sR/qLZy8=
121+
github.com/hashicorp/golang-lru v0.5.1 h1:0hERBMJE1eitiLkihrMvRVBYAkpHzc/J3QdDN+dAcgU=
121122
github.com/hashicorp/golang-lru v0.5.1/go.mod h1:/m3WP610KZHVQ1SGc6re/UDhFvYD7pJ4Ao+sR/qLZy8=
122-
github.com/hashicorp/golang-lru v0.5.3 h1:YPkqC67at8FYaadspW/6uE0COsBxS2656RLEr8Bppgk=
123-
github.com/hashicorp/golang-lru v0.5.3/go.mod h1:iADmTwqILo4mZ8BN3D2Q6+9jd8WM5uGBxy+E8yxSoD4=
124123
github.com/hashicorp/hcl v1.0.0/go.mod h1:E5yfLk+7swimpb2L/Alb/PJmXilQ/rhwaUYs4T20WEQ=
125124
github.com/hpcloud/tail v1.0.0/go.mod h1:ab1qPbhIpdTxEkNHXyeSf5vhxWSCs/tWer42PpOxQnU=
126125
github.com/inconshreveable/mousetrap v1.0.0/go.mod h1:PxqpIevigyE2G7u3NXJIT2ANytuPF1OarO4DADm73n8=

example/http/go.sum

-2
Original file line numberDiff line numberDiff line change
@@ -93,8 +93,6 @@ github.com/gostaticanalysis/analysisutil v0.0.3/go.mod h1:eEOZF4jCKGi+aprrirO9e7
9393
github.com/grpc-ecosystem/go-grpc-middleware v1.0.0/go.mod h1:FiyG127CGDf3tlThmgyCl78X/SZQqEOJBCDaAfeWzPs=
9494
github.com/grpc-ecosystem/go-grpc-prometheus v1.2.0/go.mod h1:8NvIoxWQoOIhqOTXgfV/d3M/q6VIi02HzZEHgUlZvzk=
9595
github.com/grpc-ecosystem/grpc-gateway v1.9.0/go.mod h1:vNeuVxBJEsws4ogUvrchl83t/GYV9WGTSLVdBhOQFDY=
96-
github.com/hashicorp/golang-lru v0.5.3 h1:YPkqC67at8FYaadspW/6uE0COsBxS2656RLEr8Bppgk=
97-
github.com/hashicorp/golang-lru v0.5.3/go.mod h1:iADmTwqILo4mZ8BN3D2Q6+9jd8WM5uGBxy+E8yxSoD4=
9896
github.com/hashicorp/hcl v1.0.0/go.mod h1:E5yfLk+7swimpb2L/Alb/PJmXilQ/rhwaUYs4T20WEQ=
9997
github.com/hpcloud/tail v1.0.0 h1:nfCOvKYfkgYP8hkirhJocXT2+zOD8yUNjXaWfTlyFKI=
10098
github.com/hpcloud/tail v1.0.0/go.mod h1:ab1qPbhIpdTxEkNHXyeSf5vhxWSCs/tWer42PpOxQnU=

example/jaeger/go.sum

-2
Original file line numberDiff line numberDiff line change
@@ -103,8 +103,6 @@ github.com/grpc-ecosystem/go-grpc-prometheus v1.2.0/go.mod h1:8NvIoxWQoOIhqOTXgf
103103
github.com/grpc-ecosystem/grpc-gateway v1.9.0/go.mod h1:vNeuVxBJEsws4ogUvrchl83t/GYV9WGTSLVdBhOQFDY=
104104
github.com/hashicorp/golang-lru v0.5.0/go.mod h1:/m3WP610KZHVQ1SGc6re/UDhFvYD7pJ4Ao+sR/qLZy8=
105105
github.com/hashicorp/golang-lru v0.5.1/go.mod h1:/m3WP610KZHVQ1SGc6re/UDhFvYD7pJ4Ao+sR/qLZy8=
106-
github.com/hashicorp/golang-lru v0.5.3 h1:YPkqC67at8FYaadspW/6uE0COsBxS2656RLEr8Bppgk=
107-
github.com/hashicorp/golang-lru v0.5.3/go.mod h1:iADmTwqILo4mZ8BN3D2Q6+9jd8WM5uGBxy+E8yxSoD4=
108106
github.com/hashicorp/hcl v1.0.0/go.mod h1:E5yfLk+7swimpb2L/Alb/PJmXilQ/rhwaUYs4T20WEQ=
109107
github.com/hpcloud/tail v1.0.0/go.mod h1:ab1qPbhIpdTxEkNHXyeSf5vhxWSCs/tWer42PpOxQnU=
110108
github.com/inconshreveable/mousetrap v1.0.0/go.mod h1:PxqpIevigyE2G7u3NXJIT2ANytuPF1OarO4DADm73n8=

example/namedtracer/go.sum

-2
Original file line numberDiff line numberDiff line change
@@ -92,8 +92,6 @@ github.com/gostaticanalysis/analysisutil v0.0.3/go.mod h1:eEOZF4jCKGi+aprrirO9e7
9292
github.com/grpc-ecosystem/go-grpc-middleware v1.0.0/go.mod h1:FiyG127CGDf3tlThmgyCl78X/SZQqEOJBCDaAfeWzPs=
9393
github.com/grpc-ecosystem/go-grpc-prometheus v1.2.0/go.mod h1:8NvIoxWQoOIhqOTXgfV/d3M/q6VIi02HzZEHgUlZvzk=
9494
github.com/grpc-ecosystem/grpc-gateway v1.9.0/go.mod h1:vNeuVxBJEsws4ogUvrchl83t/GYV9WGTSLVdBhOQFDY=
95-
github.com/hashicorp/golang-lru v0.5.3 h1:YPkqC67at8FYaadspW/6uE0COsBxS2656RLEr8Bppgk=
96-
github.com/hashicorp/golang-lru v0.5.3/go.mod h1:iADmTwqILo4mZ8BN3D2Q6+9jd8WM5uGBxy+E8yxSoD4=
9795
github.com/hashicorp/hcl v1.0.0/go.mod h1:E5yfLk+7swimpb2L/Alb/PJmXilQ/rhwaUYs4T20WEQ=
9896
github.com/hpcloud/tail v1.0.0/go.mod h1:ab1qPbhIpdTxEkNHXyeSf5vhxWSCs/tWer42PpOxQnU=
9997
github.com/inconshreveable/mousetrap v1.0.0/go.mod h1:PxqpIevigyE2G7u3NXJIT2ANytuPF1OarO4DADm73n8=

exporter/trace/jaeger/go.sum

-2
Original file line numberDiff line numberDiff line change
@@ -105,8 +105,6 @@ github.com/grpc-ecosystem/go-grpc-prometheus v1.2.0/go.mod h1:8NvIoxWQoOIhqOTXgf
105105
github.com/grpc-ecosystem/grpc-gateway v1.9.0/go.mod h1:vNeuVxBJEsws4ogUvrchl83t/GYV9WGTSLVdBhOQFDY=
106106
github.com/hashicorp/golang-lru v0.5.0/go.mod h1:/m3WP610KZHVQ1SGc6re/UDhFvYD7pJ4Ao+sR/qLZy8=
107107
github.com/hashicorp/golang-lru v0.5.1/go.mod h1:/m3WP610KZHVQ1SGc6re/UDhFvYD7pJ4Ao+sR/qLZy8=
108-
github.com/hashicorp/golang-lru v0.5.3 h1:YPkqC67at8FYaadspW/6uE0COsBxS2656RLEr8Bppgk=
109-
github.com/hashicorp/golang-lru v0.5.3/go.mod h1:iADmTwqILo4mZ8BN3D2Q6+9jd8WM5uGBxy+E8yxSoD4=
110108
github.com/hashicorp/hcl v1.0.0/go.mod h1:E5yfLk+7swimpb2L/Alb/PJmXilQ/rhwaUYs4T20WEQ=
111109
github.com/hpcloud/tail v1.0.0/go.mod h1:ab1qPbhIpdTxEkNHXyeSf5vhxWSCs/tWer42PpOxQnU=
112110
github.com/inconshreveable/mousetrap v1.0.0/go.mod h1:PxqpIevigyE2G7u3NXJIT2ANytuPF1OarO4DADm73n8=

exporter/trace/stackdriver/go.sum

+1-2
Original file line numberDiff line numberDiff line change
@@ -117,9 +117,8 @@ github.com/grpc-ecosystem/go-grpc-middleware v1.0.0/go.mod h1:FiyG127CGDf3tlThmg
117117
github.com/grpc-ecosystem/go-grpc-prometheus v1.2.0/go.mod h1:8NvIoxWQoOIhqOTXgfV/d3M/q6VIi02HzZEHgUlZvzk=
118118
github.com/grpc-ecosystem/grpc-gateway v1.9.0/go.mod h1:vNeuVxBJEsws4ogUvrchl83t/GYV9WGTSLVdBhOQFDY=
119119
github.com/hashicorp/golang-lru v0.5.0/go.mod h1:/m3WP610KZHVQ1SGc6re/UDhFvYD7pJ4Ao+sR/qLZy8=
120+
github.com/hashicorp/golang-lru v0.5.1 h1:0hERBMJE1eitiLkihrMvRVBYAkpHzc/J3QdDN+dAcgU=
120121
github.com/hashicorp/golang-lru v0.5.1/go.mod h1:/m3WP610KZHVQ1SGc6re/UDhFvYD7pJ4Ao+sR/qLZy8=
121-
github.com/hashicorp/golang-lru v0.5.3 h1:YPkqC67at8FYaadspW/6uE0COsBxS2656RLEr8Bppgk=
122-
github.com/hashicorp/golang-lru v0.5.3/go.mod h1:iADmTwqILo4mZ8BN3D2Q6+9jd8WM5uGBxy+E8yxSoD4=
123122
github.com/hashicorp/hcl v1.0.0/go.mod h1:E5yfLk+7swimpb2L/Alb/PJmXilQ/rhwaUYs4T20WEQ=
124123
github.com/hpcloud/tail v1.0.0/go.mod h1:ab1qPbhIpdTxEkNHXyeSf5vhxWSCs/tWer42PpOxQnU=
125124
github.com/inconshreveable/mousetrap v1.0.0/go.mod h1:PxqpIevigyE2G7u3NXJIT2ANytuPF1OarO4DADm73n8=

go.mod

-1
Original file line numberDiff line numberDiff line change
@@ -13,7 +13,6 @@ require (
1313
github.com/google/go-cmp v0.3.1
1414
github.com/google/gofuzz v1.0.0 // indirect
1515
github.com/gostaticanalysis/analysisutil v0.0.3 // indirect
16-
github.com/hashicorp/golang-lru v0.5.3
1716
github.com/konsorten/go-windows-terminal-sequences v1.0.2 // indirect
1817
github.com/magiconair/properties v1.8.1 // indirect
1918
github.com/mattn/go-isatty v0.0.10 // indirect

go.sum

-2
Original file line numberDiff line numberDiff line change
@@ -132,8 +132,6 @@ github.com/gostaticanalysis/analysisutil v0.0.3/go.mod h1:eEOZF4jCKGi+aprrirO9e7
132132
github.com/grpc-ecosystem/go-grpc-middleware v1.0.0/go.mod h1:FiyG127CGDf3tlThmgyCl78X/SZQqEOJBCDaAfeWzPs=
133133
github.com/grpc-ecosystem/go-grpc-prometheus v1.2.0/go.mod h1:8NvIoxWQoOIhqOTXgfV/d3M/q6VIi02HzZEHgUlZvzk=
134134
github.com/grpc-ecosystem/grpc-gateway v1.9.0/go.mod h1:vNeuVxBJEsws4ogUvrchl83t/GYV9WGTSLVdBhOQFDY=
135-
github.com/hashicorp/golang-lru v0.5.3 h1:YPkqC67at8FYaadspW/6uE0COsBxS2656RLEr8Bppgk=
136-
github.com/hashicorp/golang-lru v0.5.3/go.mod h1:iADmTwqILo4mZ8BN3D2Q6+9jd8WM5uGBxy+E8yxSoD4=
137135
github.com/hashicorp/hcl v1.0.0 h1:0Anlzjpi4vEasTeNFn2mLJgTSwt0+6sfsiTG8qcWGx4=
138136
github.com/hashicorp/hcl v1.0.0/go.mod h1:E5yfLk+7swimpb2L/Alb/PJmXilQ/rhwaUYs4T20WEQ=
139137
github.com/hpcloud/tail v1.0.0 h1:nfCOvKYfkgYP8hkirhJocXT2+zOD8yUNjXaWfTlyFKI=

sdk/trace/attributesMap.go

+90
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,90 @@
1+
// Copyright 2019, OpenTelemetry Authors
2+
//
3+
// Licensed under the Apache License, Version 2.0 (the "License");
4+
// you may not use this file except in compliance with the License.
5+
// You may obtain a copy of the License at
6+
//
7+
// http://www.apache.org/licenses/LICENSE-2.0
8+
//
9+
// Unless required by applicable law or agreed to in writing, software
10+
// distributed under the License is distributed on an "AS IS" BASIS,
11+
// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
12+
// See the License for the specific language governing permissions and
13+
// limitations under the License.
14+
15+
package trace
16+
17+
import (
18+
"container/list"
19+
20+
"go.opentelemetry.io/otel/api/core"
21+
"go.opentelemetry.io/otel/sdk/export/trace"
22+
)
23+
24+
// attributesMap is a capped map of attributes, holding the most recent attributes.
25+
// Eviction is done via a LRU method, the oldest entry is removed to create room for a new entry.
26+
// Updates are allowed and refreshes the usage of the key.
27+
//
28+
// This is based from https://github.com/hashicorp/golang-lru/blob/master/simplelru/lru.go
29+
// With a subset of the its operations and specific for holding core.KeyValue
30+
type attributesMap struct {
31+
attributes map[core.Key]*list.Element
32+
evictList *list.List
33+
droppedCount int
34+
capacity int
35+
}
36+
37+
func newAttributesMap(capacity int) *attributesMap {
38+
lm := &attributesMap{
39+
attributes: make(map[core.Key]*list.Element),
40+
evictList: list.New(),
41+
capacity: capacity,
42+
}
43+
return lm
44+
}
45+
46+
func (am *attributesMap) add(kv core.KeyValue) {
47+
// Check for existing item
48+
if ent, ok := am.attributes[kv.Key]; ok {
49+
am.evictList.MoveToFront(ent)
50+
ent.Value = &kv
51+
return
52+
}
53+
54+
// Add new item
55+
entry := am.evictList.PushFront(&kv)
56+
am.attributes[kv.Key] = entry
57+
58+
// Verify size not exceeded
59+
if am.evictList.Len() > am.capacity {
60+
am.removeOldest()
61+
am.droppedCount++
62+
}
63+
}
64+
65+
func (am *attributesMap) toSpanData(sd *trace.SpanData) {
66+
len := am.evictList.Len()
67+
if len == 0 {
68+
return
69+
}
70+
71+
attributes := make([]core.KeyValue, 0, len)
72+
for ent := am.evictList.Back(); ent != nil; ent = ent.Prev() {
73+
if value, ok := ent.Value.(*core.KeyValue); ok {
74+
attributes = append(attributes, *value)
75+
}
76+
}
77+
78+
sd.Attributes = attributes
79+
sd.DroppedAttributeCount = am.droppedCount
80+
}
81+
82+
// removeOldest removes the oldest item from the cache.
83+
func (am *attributesMap) removeOldest() {
84+
ent := am.evictList.Back()
85+
if ent != nil {
86+
am.evictList.Remove(ent)
87+
kv := ent.Value.(*core.KeyValue)
88+
delete(am.attributes, kv.Key)
89+
}
90+
}

sdk/trace/lrumap.go

-37
This file was deleted.

sdk/trace/span.go

+7-21
Original file line numberDiff line numberDiff line change
@@ -38,9 +38,9 @@ type span struct {
3838
mu sync.Mutex // protects the contents of *data (but not the pointer value.)
3939
spanContext core.SpanContext
4040

41-
// lruAttributes are capped at configured limit. When the capacity is reached an oldest entry
41+
// attributes are capped at configured limit. When the capacity is reached an oldest entry
4242
// is removed to create room for a new entry.
43-
lruAttributes *lruMap
43+
attributes *attributesMap
4444

4545
// messageEvents are stored in FIFO queue capped by configured limit.
4646
messageEvents *evictedQueue
@@ -228,10 +228,9 @@ func (s *span) makeSpanData() *export.SpanData {
228228
s.mu.Lock()
229229
defer s.mu.Unlock()
230230
sd = *s.data
231-
if s.lruAttributes.simpleLruMap.Len() > 0 {
232-
sd.Attributes = s.lruAttributesToAttributeMap()
233-
sd.DroppedAttributeCount = s.lruAttributes.droppedCount
234-
}
231+
232+
s.attributes.toSpanData(&sd)
233+
235234
if len(s.messageEvents.queue) > 0 {
236235
sd.MessageEvents = s.interfaceArrayToMessageEventArray()
237236
sd.DroppedMessageEventCount = s.messageEvents.droppedCount
@@ -259,24 +258,11 @@ func (s *span) interfaceArrayToMessageEventArray() []export.Event {
259258
return messageEventArr
260259
}
261260

262-
func (s *span) lruAttributesToAttributeMap() []core.KeyValue {
263-
attributes := make([]core.KeyValue, 0, s.lruAttributes.simpleLruMap.Len())
264-
for _, key := range s.lruAttributes.simpleLruMap.Keys() {
265-
value, ok := s.lruAttributes.simpleLruMap.Get(key)
266-
if ok {
267-
key := key.(core.Key)
268-
value := value.(core.Value)
269-
attributes = append(attributes, core.KeyValue{Key: key, Value: value})
270-
}
271-
}
272-
return attributes
273-
}
274-
275261
func (s *span) copyToCappedAttributes(attributes ...core.KeyValue) {
276262
s.mu.Lock()
277263
defer s.mu.Unlock()
278264
for _, a := range attributes {
279-
s.lruAttributes.add(a.Key, a.Value)
265+
s.attributes.add(a)
280266
}
281267
}
282268

@@ -328,7 +314,7 @@ func startSpanInternal(tr *tracer, name string, parent core.SpanContext, remoteP
328314
Name: name,
329315
HasRemoteParent: remoteParent,
330316
}
331-
span.lruAttributes = newLruMap(cfg.MaxAttributesPerSpan)
317+
span.attributes = newAttributesMap(cfg.MaxAttributesPerSpan)
332318
span.messageEvents = newEvictedQueue(cfg.MaxEventsPerSpan)
333319
span.links = newEvictedQueue(cfg.MaxLinksPerSpan)
334320

0 commit comments

Comments
 (0)