mirror of
https://github.com/open-telemetry/opentelemetry-go.git
synced 2024-12-28 21:09:17 +02:00
151 lines
3.3 KiB
Go
151 lines
3.3 KiB
Go
// Copyright The OpenTelemetry Authors
|
|
// SPDX-License-Identifier: Apache-2.0
|
|
|
|
package opencensus // import "go.opentelemetry.io/otel/bridge/opencensus"
|
|
|
|
import (
|
|
"context"
|
|
"testing"
|
|
"time"
|
|
|
|
"github.com/stretchr/testify/require"
|
|
ocmetricdata "go.opencensus.io/metric/metricdata"
|
|
"go.opencensus.io/metric/metricproducer"
|
|
ocresource "go.opencensus.io/resource"
|
|
|
|
"go.opentelemetry.io/otel/attribute"
|
|
"go.opentelemetry.io/otel/sdk/instrumentation"
|
|
"go.opentelemetry.io/otel/sdk/metric/metricdata"
|
|
"go.opentelemetry.io/otel/sdk/metric/metricdata/metricdatatest"
|
|
)
|
|
|
|
func TestMetricProducer(t *testing.T) {
|
|
now := time.Now()
|
|
for _, tc := range []struct {
|
|
desc string
|
|
input []*ocmetricdata.Metric
|
|
expected []metricdata.ScopeMetrics
|
|
expectErr bool
|
|
}{
|
|
{
|
|
desc: "empty",
|
|
expected: nil,
|
|
},
|
|
{
|
|
desc: "success",
|
|
input: []*ocmetricdata.Metric{
|
|
{
|
|
Resource: &ocresource.Resource{
|
|
Labels: map[string]string{
|
|
"R1": "V1",
|
|
"R2": "V2",
|
|
},
|
|
},
|
|
TimeSeries: []*ocmetricdata.TimeSeries{
|
|
{
|
|
StartTime: now,
|
|
Points: []ocmetricdata.Point{
|
|
{Value: int64(123), Time: now},
|
|
},
|
|
},
|
|
},
|
|
},
|
|
},
|
|
expected: []metricdata.ScopeMetrics{{
|
|
Scope: instrumentation.Scope{
|
|
Name: scopeName,
|
|
Version: Version(),
|
|
},
|
|
Metrics: []metricdata.Metrics{
|
|
{
|
|
Data: metricdata.Gauge[int64]{
|
|
DataPoints: []metricdata.DataPoint[int64]{
|
|
{
|
|
Attributes: attribute.NewSet(),
|
|
StartTime: now,
|
|
Time: now,
|
|
Value: 123,
|
|
},
|
|
},
|
|
},
|
|
},
|
|
},
|
|
}},
|
|
},
|
|
{
|
|
desc: "partial success",
|
|
input: []*ocmetricdata.Metric{
|
|
{
|
|
Descriptor: ocmetricdata.Descriptor{
|
|
Name: "foo.com/bad-point",
|
|
Description: "a bad type",
|
|
Unit: ocmetricdata.UnitDimensionless,
|
|
Type: ocmetricdata.TypeGaugeDistribution,
|
|
},
|
|
},
|
|
{
|
|
Resource: &ocresource.Resource{
|
|
Labels: map[string]string{
|
|
"R1": "V1",
|
|
"R2": "V2",
|
|
},
|
|
},
|
|
TimeSeries: []*ocmetricdata.TimeSeries{
|
|
{
|
|
StartTime: now,
|
|
Points: []ocmetricdata.Point{
|
|
{Value: int64(123), Time: now},
|
|
},
|
|
},
|
|
},
|
|
},
|
|
},
|
|
expected: []metricdata.ScopeMetrics{{
|
|
Scope: instrumentation.Scope{
|
|
Name: scopeName,
|
|
Version: Version(),
|
|
},
|
|
Metrics: []metricdata.Metrics{
|
|
{
|
|
Data: metricdata.Gauge[int64]{
|
|
DataPoints: []metricdata.DataPoint[int64]{
|
|
{
|
|
Attributes: attribute.NewSet(),
|
|
StartTime: now,
|
|
Time: now,
|
|
Value: 123,
|
|
},
|
|
},
|
|
},
|
|
},
|
|
},
|
|
}},
|
|
expectErr: true,
|
|
},
|
|
} {
|
|
t.Run(tc.desc, func(t *testing.T) {
|
|
fakeProducer := &fakeOCProducer{metrics: tc.input}
|
|
metricproducer.GlobalManager().AddProducer(fakeProducer)
|
|
defer metricproducer.GlobalManager().DeleteProducer(fakeProducer)
|
|
output, err := NewMetricProducer().Produce(context.Background())
|
|
if tc.expectErr {
|
|
require.Error(t, err)
|
|
} else {
|
|
require.Nil(t, err)
|
|
}
|
|
require.Equal(t, len(output), len(tc.expected))
|
|
for i := range output {
|
|
metricdatatest.AssertEqual(t, tc.expected[i], output[i])
|
|
}
|
|
})
|
|
}
|
|
}
|
|
|
|
type fakeOCProducer struct {
|
|
metrics []*ocmetricdata.Metric
|
|
}
|
|
|
|
func (f *fakeOCProducer) Read() []*ocmetricdata.Metric {
|
|
return f.metrics
|
|
}
|