mirror of
https://github.com/open-telemetry/opentelemetry-go.git
synced 2024-12-14 10:13:10 +02:00
c3c4273ecc
* Store span data directly in the span - Nesting only some of a span's data in a `data` field (with the rest of the data living direclty in the `span` struct) is confusing. - export.SpanData is meant to be an immutable *snapshot* of a span, not the "authoritative" state of the span. - Refactor attributesMap.toSpanData into toKeyValue and make it return a []label.KeyValue which is clearer than modifying a struct passed to the function. - Read droppedCount from the attributesMap as a separate operation instead of setting it from within attributesMap.toSpanData. - Set a span's end time in the span itself rather than in the SpanData to allow reading the span's end time after a span has ended. - Set a span's end time as soon as possible within span.End so that we don't influence the span's end time with operations such as fetching span processors and generating span data. - Remove error handling for uninitialized spans. This check seems to be necessary only because we used to have an *export.SpanData field which could be nil. Now that we no longer have this field I think we can safely remove the check. The error isn't used anywhere else so remove it, too. * Store parent as trace.SpanContext The spec requires that the parent field of a Span be a Span, a SpanContext or null. Rather than extracting the parent's span ID from the trace.SpanContext which we get from the tracer, store the trace.SpanContext as is and explicitly extract the parent's span ID where necessary. * Add ReadOnlySpan interface Use this interface instead of export.SpanData in places where reading information from a span is necessary. Use export.SpanData only when exporting spans. * Add ReadWriteSpan interface Use this interface instead of export.SpanData in places where it is necessary to read information from a span and write to it at the same time. * Rename export.SpanData to SpanSnapshot SpanSnapshot represents the nature of this type as well as its intended use more accurately. Clarify the purpose of SpanSnapshot in the docs and emphasize what should and should not be done with it. * Rephrase attributesMap doc comment "refreshes" is wrong for plural ("updates"). * Refactor span.End() - Improve accuracy of span duration. Record span end time ASAP. We want to measure a user operation as accurately as possible, which means we want to mark the end time of a span as soon as possible after span.End() is called. Any operations we do inside span.End() before storing the end time affect the total duration of the span, and although these operations are rather fast at the moment they still seem to affect the duration of the span by "artificially" adding time between the start and end timestamps. This is relevant only in cases where the end time isn't explicitly specified. - Remove redundant idempotence check. Now that IsRecording() is based on the value of span.endTime, IsRecording() will always return false after span.End() had been called because span.endTime won't be zero. This means we no longer need span.endOnce. - Improve TestEndSpanTwice so that it also ensures subsequent calls to span.End() don't modify the span's end time. * Update changelog Co-authored-by: Tyler Yahn <codingalias@gmail.com> Co-authored-by: Tyler Yahn <MrAlias@users.noreply.github.com>
92 lines
2.6 KiB
Go
92 lines
2.6 KiB
Go
// Copyright The OpenTelemetry Authors
|
|
//
|
|
// Licensed under the Apache License, Version 2.0 (the "License");
|
|
// you may not use this file except in compliance with the License.
|
|
// You may obtain a copy of the License at
|
|
//
|
|
// http://www.apache.org/licenses/LICENSE-2.0
|
|
//
|
|
// Unless required by applicable law or agreed to in writing, software
|
|
// distributed under the License is distributed on an "AS IS" BASIS,
|
|
// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
|
|
// See the License for the specific language governing permissions and
|
|
// limitations under the License.
|
|
|
|
package trace // import "go.opentelemetry.io/otel/sdk/trace"
|
|
|
|
import (
|
|
"container/list"
|
|
|
|
"go.opentelemetry.io/otel/label"
|
|
)
|
|
|
|
// attributesMap is a capped map of attributes, holding the most recent attributes.
|
|
// Eviction is done via a LRU method, the oldest entry is removed to create room for a new entry.
|
|
// Updates are allowed and they refresh the usage of the key.
|
|
//
|
|
// This is based from https://github.com/hashicorp/golang-lru/blob/master/simplelru/lru.go
|
|
// With a subset of the its operations and specific for holding label.KeyValue
|
|
type attributesMap struct {
|
|
attributes map[label.Key]*list.Element
|
|
evictList *list.List
|
|
droppedCount int
|
|
capacity int
|
|
}
|
|
|
|
func newAttributesMap(capacity int) *attributesMap {
|
|
lm := &attributesMap{
|
|
attributes: make(map[label.Key]*list.Element),
|
|
evictList: list.New(),
|
|
capacity: capacity,
|
|
}
|
|
return lm
|
|
}
|
|
|
|
func (am *attributesMap) add(kv label.KeyValue) {
|
|
// Check for existing item
|
|
if ent, ok := am.attributes[kv.Key]; ok {
|
|
am.evictList.MoveToFront(ent)
|
|
ent.Value = &kv
|
|
return
|
|
}
|
|
|
|
// Add new item
|
|
entry := am.evictList.PushFront(&kv)
|
|
am.attributes[kv.Key] = entry
|
|
|
|
// Verify size not exceeded
|
|
if am.evictList.Len() > am.capacity {
|
|
am.removeOldest()
|
|
am.droppedCount++
|
|
}
|
|
}
|
|
|
|
// toKeyValue copies the attributesMap into a slice of label.KeyValue and
|
|
// returns it. If the map is empty, a nil is returned.
|
|
// TODO: Is it more efficient to return a pointer to the slice?
|
|
func (am *attributesMap) toKeyValue() []label.KeyValue {
|
|
len := am.evictList.Len()
|
|
if len == 0 {
|
|
return nil
|
|
}
|
|
|
|
attributes := make([]label.KeyValue, 0, len)
|
|
for ent := am.evictList.Back(); ent != nil; ent = ent.Prev() {
|
|
if value, ok := ent.Value.(*label.KeyValue); ok {
|
|
attributes = append(attributes, *value)
|
|
}
|
|
}
|
|
|
|
return attributes
|
|
}
|
|
|
|
// removeOldest removes the oldest item from the cache.
|
|
func (am *attributesMap) removeOldest() {
|
|
ent := am.evictList.Back()
|
|
if ent != nil {
|
|
am.evictList.Remove(ent)
|
|
kv := ent.Value.(*label.KeyValue)
|
|
delete(am.attributes, kv.Key)
|
|
}
|
|
}
|