mirror of
https://github.com/open-telemetry/opentelemetry-go.git
synced 2025-04-11 11:21:59 +02:00
Encode labels once during checkpoint (#572)
The `checkpoint` function is executed in a single thread so we can do the encoding lazily before passing the encoded version of labels to the exporter. This is a cheap and quick way to avoid encoding the labels on every collection interval. Co-authored-by: Rahul Patel <rahulpa@google.com>
This commit is contained in:
parent
f7df68b68b
commit
cc756f6a8d
@ -98,6 +98,9 @@ type (
|
|||||||
// cachedValue contains a `reflect.Value` of the `ordered`
|
// cachedValue contains a `reflect.Value` of the `ordered`
|
||||||
// member
|
// member
|
||||||
cachedValue reflect.Value
|
cachedValue reflect.Value
|
||||||
|
// cachedEncoded contains an encoded version of the
|
||||||
|
// `ordered` member
|
||||||
|
cachedEncoded string
|
||||||
}
|
}
|
||||||
|
|
||||||
// mapkey uniquely describes a metric instrument in terms of
|
// mapkey uniquely describes a metric instrument in terms of
|
||||||
@ -379,6 +382,14 @@ func (ls *labels) computeOrdered(kvs []core.KeyValue) {
|
|||||||
ls.cachedValue = reflect.ValueOf(ls.ordered)
|
ls.cachedValue = reflect.ValueOf(ls.ordered)
|
||||||
}
|
}
|
||||||
|
|
||||||
|
func (ls *labels) ensureEncoded(encoder export.LabelEncoder) {
|
||||||
|
if ls.cachedEncoded != "" {
|
||||||
|
return
|
||||||
|
}
|
||||||
|
iter := export.NewLabelIterator(ls)
|
||||||
|
ls.cachedEncoded = encoder.Encode(iter)
|
||||||
|
}
|
||||||
|
|
||||||
func computeOrderedFixed(kvs []core.KeyValue) orderedLabels {
|
func computeOrderedFixed(kvs []core.KeyValue) orderedLabels {
|
||||||
switch len(kvs) {
|
switch len(kvs) {
|
||||||
case 1:
|
case 1:
|
||||||
@ -556,12 +567,8 @@ func (m *SDK) checkpoint(ctx context.Context, descriptor *metric.Descriptor, rec
|
|||||||
}
|
}
|
||||||
recorder.Checkpoint(ctx, descriptor)
|
recorder.Checkpoint(ctx, descriptor)
|
||||||
|
|
||||||
// TODO Labels are encoded once per collection interval,
|
labels.ensureEncoded(m.labelEncoder)
|
||||||
// instead of once per bound instrument lifetime. This can be
|
exportLabels := export.NewLabels(labels, labels.cachedEncoded, m.labelEncoder)
|
||||||
// addressed similarly to OTEP 78, see
|
|
||||||
// https://github.com/jmacd/opentelemetry-go/blob/8bed2e14df7f9f4688fbab141924bb786dc9a3a1/api/context/internal/set.go#L89
|
|
||||||
iter := export.NewLabelIterator(labels)
|
|
||||||
exportLabels := export.NewLabels(labels, m.labelEncoder.Encode(iter), m.labelEncoder)
|
|
||||||
exportRecord := export.NewRecord(descriptor, exportLabels, recorder)
|
exportRecord := export.NewRecord(descriptor, exportLabels, recorder)
|
||||||
err := m.batcher.Process(ctx, exportRecord)
|
err := m.batcher.Process(ctx, exportRecord)
|
||||||
if err != nil {
|
if err != nil {
|
||||||
|
Loading…
x
Reference in New Issue
Block a user