/
exporter.go
242 lines (213 loc) · 6.83 KB
/
exporter.go
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
//
//
// Tencent is pleased to support the open source community by making tRPC available.
//
// Copyright (C) 2023 THL A29 Limited, a Tencent company.
// All rights reserved.
//
// If you have downloaded a copy of the tRPC source code from Tencent,
// please note that tRPC source code is licensed under the Apache 2.0 License,
// A copy of the Apache 2.0 License is included in this file.
//
//
package meterprovider
import (
"context"
"fmt"
"strings"
"sync"
"go.opentelemetry.io/otel/attribute"
"go.opentelemetry.io/otel/metric"
"go.opentelemetry.io/otel/metric/unit"
"go.opentelemetry.io/otel/sdk/instrumentation"
"go.opentelemetry.io/otel/sdk/metric/aggregator/histogram"
controller "go.opentelemetry.io/otel/sdk/metric/controller/basic"
"go.opentelemetry.io/otel/sdk/metric/export"
"go.opentelemetry.io/otel/sdk/metric/export/aggregation"
"go.opentelemetry.io/otel/sdk/metric/number"
processor "go.opentelemetry.io/otel/sdk/metric/processor/basic"
selector "go.opentelemetry.io/otel/sdk/metric/selector/simple"
)
// Exporter is a manually collected exporter for testing the SDK. It does not
// satisfy the `export.Exporter` interface because it is not intended to be
// used with the periodic collection of the SDK, instead the test should
// manually call `Collect()`
//
// Exporters are not thread safe, and should only be used for testing.
type Exporter struct {
// records contains the last metrics collected.
records []ExportRecord
mux *sync.RWMutex
controller *controller.Controller
temporalitySelector aggregation.TemporalitySelector
}
// NewOtelMeterProvider creates a MeterProvider and Exporter to be used in tests.
func NewOtelMeterProvider(opts ...Option) (metric.MeterProvider, *Exporter) {
cfg := newConfig(opts...)
c := controller.New(
processor.NewFactory(
selector.NewWithHistogramDistribution(histogram.WithExplicitBoundaries(cfg.boundaries)),
cfg.temporalitySelector,
),
controller.WithCollectPeriod(0),
)
exp := &Exporter{
mux: new(sync.RWMutex),
controller: c,
temporalitySelector: cfg.temporalitySelector,
}
return c, exp
}
// Library is the same as "sdk/instrumentation".Library but there is
// a package cycle to use it so it is redeclared here.
type Library struct {
InstrumentationName string
InstrumentationVersion string
SchemaURL string
}
// ExportRecord represents one collected datapoint from the Exporter.
type ExportRecord struct {
InstrumentName string
InstrumentationLibrary Library
Attributes []attribute.KeyValue
AggregationKind aggregation.Kind
NumberKind number.Kind
Sum number.Number
Count uint64
Histogram aggregation.Buckets
LastValue number.Number
Unit unit.Unit
}
// var descTpl = `instrument library:%s, version:%s, schemaURL:%s instrument name:%s, aggregation:%s, value:%v`
var descTpl = `instrument server:%s, metric name:%s, aggregation:%s, unit:%s, value:%v`
// String implements Stringer to custom print.
func (r ExportRecord) String() string {
args := []interface{}{
r.InstrumentationLibrary.InstrumentationName,
// TODO 如果构建时指定了`go build -vcs',可以尝试提取版本信息
// r.InstrumentationLibrary.InstrumentationVersion,
// r.InstrumentationLibrary.SchemaURL,
r.InstrumentName,
r.AggregationKind,
r.Unit,
}
switch r.AggregationKind {
case aggregation.SumKind:
args = append(args, r.Sum)
case aggregation.HistogramKind:
sb := &strings.Builder{}
for i, b := range r.Histogram.Boundaries {
fmt.Fprintf(sb, "[~%v]:%v ", b, r.Histogram.Counts[i])
}
args = append(args, sb.String())
case aggregation.LastValueKind:
args = append(args, r.LastValue)
default:
return "not supported"
}
return fmt.Sprintf(descTpl, args...)
}
// Collect triggers the SDK's collect methods and then aggregates the data into
// ExportRecords. This will overwrite any previous collected metrics.
func (e *Exporter) Collect(ctx context.Context) error {
e.mux.Lock()
defer e.mux.Unlock()
e.records = []ExportRecord{}
err := e.controller.Collect(ctx)
if err != nil {
return err
}
return e.controller.ForEach(func(l instrumentation.Library, r export.Reader) error {
lib := Library{
InstrumentationName: l.Name,
InstrumentationVersion: l.Version,
SchemaURL: l.SchemaURL,
}
return r.ForEach(e.temporalitySelector, func(rec export.Record) error {
record := ExportRecord{
InstrumentName: rec.Descriptor().Name(),
InstrumentationLibrary: lib,
Attributes: rec.Attributes().ToSlice(),
AggregationKind: rec.Aggregation().Kind(),
NumberKind: rec.Descriptor().NumberKind(),
Unit: rec.Descriptor().Unit(),
}
var err error
switch agg := rec.Aggregation().(type) {
case aggregation.Histogram:
record.AggregationKind = aggregation.HistogramKind
record.Histogram, err = agg.Histogram()
if err != nil {
return err
}
record.Sum, err = agg.Sum()
if err != nil {
return err
}
record.Count, err = agg.Count()
if err != nil {
return err
}
case aggregation.Count:
record.Count, err = agg.Count()
if err != nil {
return err
}
case aggregation.LastValue:
record.LastValue, _, err = agg.LastValue()
if err != nil {
return err
}
case aggregation.Sum:
record.Sum, err = agg.Sum()
if err != nil {
return err
}
}
e.records = append(e.records, record)
return nil
})
})
}
// GetRecords returns all records found by the SDK.
func (e *Exporter) GetRecords() []ExportRecord {
e.mux.RLock()
defer e.mux.RUnlock()
records := make([]ExportRecord, len(e.records))
copy(records, e.records)
return records
}
// ErrNotFound 记录未找到
var ErrNotFound = fmt.Errorf("record not found")
// GetByName returns the first Record with a matching instrument name.
func (e *Exporter) GetByName(name string) (ExportRecord, error) {
e.mux.RLock()
defer e.mux.RUnlock()
for _, rec := range e.records {
if rec.InstrumentName == name {
return rec, nil
}
}
return ExportRecord{}, ErrNotFound
}
// GetByNameAndAttributes returns the first Record with a matching name and the sub-set of attributes.
func (e *Exporter) GetByNameAndAttributes(name string, attributes []attribute.KeyValue) (ExportRecord, error) {
e.mux.RLock()
defer e.mux.RUnlock()
for _, rec := range e.records {
if rec.InstrumentName == name && subSet(attributes, rec.Attributes) {
return rec, nil
}
}
return ExportRecord{}, ErrNotFound
}
// subSet returns true if attributesA is a subset of attributesB.
func subSet(attributesA, attributesB []attribute.KeyValue) bool {
b := attribute.NewSet(attributesB...)
for _, kv := range attributesA {
if v, found := b.Value(kv.Key); !found || v != kv.Value {
return false
}
}
return true
}