2021-06-11 22:25:56 +02:00
|
|
|
// 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 otlpmetric_test
|
|
|
|
|
|
|
|
import (
|
|
|
|
"context"
|
2021-08-13 00:44:58 +02:00
|
|
|
"fmt"
|
2021-06-11 22:25:56 +02:00
|
|
|
"testing"
|
|
|
|
"time"
|
|
|
|
|
2021-08-13 00:44:58 +02:00
|
|
|
"github.com/google/go-cmp/cmp"
|
2021-06-11 22:25:56 +02:00
|
|
|
"github.com/stretchr/testify/assert"
|
|
|
|
"github.com/stretchr/testify/require"
|
2021-08-13 00:44:58 +02:00
|
|
|
"google.golang.org/protobuf/testing/protocmp"
|
2021-06-11 22:25:56 +02:00
|
|
|
|
|
|
|
"go.opentelemetry.io/otel/attribute"
|
2021-08-13 00:44:58 +02:00
|
|
|
"go.opentelemetry.io/otel/exporters/otlp/otlpmetric"
|
|
|
|
"go.opentelemetry.io/otel/exporters/otlp/otlpmetric/internal/metrictransform"
|
2021-06-11 22:25:56 +02:00
|
|
|
"go.opentelemetry.io/otel/metric"
|
2021-09-27 17:51:47 +02:00
|
|
|
"go.opentelemetry.io/otel/metric/metrictest"
|
2021-06-11 22:25:56 +02:00
|
|
|
"go.opentelemetry.io/otel/metric/number"
|
2021-08-12 01:02:28 +02:00
|
|
|
"go.opentelemetry.io/otel/metric/sdkapi"
|
2021-06-11 22:25:56 +02:00
|
|
|
metricsdk "go.opentelemetry.io/otel/sdk/export/metric"
|
2021-10-15 20:18:36 +02:00
|
|
|
"go.opentelemetry.io/otel/sdk/export/metric/aggregation"
|
2021-09-27 17:51:47 +02:00
|
|
|
"go.opentelemetry.io/otel/sdk/instrumentation"
|
2021-08-13 00:44:58 +02:00
|
|
|
"go.opentelemetry.io/otel/sdk/metric/aggregator/histogram"
|
|
|
|
"go.opentelemetry.io/otel/sdk/metric/aggregator/sum"
|
2021-09-27 17:51:47 +02:00
|
|
|
"go.opentelemetry.io/otel/sdk/metric/processor/processortest"
|
2021-06-11 22:25:56 +02:00
|
|
|
"go.opentelemetry.io/otel/sdk/resource"
|
|
|
|
commonpb "go.opentelemetry.io/proto/otlp/common/v1"
|
|
|
|
metricpb "go.opentelemetry.io/proto/otlp/metrics/v1"
|
|
|
|
)
|
|
|
|
|
|
|
|
var (
|
|
|
|
// Timestamps used in this test:
|
|
|
|
|
|
|
|
intervalStart = time.Now()
|
|
|
|
intervalEnd = intervalStart.Add(time.Hour)
|
|
|
|
)
|
|
|
|
|
|
|
|
type stubClient struct {
|
|
|
|
rm []*metricpb.ResourceMetrics
|
|
|
|
}
|
|
|
|
|
|
|
|
func (m *stubClient) Start(ctx context.Context) error {
|
|
|
|
return nil
|
|
|
|
}
|
|
|
|
|
|
|
|
func (m *stubClient) Stop(ctx context.Context) error {
|
|
|
|
return nil
|
|
|
|
}
|
|
|
|
|
|
|
|
func (m *stubClient) UploadMetrics(ctx context.Context, protoMetrics []*metricpb.ResourceMetrics) error {
|
|
|
|
m.rm = append(m.rm, protoMetrics...)
|
|
|
|
return nil
|
|
|
|
}
|
|
|
|
|
|
|
|
var _ otlpmetric.Client = (*stubClient)(nil)
|
|
|
|
|
|
|
|
func (m *stubClient) Reset() {
|
|
|
|
m.rm = nil
|
|
|
|
}
|
|
|
|
|
|
|
|
func newExporter(t *testing.T, opts ...otlpmetric.Option) (*otlpmetric.Exporter, *stubClient) {
|
|
|
|
client := &stubClient{}
|
|
|
|
exp, _ := otlpmetric.New(context.Background(), client, opts...)
|
|
|
|
return exp, client
|
|
|
|
}
|
|
|
|
|
|
|
|
func startTime() uint64 {
|
|
|
|
return uint64(intervalStart.UnixNano())
|
|
|
|
}
|
|
|
|
|
|
|
|
func pointTime() uint64 {
|
|
|
|
return uint64(intervalEnd.UnixNano())
|
|
|
|
}
|
|
|
|
|
2021-09-27 17:51:47 +02:00
|
|
|
type testRecord struct {
|
2021-08-13 00:44:58 +02:00
|
|
|
name string
|
|
|
|
iKind sdkapi.InstrumentKind
|
|
|
|
nKind number.Kind
|
|
|
|
labels []attribute.KeyValue
|
2021-09-27 17:51:47 +02:00
|
|
|
|
|
|
|
meterName string
|
|
|
|
meterOpts []metric.MeterOption
|
|
|
|
}
|
|
|
|
|
|
|
|
func record(
|
|
|
|
name string,
|
|
|
|
iKind sdkapi.InstrumentKind,
|
|
|
|
nKind number.Kind,
|
|
|
|
labels []attribute.KeyValue,
|
|
|
|
meterName string,
|
|
|
|
meterOpts ...metric.MeterOption) testRecord {
|
|
|
|
return testRecord{
|
|
|
|
name: name,
|
|
|
|
iKind: iKind,
|
|
|
|
nKind: nKind,
|
|
|
|
labels: labels,
|
|
|
|
meterName: meterName,
|
|
|
|
meterOpts: meterOpts,
|
|
|
|
}
|
2021-06-11 22:25:56 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
var (
|
|
|
|
baseKeyValues = []attribute.KeyValue{attribute.String("host", "test.com")}
|
|
|
|
cpuKey = attribute.Key("CPU")
|
|
|
|
|
|
|
|
testHistogramBoundaries = []float64{2.0, 4.0, 8.0}
|
|
|
|
|
|
|
|
cpu1Labels = []*commonpb.KeyValue{
|
|
|
|
{
|
|
|
|
Key: "CPU",
|
|
|
|
Value: &commonpb.AnyValue{
|
|
|
|
Value: &commonpb.AnyValue_IntValue{
|
|
|
|
IntValue: 1,
|
|
|
|
},
|
|
|
|
},
|
|
|
|
},
|
|
|
|
{
|
|
|
|
Key: "host",
|
|
|
|
Value: &commonpb.AnyValue{
|
|
|
|
Value: &commonpb.AnyValue_StringValue{
|
|
|
|
StringValue: "test.com",
|
|
|
|
},
|
|
|
|
},
|
|
|
|
},
|
|
|
|
}
|
|
|
|
cpu2Labels = []*commonpb.KeyValue{
|
|
|
|
{
|
|
|
|
Key: "CPU",
|
|
|
|
Value: &commonpb.AnyValue{
|
|
|
|
Value: &commonpb.AnyValue_IntValue{
|
|
|
|
IntValue: 2,
|
|
|
|
},
|
|
|
|
},
|
|
|
|
},
|
|
|
|
{
|
|
|
|
Key: "host",
|
|
|
|
Value: &commonpb.AnyValue{
|
|
|
|
Value: &commonpb.AnyValue_StringValue{
|
|
|
|
StringValue: "test.com",
|
|
|
|
},
|
|
|
|
},
|
|
|
|
},
|
|
|
|
}
|
|
|
|
|
2021-08-13 00:44:58 +02:00
|
|
|
testerAResource = resource.NewSchemaless(attribute.String("instance", "tester-a"))
|
|
|
|
testerAResourcePb = metrictransform.Resource(testerAResource)
|
2021-06-11 22:25:56 +02:00
|
|
|
)
|
|
|
|
|
2021-09-27 17:51:47 +02:00
|
|
|
const (
|
|
|
|
// Most of this test uses an empty instrumentation library name.
|
|
|
|
testLibName = ""
|
|
|
|
)
|
|
|
|
|
2021-06-11 22:25:56 +02:00
|
|
|
func TestNoGroupingExport(t *testing.T) {
|
|
|
|
runMetricExportTests(
|
|
|
|
t,
|
|
|
|
nil,
|
2021-09-27 17:51:47 +02:00
|
|
|
resource.Empty(),
|
|
|
|
[]testRecord{
|
|
|
|
record(
|
2021-06-11 22:25:56 +02:00
|
|
|
"int64-count",
|
2021-08-12 01:02:28 +02:00
|
|
|
sdkapi.CounterInstrumentKind,
|
2021-06-11 22:25:56 +02:00
|
|
|
number.Int64Kind,
|
|
|
|
append(baseKeyValues, cpuKey.Int(1)),
|
2021-09-27 17:51:47 +02:00
|
|
|
testLibName,
|
|
|
|
),
|
|
|
|
record(
|
2021-06-11 22:25:56 +02:00
|
|
|
"int64-count",
|
2021-08-12 01:02:28 +02:00
|
|
|
sdkapi.CounterInstrumentKind,
|
2021-06-11 22:25:56 +02:00
|
|
|
number.Int64Kind,
|
|
|
|
append(baseKeyValues, cpuKey.Int(2)),
|
2021-09-27 17:51:47 +02:00
|
|
|
testLibName,
|
|
|
|
),
|
2021-06-11 22:25:56 +02:00
|
|
|
},
|
|
|
|
[]*metricpb.ResourceMetrics{
|
|
|
|
{
|
|
|
|
Resource: nil,
|
|
|
|
InstrumentationLibraryMetrics: []*metricpb.InstrumentationLibraryMetrics{
|
|
|
|
{
|
|
|
|
Metrics: []*metricpb.Metric{
|
|
|
|
{
|
|
|
|
Name: "int64-count",
|
|
|
|
Data: &metricpb.Metric_Sum{
|
|
|
|
Sum: &metricpb.Sum{
|
|
|
|
IsMonotonic: true,
|
|
|
|
AggregationTemporality: metricpb.AggregationTemporality_AGGREGATION_TEMPORALITY_CUMULATIVE,
|
|
|
|
DataPoints: []*metricpb.NumberDataPoint{
|
|
|
|
{
|
|
|
|
Value: &metricpb.NumberDataPoint_AsInt{AsInt: 11},
|
|
|
|
Attributes: cpu1Labels,
|
|
|
|
StartTimeUnixNano: startTime(),
|
|
|
|
TimeUnixNano: pointTime(),
|
|
|
|
},
|
|
|
|
{
|
|
|
|
Value: &metricpb.NumberDataPoint_AsInt{AsInt: 11},
|
|
|
|
Attributes: cpu2Labels,
|
|
|
|
StartTimeUnixNano: startTime(),
|
|
|
|
TimeUnixNano: pointTime(),
|
|
|
|
},
|
|
|
|
},
|
|
|
|
},
|
|
|
|
},
|
|
|
|
},
|
|
|
|
},
|
|
|
|
},
|
|
|
|
},
|
|
|
|
},
|
|
|
|
},
|
|
|
|
)
|
|
|
|
}
|
|
|
|
|
2021-09-01 22:38:37 +02:00
|
|
|
func TestHistogramMetricGroupingExport(t *testing.T) {
|
2021-09-27 17:51:47 +02:00
|
|
|
r := record(
|
2021-09-01 22:38:37 +02:00
|
|
|
"histogram",
|
|
|
|
sdkapi.HistogramInstrumentKind,
|
2021-06-11 22:25:56 +02:00
|
|
|
number.Int64Kind,
|
|
|
|
append(baseKeyValues, cpuKey.Int(1)),
|
2021-09-27 17:51:47 +02:00
|
|
|
testLibName,
|
|
|
|
)
|
2021-06-11 22:25:56 +02:00
|
|
|
expected := []*metricpb.ResourceMetrics{
|
|
|
|
{
|
|
|
|
Resource: nil,
|
|
|
|
InstrumentationLibraryMetrics: []*metricpb.InstrumentationLibraryMetrics{
|
|
|
|
{
|
|
|
|
Metrics: []*metricpb.Metric{
|
|
|
|
{
|
2021-09-01 22:38:37 +02:00
|
|
|
Name: "histogram",
|
2021-06-11 22:25:56 +02:00
|
|
|
Data: &metricpb.Metric_Histogram{
|
|
|
|
Histogram: &metricpb.Histogram{
|
|
|
|
AggregationTemporality: metricpb.AggregationTemporality_AGGREGATION_TEMPORALITY_CUMULATIVE,
|
|
|
|
DataPoints: []*metricpb.HistogramDataPoint{
|
|
|
|
{
|
|
|
|
Attributes: cpu1Labels,
|
|
|
|
StartTimeUnixNano: startTime(),
|
|
|
|
TimeUnixNano: pointTime(),
|
|
|
|
Count: 2,
|
|
|
|
Sum: 11,
|
|
|
|
ExplicitBounds: testHistogramBoundaries,
|
|
|
|
BucketCounts: []uint64{1, 0, 0, 1},
|
|
|
|
},
|
|
|
|
{
|
|
|
|
Attributes: cpu1Labels,
|
|
|
|
Count: 2,
|
|
|
|
Sum: 11,
|
|
|
|
ExplicitBounds: testHistogramBoundaries,
|
|
|
|
BucketCounts: []uint64{1, 0, 0, 1},
|
|
|
|
StartTimeUnixNano: startTime(),
|
|
|
|
TimeUnixNano: pointTime(),
|
|
|
|
},
|
|
|
|
},
|
|
|
|
},
|
|
|
|
},
|
|
|
|
},
|
|
|
|
},
|
|
|
|
},
|
|
|
|
},
|
|
|
|
},
|
|
|
|
}
|
2021-09-27 17:51:47 +02:00
|
|
|
runMetricExportTests(t, nil, resource.Empty(), []testRecord{r, r}, expected)
|
2021-06-11 22:25:56 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
func TestCountInt64MetricGroupingExport(t *testing.T) {
|
2021-09-27 17:51:47 +02:00
|
|
|
r := record(
|
2021-06-11 22:25:56 +02:00
|
|
|
"int64-count",
|
2021-08-12 01:02:28 +02:00
|
|
|
sdkapi.CounterInstrumentKind,
|
2021-06-11 22:25:56 +02:00
|
|
|
number.Int64Kind,
|
|
|
|
append(baseKeyValues, cpuKey.Int(1)),
|
2021-09-27 17:51:47 +02:00
|
|
|
testLibName,
|
|
|
|
)
|
2021-06-11 22:25:56 +02:00
|
|
|
runMetricExportTests(
|
|
|
|
t,
|
|
|
|
nil,
|
2021-09-27 17:51:47 +02:00
|
|
|
resource.Empty(),
|
|
|
|
[]testRecord{r, r},
|
2021-06-11 22:25:56 +02:00
|
|
|
[]*metricpb.ResourceMetrics{
|
|
|
|
{
|
|
|
|
Resource: nil,
|
|
|
|
InstrumentationLibraryMetrics: []*metricpb.InstrumentationLibraryMetrics{
|
|
|
|
{
|
|
|
|
Metrics: []*metricpb.Metric{
|
|
|
|
{
|
|
|
|
Name: "int64-count",
|
|
|
|
Data: &metricpb.Metric_Sum{
|
|
|
|
Sum: &metricpb.Sum{
|
|
|
|
IsMonotonic: true,
|
|
|
|
AggregationTemporality: metricpb.AggregationTemporality_AGGREGATION_TEMPORALITY_CUMULATIVE,
|
|
|
|
DataPoints: []*metricpb.NumberDataPoint{
|
|
|
|
{
|
|
|
|
Value: &metricpb.NumberDataPoint_AsInt{AsInt: 11},
|
|
|
|
Attributes: cpu1Labels,
|
|
|
|
StartTimeUnixNano: startTime(),
|
|
|
|
TimeUnixNano: pointTime(),
|
|
|
|
},
|
|
|
|
{
|
|
|
|
Value: &metricpb.NumberDataPoint_AsInt{AsInt: 11},
|
|
|
|
Attributes: cpu1Labels,
|
|
|
|
StartTimeUnixNano: startTime(),
|
|
|
|
TimeUnixNano: pointTime(),
|
|
|
|
},
|
|
|
|
},
|
|
|
|
},
|
|
|
|
},
|
|
|
|
},
|
|
|
|
},
|
|
|
|
},
|
|
|
|
},
|
|
|
|
},
|
|
|
|
},
|
|
|
|
)
|
|
|
|
}
|
|
|
|
|
|
|
|
func TestCountFloat64MetricGroupingExport(t *testing.T) {
|
2021-09-27 17:51:47 +02:00
|
|
|
r := record(
|
2021-06-11 22:25:56 +02:00
|
|
|
"float64-count",
|
2021-08-12 01:02:28 +02:00
|
|
|
sdkapi.CounterInstrumentKind,
|
2021-06-11 22:25:56 +02:00
|
|
|
number.Float64Kind,
|
|
|
|
append(baseKeyValues, cpuKey.Int(1)),
|
2021-09-27 17:51:47 +02:00
|
|
|
testLibName,
|
|
|
|
)
|
2021-06-11 22:25:56 +02:00
|
|
|
runMetricExportTests(
|
|
|
|
t,
|
|
|
|
nil,
|
2021-09-27 17:51:47 +02:00
|
|
|
resource.Empty(),
|
|
|
|
[]testRecord{r, r},
|
2021-06-11 22:25:56 +02:00
|
|
|
[]*metricpb.ResourceMetrics{
|
|
|
|
{
|
|
|
|
Resource: nil,
|
|
|
|
InstrumentationLibraryMetrics: []*metricpb.InstrumentationLibraryMetrics{
|
|
|
|
{
|
|
|
|
Metrics: []*metricpb.Metric{
|
|
|
|
{
|
|
|
|
Name: "float64-count",
|
|
|
|
Data: &metricpb.Metric_Sum{
|
|
|
|
Sum: &metricpb.Sum{
|
|
|
|
IsMonotonic: true,
|
|
|
|
AggregationTemporality: metricpb.AggregationTemporality_AGGREGATION_TEMPORALITY_CUMULATIVE,
|
|
|
|
DataPoints: []*metricpb.NumberDataPoint{
|
|
|
|
{
|
|
|
|
Value: &metricpb.NumberDataPoint_AsDouble{AsDouble: 11.0},
|
|
|
|
Attributes: cpu1Labels,
|
|
|
|
StartTimeUnixNano: startTime(),
|
|
|
|
TimeUnixNano: pointTime(),
|
|
|
|
},
|
|
|
|
{
|
|
|
|
Value: &metricpb.NumberDataPoint_AsDouble{AsDouble: 11.0},
|
|
|
|
Attributes: cpu1Labels,
|
|
|
|
StartTimeUnixNano: startTime(),
|
|
|
|
TimeUnixNano: pointTime(),
|
|
|
|
},
|
|
|
|
},
|
|
|
|
},
|
|
|
|
},
|
|
|
|
},
|
|
|
|
},
|
|
|
|
},
|
|
|
|
},
|
|
|
|
},
|
|
|
|
},
|
|
|
|
)
|
|
|
|
}
|
|
|
|
|
|
|
|
func TestResourceMetricGroupingExport(t *testing.T) {
|
|
|
|
runMetricExportTests(
|
|
|
|
t,
|
|
|
|
nil,
|
2021-08-13 00:44:58 +02:00
|
|
|
testerAResource,
|
2021-09-27 17:51:47 +02:00
|
|
|
[]testRecord{
|
|
|
|
record(
|
2021-06-11 22:25:56 +02:00
|
|
|
"int64-count",
|
2021-08-12 01:02:28 +02:00
|
|
|
sdkapi.CounterInstrumentKind,
|
2021-06-11 22:25:56 +02:00
|
|
|
number.Int64Kind,
|
|
|
|
append(baseKeyValues, cpuKey.Int(1)),
|
2021-09-27 17:51:47 +02:00
|
|
|
testLibName,
|
|
|
|
),
|
|
|
|
record(
|
2021-06-11 22:25:56 +02:00
|
|
|
"int64-count",
|
2021-08-12 01:02:28 +02:00
|
|
|
sdkapi.CounterInstrumentKind,
|
2021-06-11 22:25:56 +02:00
|
|
|
number.Int64Kind,
|
|
|
|
append(baseKeyValues, cpuKey.Int(1)),
|
2021-09-27 17:51:47 +02:00
|
|
|
testLibName,
|
|
|
|
),
|
|
|
|
record(
|
2021-06-11 22:25:56 +02:00
|
|
|
"int64-count",
|
2021-08-12 01:02:28 +02:00
|
|
|
sdkapi.CounterInstrumentKind,
|
2021-06-11 22:25:56 +02:00
|
|
|
number.Int64Kind,
|
|
|
|
append(baseKeyValues, cpuKey.Int(2)),
|
2021-09-27 17:51:47 +02:00
|
|
|
testLibName,
|
|
|
|
),
|
|
|
|
record(
|
2021-06-11 22:25:56 +02:00
|
|
|
"int64-count",
|
2021-08-12 01:02:28 +02:00
|
|
|
sdkapi.CounterInstrumentKind,
|
2021-06-11 22:25:56 +02:00
|
|
|
number.Int64Kind,
|
|
|
|
append(baseKeyValues, cpuKey.Int(1)),
|
2021-09-27 17:51:47 +02:00
|
|
|
testLibName,
|
|
|
|
),
|
2021-06-11 22:25:56 +02:00
|
|
|
},
|
|
|
|
[]*metricpb.ResourceMetrics{
|
|
|
|
{
|
2021-08-13 00:44:58 +02:00
|
|
|
Resource: testerAResourcePb,
|
2021-06-11 22:25:56 +02:00
|
|
|
InstrumentationLibraryMetrics: []*metricpb.InstrumentationLibraryMetrics{
|
|
|
|
{
|
|
|
|
Metrics: []*metricpb.Metric{
|
|
|
|
{
|
|
|
|
Name: "int64-count",
|
|
|
|
Data: &metricpb.Metric_Sum{
|
|
|
|
Sum: &metricpb.Sum{
|
|
|
|
IsMonotonic: true,
|
|
|
|
AggregationTemporality: metricpb.AggregationTemporality_AGGREGATION_TEMPORALITY_CUMULATIVE,
|
|
|
|
DataPoints: []*metricpb.NumberDataPoint{
|
|
|
|
{
|
|
|
|
Value: &metricpb.NumberDataPoint_AsInt{AsInt: 11},
|
|
|
|
Attributes: cpu1Labels,
|
|
|
|
StartTimeUnixNano: startTime(),
|
|
|
|
TimeUnixNano: pointTime(),
|
|
|
|
},
|
|
|
|
{
|
|
|
|
Value: &metricpb.NumberDataPoint_AsInt{AsInt: 11},
|
|
|
|
Attributes: cpu1Labels,
|
|
|
|
StartTimeUnixNano: startTime(),
|
|
|
|
TimeUnixNano: pointTime(),
|
|
|
|
},
|
|
|
|
{
|
|
|
|
Value: &metricpb.NumberDataPoint_AsInt{AsInt: 11},
|
|
|
|
Attributes: cpu2Labels,
|
|
|
|
StartTimeUnixNano: startTime(),
|
|
|
|
TimeUnixNano: pointTime(),
|
|
|
|
},
|
|
|
|
{
|
|
|
|
Value: &metricpb.NumberDataPoint_AsInt{AsInt: 11},
|
|
|
|
Attributes: cpu1Labels,
|
|
|
|
StartTimeUnixNano: startTime(),
|
|
|
|
TimeUnixNano: pointTime(),
|
|
|
|
},
|
|
|
|
},
|
|
|
|
},
|
|
|
|
},
|
|
|
|
},
|
|
|
|
},
|
|
|
|
},
|
|
|
|
},
|
|
|
|
},
|
|
|
|
},
|
|
|
|
)
|
|
|
|
}
|
|
|
|
|
|
|
|
func TestResourceInstLibMetricGroupingExport(t *testing.T) {
|
2021-09-27 17:51:47 +02:00
|
|
|
version1 := metric.WithInstrumentationVersion("v1")
|
|
|
|
version2 := metric.WithInstrumentationVersion("v2")
|
|
|
|
specialSchema := metric.WithSchemaURL("schurl")
|
|
|
|
summingLib := "summing-lib"
|
|
|
|
countingLib := "counting-lib"
|
2021-06-11 22:25:56 +02:00
|
|
|
runMetricExportTests(
|
|
|
|
t,
|
|
|
|
nil,
|
2021-08-13 00:44:58 +02:00
|
|
|
testerAResource,
|
2021-09-27 17:51:47 +02:00
|
|
|
[]testRecord{
|
|
|
|
record(
|
2021-06-11 22:25:56 +02:00
|
|
|
"int64-count",
|
2021-08-12 01:02:28 +02:00
|
|
|
sdkapi.CounterInstrumentKind,
|
2021-06-11 22:25:56 +02:00
|
|
|
number.Int64Kind,
|
|
|
|
append(baseKeyValues, cpuKey.Int(1)),
|
2021-09-27 17:51:47 +02:00
|
|
|
countingLib,
|
|
|
|
version1,
|
|
|
|
),
|
|
|
|
record(
|
2021-06-11 22:25:56 +02:00
|
|
|
"int64-count",
|
2021-08-12 01:02:28 +02:00
|
|
|
sdkapi.CounterInstrumentKind,
|
2021-06-11 22:25:56 +02:00
|
|
|
number.Int64Kind,
|
|
|
|
append(baseKeyValues, cpuKey.Int(1)),
|
2021-09-27 17:51:47 +02:00
|
|
|
countingLib,
|
|
|
|
version2,
|
|
|
|
),
|
|
|
|
record(
|
2021-06-11 22:25:56 +02:00
|
|
|
"int64-count",
|
2021-08-12 01:02:28 +02:00
|
|
|
sdkapi.CounterInstrumentKind,
|
2021-06-11 22:25:56 +02:00
|
|
|
number.Int64Kind,
|
|
|
|
append(baseKeyValues, cpuKey.Int(1)),
|
2021-09-27 17:51:47 +02:00
|
|
|
countingLib,
|
|
|
|
version1,
|
|
|
|
),
|
|
|
|
record(
|
2021-06-11 22:25:56 +02:00
|
|
|
"int64-count",
|
2021-08-12 01:02:28 +02:00
|
|
|
sdkapi.CounterInstrumentKind,
|
2021-06-11 22:25:56 +02:00
|
|
|
number.Int64Kind,
|
|
|
|
append(baseKeyValues, cpuKey.Int(2)),
|
2021-09-27 17:51:47 +02:00
|
|
|
countingLib,
|
|
|
|
version1,
|
|
|
|
),
|
|
|
|
record(
|
2021-06-11 22:25:56 +02:00
|
|
|
"int64-count",
|
2021-08-12 01:02:28 +02:00
|
|
|
sdkapi.CounterInstrumentKind,
|
2021-06-11 22:25:56 +02:00
|
|
|
number.Int64Kind,
|
|
|
|
append(baseKeyValues, cpuKey.Int(1)),
|
2021-09-27 17:51:47 +02:00
|
|
|
summingLib,
|
|
|
|
specialSchema,
|
|
|
|
),
|
2021-06-11 22:25:56 +02:00
|
|
|
},
|
|
|
|
[]*metricpb.ResourceMetrics{
|
|
|
|
{
|
2021-08-13 00:44:58 +02:00
|
|
|
Resource: testerAResourcePb,
|
2021-06-11 22:25:56 +02:00
|
|
|
InstrumentationLibraryMetrics: []*metricpb.InstrumentationLibraryMetrics{
|
|
|
|
{
|
|
|
|
InstrumentationLibrary: &commonpb.InstrumentationLibrary{
|
|
|
|
Name: "counting-lib",
|
|
|
|
Version: "v1",
|
|
|
|
},
|
|
|
|
Metrics: []*metricpb.Metric{
|
|
|
|
{
|
|
|
|
Name: "int64-count",
|
|
|
|
Data: &metricpb.Metric_Sum{
|
|
|
|
Sum: &metricpb.Sum{
|
|
|
|
IsMonotonic: true,
|
|
|
|
AggregationTemporality: metricpb.AggregationTemporality_AGGREGATION_TEMPORALITY_CUMULATIVE,
|
|
|
|
DataPoints: []*metricpb.NumberDataPoint{
|
|
|
|
{
|
|
|
|
Value: &metricpb.NumberDataPoint_AsInt{AsInt: 11},
|
|
|
|
Attributes: cpu1Labels,
|
|
|
|
StartTimeUnixNano: startTime(),
|
|
|
|
TimeUnixNano: pointTime(),
|
|
|
|
},
|
|
|
|
{
|
|
|
|
Value: &metricpb.NumberDataPoint_AsInt{AsInt: 11},
|
|
|
|
Attributes: cpu1Labels,
|
|
|
|
StartTimeUnixNano: startTime(),
|
|
|
|
TimeUnixNano: pointTime(),
|
|
|
|
},
|
|
|
|
{
|
|
|
|
Value: &metricpb.NumberDataPoint_AsInt{AsInt: 11},
|
|
|
|
Attributes: cpu2Labels,
|
|
|
|
StartTimeUnixNano: startTime(),
|
|
|
|
TimeUnixNano: pointTime(),
|
|
|
|
},
|
|
|
|
},
|
|
|
|
},
|
|
|
|
},
|
|
|
|
},
|
|
|
|
},
|
|
|
|
},
|
|
|
|
{
|
|
|
|
InstrumentationLibrary: &commonpb.InstrumentationLibrary{
|
|
|
|
Name: "counting-lib",
|
|
|
|
Version: "v2",
|
|
|
|
},
|
|
|
|
Metrics: []*metricpb.Metric{
|
|
|
|
{
|
|
|
|
Name: "int64-count",
|
|
|
|
Data: &metricpb.Metric_Sum{
|
|
|
|
Sum: &metricpb.Sum{
|
|
|
|
IsMonotonic: true,
|
|
|
|
AggregationTemporality: metricpb.AggregationTemporality_AGGREGATION_TEMPORALITY_CUMULATIVE,
|
|
|
|
DataPoints: []*metricpb.NumberDataPoint{
|
|
|
|
{
|
|
|
|
Value: &metricpb.NumberDataPoint_AsInt{AsInt: 11},
|
|
|
|
Attributes: cpu1Labels,
|
|
|
|
StartTimeUnixNano: startTime(),
|
|
|
|
TimeUnixNano: pointTime(),
|
|
|
|
},
|
|
|
|
},
|
|
|
|
},
|
|
|
|
},
|
|
|
|
},
|
|
|
|
},
|
|
|
|
},
|
|
|
|
{
|
|
|
|
InstrumentationLibrary: &commonpb.InstrumentationLibrary{
|
|
|
|
Name: "summing-lib",
|
|
|
|
},
|
2021-09-27 17:51:47 +02:00
|
|
|
SchemaUrl: "schurl",
|
2021-06-11 22:25:56 +02:00
|
|
|
Metrics: []*metricpb.Metric{
|
|
|
|
{
|
|
|
|
Name: "int64-count",
|
|
|
|
Data: &metricpb.Metric_Sum{
|
|
|
|
Sum: &metricpb.Sum{
|
|
|
|
IsMonotonic: true,
|
|
|
|
AggregationTemporality: metricpb.AggregationTemporality_AGGREGATION_TEMPORALITY_CUMULATIVE,
|
|
|
|
DataPoints: []*metricpb.NumberDataPoint{
|
|
|
|
{
|
|
|
|
Value: &metricpb.NumberDataPoint_AsInt{AsInt: 11},
|
|
|
|
Attributes: cpu1Labels,
|
|
|
|
StartTimeUnixNano: startTime(),
|
|
|
|
TimeUnixNano: pointTime(),
|
|
|
|
},
|
|
|
|
},
|
|
|
|
},
|
|
|
|
},
|
|
|
|
},
|
|
|
|
},
|
|
|
|
},
|
|
|
|
},
|
|
|
|
},
|
|
|
|
},
|
|
|
|
)
|
|
|
|
}
|
|
|
|
|
2021-10-15 20:18:36 +02:00
|
|
|
func TestStatelessAggregationTemporality(t *testing.T) {
|
2021-06-11 22:25:56 +02:00
|
|
|
type testcase struct {
|
|
|
|
name string
|
2021-08-12 01:02:28 +02:00
|
|
|
instrumentKind sdkapi.InstrumentKind
|
2021-06-11 22:25:56 +02:00
|
|
|
aggTemporality metricpb.AggregationTemporality
|
|
|
|
monotonic bool
|
|
|
|
}
|
|
|
|
|
|
|
|
for _, k := range []testcase{
|
2021-08-12 01:02:28 +02:00
|
|
|
{"counter", sdkapi.CounterInstrumentKind, metricpb.AggregationTemporality_AGGREGATION_TEMPORALITY_DELTA, true},
|
|
|
|
{"updowncounter", sdkapi.UpDownCounterInstrumentKind, metricpb.AggregationTemporality_AGGREGATION_TEMPORALITY_DELTA, false},
|
2021-09-01 22:38:37 +02:00
|
|
|
{"counterobserver", sdkapi.CounterObserverInstrumentKind, metricpb.AggregationTemporality_AGGREGATION_TEMPORALITY_CUMULATIVE, true},
|
|
|
|
{"updowncounterobserver", sdkapi.UpDownCounterObserverInstrumentKind, metricpb.AggregationTemporality_AGGREGATION_TEMPORALITY_CUMULATIVE, false},
|
2021-06-11 22:25:56 +02:00
|
|
|
} {
|
|
|
|
t.Run(k.name, func(t *testing.T) {
|
|
|
|
runMetricExportTests(
|
|
|
|
t,
|
|
|
|
[]otlpmetric.Option{
|
2021-10-15 20:18:36 +02:00
|
|
|
otlpmetric.WithMetricAggregationTemporalitySelector(
|
|
|
|
aggregation.StatelessTemporalitySelector(),
|
2021-06-11 22:25:56 +02:00
|
|
|
),
|
|
|
|
},
|
2021-08-13 00:44:58 +02:00
|
|
|
testerAResource,
|
2021-09-27 17:51:47 +02:00
|
|
|
[]testRecord{
|
|
|
|
record(
|
2021-06-11 22:25:56 +02:00
|
|
|
"instrument",
|
|
|
|
k.instrumentKind,
|
|
|
|
number.Int64Kind,
|
|
|
|
append(baseKeyValues, cpuKey.Int(1)),
|
2021-09-27 17:51:47 +02:00
|
|
|
testLibName,
|
|
|
|
),
|
2021-06-11 22:25:56 +02:00
|
|
|
},
|
|
|
|
[]*metricpb.ResourceMetrics{
|
|
|
|
{
|
2021-08-13 00:44:58 +02:00
|
|
|
Resource: testerAResourcePb,
|
2021-06-11 22:25:56 +02:00
|
|
|
InstrumentationLibraryMetrics: []*metricpb.InstrumentationLibraryMetrics{
|
|
|
|
{
|
|
|
|
Metrics: []*metricpb.Metric{
|
|
|
|
{
|
|
|
|
Name: "instrument",
|
|
|
|
Data: &metricpb.Metric_Sum{
|
|
|
|
Sum: &metricpb.Sum{
|
|
|
|
IsMonotonic: k.monotonic,
|
|
|
|
AggregationTemporality: k.aggTemporality,
|
|
|
|
DataPoints: []*metricpb.NumberDataPoint{
|
|
|
|
{
|
|
|
|
Value: &metricpb.NumberDataPoint_AsInt{AsInt: 11},
|
|
|
|
Attributes: cpu1Labels,
|
|
|
|
StartTimeUnixNano: startTime(),
|
|
|
|
TimeUnixNano: pointTime(),
|
|
|
|
},
|
|
|
|
},
|
|
|
|
},
|
|
|
|
},
|
|
|
|
},
|
|
|
|
},
|
|
|
|
},
|
|
|
|
},
|
|
|
|
},
|
|
|
|
},
|
|
|
|
)
|
|
|
|
})
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2021-09-27 17:51:47 +02:00
|
|
|
func runMetricExportTests(t *testing.T, opts []otlpmetric.Option, res *resource.Resource, records []testRecord, expected []*metricpb.ResourceMetrics) {
|
2021-06-11 22:25:56 +02:00
|
|
|
exp, driver := newExporter(t, opts...)
|
|
|
|
|
2021-09-27 17:51:47 +02:00
|
|
|
libraryRecs := map[instrumentation.Library][]metricsdk.Record{}
|
2021-08-13 00:44:58 +02:00
|
|
|
for _, r := range records {
|
2021-06-11 22:25:56 +02:00
|
|
|
lcopy := make([]attribute.KeyValue, len(r.labels))
|
|
|
|
copy(lcopy, r.labels)
|
2021-09-27 17:51:47 +02:00
|
|
|
desc := metrictest.NewDescriptor(r.name, r.iKind, r.nKind)
|
2021-06-11 22:25:56 +02:00
|
|
|
labs := attribute.NewSet(lcopy...)
|
|
|
|
|
|
|
|
var agg, ckpt metricsdk.Aggregator
|
|
|
|
if r.iKind.Adding() {
|
2021-07-21 19:06:38 +02:00
|
|
|
sums := sum.New(2)
|
|
|
|
agg, ckpt = &sums[0], &sums[1]
|
2021-06-11 22:25:56 +02:00
|
|
|
} else {
|
2021-07-21 19:06:38 +02:00
|
|
|
histos := histogram.New(2, &desc, histogram.WithExplicitBoundaries(testHistogramBoundaries))
|
|
|
|
agg, ckpt = &histos[0], &histos[1]
|
2021-06-11 22:25:56 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
ctx := context.Background()
|
|
|
|
if r.iKind.Synchronous() {
|
|
|
|
// For synchronous instruments, perform two updates: 1 and 10
|
|
|
|
switch r.nKind {
|
|
|
|
case number.Int64Kind:
|
|
|
|
require.NoError(t, agg.Update(ctx, number.NewInt64Number(1), &desc))
|
|
|
|
require.NoError(t, agg.Update(ctx, number.NewInt64Number(10), &desc))
|
|
|
|
case number.Float64Kind:
|
|
|
|
require.NoError(t, agg.Update(ctx, number.NewFloat64Number(1), &desc))
|
|
|
|
require.NoError(t, agg.Update(ctx, number.NewFloat64Number(10), &desc))
|
|
|
|
default:
|
|
|
|
t.Fatalf("invalid number kind: %v", r.nKind)
|
|
|
|
}
|
|
|
|
} else {
|
|
|
|
// For asynchronous instruments, perform a single update: 11
|
|
|
|
switch r.nKind {
|
|
|
|
case number.Int64Kind:
|
|
|
|
require.NoError(t, agg.Update(ctx, number.NewInt64Number(11), &desc))
|
|
|
|
case number.Float64Kind:
|
|
|
|
require.NoError(t, agg.Update(ctx, number.NewFloat64Number(11), &desc))
|
|
|
|
default:
|
|
|
|
t.Fatalf("invalid number kind: %v", r.nKind)
|
|
|
|
}
|
|
|
|
}
|
|
|
|
require.NoError(t, agg.SynchronizedMove(ckpt, &desc))
|
|
|
|
|
2021-09-27 17:51:47 +02:00
|
|
|
meterCfg := metric.NewMeterConfig(r.meterOpts...)
|
|
|
|
lib := instrumentation.Library{
|
|
|
|
Name: r.meterName,
|
|
|
|
Version: meterCfg.InstrumentationVersion(),
|
|
|
|
SchemaURL: meterCfg.SchemaURL(),
|
|
|
|
}
|
|
|
|
libraryRecs[lib] = append(libraryRecs[lib], metricsdk.NewRecord(&desc, &labs, ckpt.Aggregation(), intervalStart, intervalEnd))
|
2021-06-11 22:25:56 +02:00
|
|
|
}
|
2021-09-27 17:51:47 +02:00
|
|
|
assert.NoError(t, exp.Export(context.Background(), res, processortest.MultiInstrumentationLibraryReader(libraryRecs)))
|
2021-06-11 22:25:56 +02:00
|
|
|
|
|
|
|
// assert.ElementsMatch does not equate nested slices of different order,
|
|
|
|
// therefore this requires the top level slice to be broken down.
|
|
|
|
// Build a map of Resource/InstrumentationLibrary pairs to Metrics, from
|
|
|
|
// that validate the metric elements match for all expected pairs. Finally,
|
|
|
|
// make we saw all expected pairs.
|
2021-08-13 00:44:58 +02:00
|
|
|
keyFor := func(ilm *metricpb.InstrumentationLibraryMetrics) string {
|
2021-09-27 17:51:47 +02:00
|
|
|
return fmt.Sprintf("%s/%s/%s", ilm.GetInstrumentationLibrary().GetName(), ilm.GetInstrumentationLibrary().GetVersion(), ilm.GetSchemaUrl())
|
2021-06-11 22:25:56 +02:00
|
|
|
}
|
2021-08-13 00:44:58 +02:00
|
|
|
got := map[string][]*metricpb.Metric{}
|
2021-06-11 22:25:56 +02:00
|
|
|
for _, rm := range driver.rm {
|
|
|
|
for _, ilm := range rm.InstrumentationLibraryMetrics {
|
2021-08-13 00:44:58 +02:00
|
|
|
k := keyFor(ilm)
|
|
|
|
got[k] = append(got[k], ilm.GetMetrics()...)
|
2021-06-11 22:25:56 +02:00
|
|
|
}
|
|
|
|
}
|
2021-08-13 00:44:58 +02:00
|
|
|
|
|
|
|
seen := map[string]struct{}{}
|
2021-06-11 22:25:56 +02:00
|
|
|
for _, rm := range expected {
|
|
|
|
for _, ilm := range rm.InstrumentationLibraryMetrics {
|
2021-08-13 00:44:58 +02:00
|
|
|
k := keyFor(ilm)
|
2021-06-11 22:25:56 +02:00
|
|
|
seen[k] = struct{}{}
|
|
|
|
g, ok := got[k]
|
|
|
|
if !ok {
|
2021-08-13 00:44:58 +02:00
|
|
|
t.Errorf("missing metrics for:\n\tInstrumentationLibrary: %q\n", k)
|
2021-06-11 22:25:56 +02:00
|
|
|
continue
|
|
|
|
}
|
|
|
|
if !assert.Len(t, g, len(ilm.GetMetrics())) {
|
|
|
|
continue
|
|
|
|
}
|
|
|
|
for i, expected := range ilm.GetMetrics() {
|
2021-08-13 00:44:58 +02:00
|
|
|
assert.Equal(t, "", cmp.Diff(expected, g[i], protocmp.Transform()))
|
2021-06-11 22:25:56 +02:00
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
for k := range got {
|
|
|
|
if _, ok := seen[k]; !ok {
|
2021-08-13 00:44:58 +02:00
|
|
|
t.Errorf("did not expect metrics for:\n\tInstrumentationLibrary: %s\n", k)
|
2021-06-11 22:25:56 +02:00
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
func TestEmptyMetricExport(t *testing.T) {
|
|
|
|
exp, driver := newExporter(t)
|
|
|
|
|
|
|
|
for _, test := range []struct {
|
|
|
|
records []metricsdk.Record
|
|
|
|
want []*metricpb.ResourceMetrics
|
|
|
|
}{
|
|
|
|
{
|
|
|
|
[]metricsdk.Record(nil),
|
|
|
|
[]*metricpb.ResourceMetrics(nil),
|
|
|
|
},
|
|
|
|
{
|
|
|
|
[]metricsdk.Record{},
|
|
|
|
[]*metricpb.ResourceMetrics(nil),
|
|
|
|
},
|
|
|
|
} {
|
|
|
|
driver.Reset()
|
2021-09-27 17:51:47 +02:00
|
|
|
require.NoError(t, exp.Export(context.Background(), resource.Empty(), processortest.MultiInstrumentationLibraryReader(map[instrumentation.Library][]metricsdk.Record{
|
|
|
|
{
|
|
|
|
Name: testLibName,
|
|
|
|
}: test.records,
|
|
|
|
})))
|
2021-06-11 22:25:56 +02:00
|
|
|
assert.Equal(t, test.want, driver.rm)
|
|
|
|
}
|
|
|
|
}
|