forked from open-telemetry/opentelemetry-go
/
exporter.go
174 lines (154 loc) · 4.79 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
// 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 metrictest // import "go.opentelemetry.io/otel/sdk/metric/metrictest"
import (
"context"
"fmt"
"go.opentelemetry.io/otel/attribute"
"go.opentelemetry.io/otel/metric"
"go.opentelemetry.io/otel/sdk/instrumentation"
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 {
exports []ExportRecord
// resource *resource.Resource
controller *controller.Controller
}
func NewTestMeterProvider() (metric.MeterProvider, *Exporter) {
c := controller.New(
processor.NewFactory(
selector.NewWithHistogramDistribution(),
aggregation.CumulativeTemporalitySelector(),
),
controller.WithCollectPeriod(0),
)
exp := &Exporter{
controller: c,
}
return c, exp
}
// ExportRecord represents one collected datapoint from the Exporter.
type ExportRecord struct {
InstrumentName string
InstrumentationLibrary Library
Labels []attribute.KeyValue
AggregationKind aggregation.Kind
Sum number.Number
Count uint64
Histogram aggregation.Buckets
LastValue number.Number
}
// Collect triggers the SDK's collect methods and then aggregates the data into
// `ExportRecord`s.
func (e *Exporter) Collect(ctx context.Context) error {
e.exports = []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(aggregation.CumulativeTemporalitySelector(), func(rec export.Record) error {
record := ExportRecord{
InstrumentName: rec.Descriptor().Name(),
InstrumentationLibrary: lib,
Labels: rec.Labels().ToSlice(),
AggregationKind: rec.Aggregation().Kind(),
}
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.exports = append(e.exports, record)
return nil
})
})
}
// GetRecords returns all Records found by the SDK
func (e *Exporter) GetRecords() []ExportRecord {
return e.exports
}
var ErrNotFound = fmt.Errorf("record not found")
// GetByName returns the first Record with a matching name.
func (e *Exporter) GetByName(name string) (ExportRecord, error) {
for _, rec := range e.exports {
if rec.InstrumentName == name {
return rec, nil
}
}
return ExportRecord{}, ErrNotFound
}
// GetByNameAndLabels returns the first Record with a matching name and set of labels
func (e *Exporter) GetByNameAndLabels(name string, labels []attribute.KeyValue) (ExportRecord, error) {
for _, rec := range e.exports {
if rec.InstrumentName == name && labelsMatch(labels, rec.Labels) {
return rec, nil
}
}
return ExportRecord{}, ErrNotFound
}
func labelsMatch(labelsA, labelsB []attribute.KeyValue) bool {
if len(labelsA) != len(labelsB) {
return false
}
for i := range labelsA {
if labelsA[i] != labelsB[i] {
return false
}
}
return true
}