|
| 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 | +} |
0 commit comments