Skip to content

Commit

Permalink
Move previous internal metrics package to dataold (#1658)
Browse files Browse the repository at this point in the history
Need to re-generate the new internal metrics, but it is too much code to change,
so will move this to dataold (was internal anyway, so no public breaking change).

Signed-off-by: Bogdan Drutu <[email protected]>
  • Loading branch information
bogdandrutu authored Aug 28, 2020
1 parent aeb604f commit 42ac0ee
Show file tree
Hide file tree
Showing 106 changed files with 1,028 additions and 908 deletions.
39 changes: 17 additions & 22 deletions consumer/pdata/metric.go
Original file line number Diff line number Diff line change
Expand Up @@ -15,30 +15,10 @@
package pdata

import (
otlpmetrics "go.opentelemetry.io/collector/internal/data/opentelemetry-proto-gen/metrics/v1"
otlpcommon "go.opentelemetry.io/collector/internal/data/opentelemetry-proto-gen/common/v1"
otlpresource "go.opentelemetry.io/collector/internal/data/opentelemetry-proto-gen/resource/v1"
)

type MetricType otlpmetrics.MetricDescriptor_Type

const (
MetricTypeInvalid = MetricType(otlpmetrics.MetricDescriptor_INVALID_TYPE)
MetricTypeInt64 = MetricType(otlpmetrics.MetricDescriptor_INT64)
MetricTypeDouble = MetricType(otlpmetrics.MetricDescriptor_DOUBLE)
MetricTypeMonotonicInt64 = MetricType(otlpmetrics.MetricDescriptor_MONOTONIC_INT64)
MetricTypeMonotonicDouble = MetricType(otlpmetrics.MetricDescriptor_MONOTONIC_DOUBLE)
MetricTypeHistogram = MetricType(otlpmetrics.MetricDescriptor_HISTOGRAM)
MetricTypeSummary = MetricType(otlpmetrics.MetricDescriptor_SUMMARY)
)

func (mt MetricType) String() string {
return otlpmetrics.MetricDescriptor_Type(mt).String()
}

// InternalNewMetricsResourceSlice is a helper
func InternalNewMetricsResourceSlice(orig *[]*otlpmetrics.ResourceMetrics) ResourceMetricsSlice {
return newResourceMetricsSlice(orig)
}

// Metrics is an opaque interface that allows transition to the new internal Metrics data, but also facilitate the
// transition to the new components especially for traces.
//
Expand All @@ -49,3 +29,18 @@ func InternalNewMetricsResourceSlice(orig *[]*otlpmetrics.ResourceMetrics) Resou
type Metrics struct {
InternalOpaque interface{}
}

// DeprecatedNewResource temporary public function.
func DeprecatedNewResource(orig **otlpresource.Resource) Resource {
return newResource(orig)
}

// DeprecatedNewInstrumentationLibrary temporary public function.
func DeprecatedNewInstrumentationLibrary(orig **otlpcommon.InstrumentationLibrary) InstrumentationLibrary {
return newInstrumentationLibrary(orig)
}

// DeprecatedNewStringMap temporary public function.
func DeprecatedNewStringMap(orig *[]*otlpcommon.StringKeyValue) StringMap {
return newStringMap(orig)
}
59 changes: 0 additions & 59 deletions consumer/pdata/metric_test.go

This file was deleted.

16 changes: 8 additions & 8 deletions consumer/pdatautil/pdatautil.go
Original file line number Diff line number Diff line change
Expand Up @@ -24,7 +24,7 @@ import (

"go.opentelemetry.io/collector/consumer/consumerdata"
"go.opentelemetry.io/collector/consumer/pdata"
"go.opentelemetry.io/collector/internal/data"
"go.opentelemetry.io/collector/internal/dataold"
"go.opentelemetry.io/collector/translator/internaldata"
)

Expand All @@ -35,7 +35,7 @@ func MetricsToMetricsData(md pdata.Metrics) []consumerdata.MetricsData {
if cmd, ok := md.InternalOpaque.([]consumerdata.MetricsData); ok {
return cmd
}
if ims, ok := md.InternalOpaque.(data.MetricData); ok {
if ims, ok := md.InternalOpaque.(dataold.MetricData); ok {
return internaldata.MetricDataToOC(ims)
}
panic("Unsupported metrics type.")
Expand All @@ -51,8 +51,8 @@ func MetricsFromMetricsData(ocmds []consumerdata.MetricsData) pdata.Metrics {
// MetricsToInternalMetrics returns the `data.MetricData` representation of the `pdata.Metrics`.
//
// This is a temporary function that will be removed when the new internal pdata.Metrics will be finalized.
func MetricsToInternalMetrics(md pdata.Metrics) data.MetricData {
if ims, ok := md.InternalOpaque.(data.MetricData); ok {
func MetricsToInternalMetrics(md pdata.Metrics) dataold.MetricData {
if ims, ok := md.InternalOpaque.(dataold.MetricData); ok {
return ims
}
if cmd, ok := md.InternalOpaque.([]consumerdata.MetricsData); ok {
Expand All @@ -64,15 +64,15 @@ func MetricsToInternalMetrics(md pdata.Metrics) data.MetricData {
// MetricsFromMetricsData returns the `pdata.Metrics` representation of the `data.MetricData`.
//
// This is a temporary function that will be removed when the new internal pdata.Metrics will be finalized.
func MetricsFromInternalMetrics(md data.MetricData) pdata.Metrics {
func MetricsFromInternalMetrics(md dataold.MetricData) pdata.Metrics {
return pdata.Metrics{InternalOpaque: md}
}

// CloneMetrics returns a clone of the given `pdata.Metrics`.
//
// This is a temporary function that will be removed when the new internal pdata.Metrics will be finalized.
func CloneMetrics(md pdata.Metrics) pdata.Metrics {
if ims, ok := md.InternalOpaque.(data.MetricData); ok {
if ims, ok := md.InternalOpaque.(dataold.MetricData); ok {
return pdata.Metrics{InternalOpaque: ims.Clone()}
}
if ocmds, ok := md.InternalOpaque.([]consumerdata.MetricsData); ok {
Expand All @@ -86,7 +86,7 @@ func CloneMetrics(md pdata.Metrics) pdata.Metrics {
}

func MetricCount(md pdata.Metrics) int {
if ims, ok := md.InternalOpaque.(data.MetricData); ok {
if ims, ok := md.InternalOpaque.(dataold.MetricData); ok {
return ims.MetricCount()
}
if ocmds, ok := md.InternalOpaque.([]consumerdata.MetricsData); ok {
Expand All @@ -100,7 +100,7 @@ func MetricCount(md pdata.Metrics) int {
}

func MetricAndDataPointCount(md pdata.Metrics) (int, int) {
if ims, ok := md.InternalOpaque.(data.MetricData); ok {
if ims, ok := md.InternalOpaque.(dataold.MetricData); ok {
return ims.MetricAndDataPointCount()
}
if ocmds, ok := md.InternalOpaque.([]consumerdata.MetricsData); ok {
Expand Down
6 changes: 3 additions & 3 deletions consumer/pdatautil/pdatautil_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -24,11 +24,11 @@ import (

"go.opentelemetry.io/collector/consumer/consumerdata"
"go.opentelemetry.io/collector/consumer/pdata"
"go.opentelemetry.io/collector/internal/data/testdata"
"go.opentelemetry.io/collector/internal/dataold/testdataold"
)

func TestMetricCount(t *testing.T) {
metrics := pdata.Metrics{InternalOpaque: testdata.GenerateMetricDataTwoMetrics()}
metrics := pdata.Metrics{InternalOpaque: testdataold.GenerateMetricDataTwoMetrics()}
assert.Equal(t, 2, MetricCount(metrics))

metrics = pdata.Metrics{InternalOpaque: []consumerdata.MetricsData{
Expand All @@ -50,7 +50,7 @@ func TestMetricCount(t *testing.T) {
}

func TestMetricAndDataPointCount(t *testing.T) {
metrics := pdata.Metrics{InternalOpaque: testdata.GenerateMetricDataTwoMetrics()}
metrics := pdata.Metrics{InternalOpaque: testdataold.GenerateMetricDataTwoMetrics()}
metricsCount, dataPointsCount := MetricAndDataPointCount(metrics)
assert.Equal(t, 2, metricsCount)
assert.Equal(t, 4, dataPointsCount)
Expand Down
11 changes: 6 additions & 5 deletions exporter/exporterhelper/metricshelper_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -28,6 +28,7 @@ import (
"go.opentelemetry.io/collector/consumer/pdata"
"go.opentelemetry.io/collector/consumer/pdatautil"
"go.opentelemetry.io/collector/internal/data/testdata"
"go.opentelemetry.io/collector/internal/dataold/testdataold"
"go.opentelemetry.io/collector/obsreport"
"go.opentelemetry.io/collector/obsreport/obsreporttest"
)
Expand All @@ -46,7 +47,7 @@ var (
)

func TestMetricsRequest(t *testing.T) {
mr := newMetricsRequest(context.Background(), pdatautil.MetricsFromInternalMetrics(testdata.GenerateMetricDataEmpty()), nil)
mr := newMetricsRequest(context.Background(), pdatautil.MetricsFromInternalMetrics(testdataold.GenerateMetricDataEmpty()), nil)

partialErr := consumererror.PartialTracesError(errors.New("some error"), testdata.GenerateTraceDataOneSpan())
assert.Same(t, mr, mr.onPartialError(partialErr.(consumererror.PartialError)))
Expand All @@ -66,7 +67,7 @@ func TestMetricsExporter_NilPushMetricsData(t *testing.T) {
}

func TestMetricsExporter_Default(t *testing.T) {
md := testdata.GenerateMetricDataEmpty()
md := testdataold.GenerateMetricDataEmpty()
me, err := NewMetricsExporter(fakeMetricsExporterConfig, newPushMetricsData(0, nil))
assert.NotNil(t, me)
assert.NoError(t, err)
Expand All @@ -76,7 +77,7 @@ func TestMetricsExporter_Default(t *testing.T) {
}

func TestMetricsExporter_Default_ReturnError(t *testing.T) {
md := testdata.GenerateMetricDataEmpty()
md := testdataold.GenerateMetricDataEmpty()
want := errors.New("my_error")
me, err := NewMetricsExporter(fakeMetricsExporterConfig, newPushMetricsData(0, want))
require.Nil(t, err)
Expand Down Expand Up @@ -165,7 +166,7 @@ func checkRecordedMetricsForMetricsExporter(t *testing.T, me component.MetricsEx
require.NoError(t, err)
defer doneFn()

md := testdata.GenerateMetricDataTwoMetrics()
md := testdataold.GenerateMetricDataTwoMetrics()
const numBatches = 7
for i := 0; i < numBatches; i++ {
require.Equal(t, wantError, me.ConsumeMetrics(context.Background(), pdatautil.MetricsFromInternalMetrics(md)))
Expand All @@ -181,7 +182,7 @@ func checkRecordedMetricsForMetricsExporter(t *testing.T, me component.MetricsEx
}

func generateMetricsTraffic(t *testing.T, me component.MetricsExporter, numRequests int, wantError error) {
md := testdata.GenerateMetricDataOneMetricOneDataPoint()
md := testdataold.GenerateMetricDataOneMetricOneDataPoint()
ctx, span := trace.StartSpan(context.Background(), fakeMetricsParentSpanName, trace.WithSampler(trace.AlwaysSample()))
defer span.End()
for i := 0; i < numRequests; i++ {
Expand Down
4 changes: 2 additions & 2 deletions exporter/exportertest/nop_exporter_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -21,7 +21,7 @@ import (

"go.opentelemetry.io/collector/consumer/pdata"
"go.opentelemetry.io/collector/consumer/pdatautil"
"go.opentelemetry.io/collector/internal/data"
"go.opentelemetry.io/collector/internal/dataold"
)

func TestNopTraceExporter(t *testing.T) {
Expand All @@ -34,7 +34,7 @@ func TestNopTraceExporter(t *testing.T) {
func TestNopMetricsExporter(t *testing.T) {
nme := NewNopMetricsExporter()
require.NoError(t, nme.Start(context.Background(), nil))
require.NoError(t, nme.ConsumeMetrics(context.Background(), pdatautil.MetricsFromInternalMetrics(data.NewMetricData())))
require.NoError(t, nme.ConsumeMetrics(context.Background(), pdatautil.MetricsFromInternalMetrics(dataold.NewMetricData())))
require.NoError(t, nme.Shutdown(context.Background()))
}

Expand Down
5 changes: 3 additions & 2 deletions exporter/exportertest/sink_exporter_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -25,6 +25,7 @@ import (
"go.opentelemetry.io/collector/consumer/pdata"
"go.opentelemetry.io/collector/consumer/pdatautil"
"go.opentelemetry.io/collector/internal/data/testdata"
"go.opentelemetry.io/collector/internal/dataold/testdataold"
)

func TestSinkTraceExporter(t *testing.T) {
Expand Down Expand Up @@ -58,7 +59,7 @@ func TestSinkTraceExporter_Error(t *testing.T) {
func TestSinkMetricsExporter(t *testing.T) {
sink := new(SinkMetricsExporter)
require.NoError(t, sink.Start(context.Background(), componenttest.NewNopHost()))
md := testdata.GenerateMetricDataOneMetric()
md := testdataold.GenerateMetricDataOneMetric()
want := make([]pdata.Metrics, 0, 7)
for i := 0; i < 7; i++ {
require.NoError(t, sink.ConsumeMetrics(context.Background(), pdatautil.MetricsFromInternalMetrics(md)))
Expand All @@ -76,7 +77,7 @@ func TestSinkMetricsExporter_Error(t *testing.T) {
sink := new(SinkMetricsExporter)
require.NoError(t, sink.Start(context.Background(), componenttest.NewNopHost()))
sink.SetConsumeMetricsError(errors.New("my error"))
md := testdata.GenerateMetricDataOneMetric()
md := testdataold.GenerateMetricDataOneMetric()
require.Error(t, sink.ConsumeMetrics(context.Background(), pdatautil.MetricsFromInternalMetrics(md)))
assert.Len(t, sink.AllMetrics(), 0)
assert.Equal(t, 0, sink.MetricsCount())
Expand Down
4 changes: 2 additions & 2 deletions exporter/fileexporter/file_exporter.go
Original file line number Diff line number Diff line change
Expand Up @@ -25,10 +25,10 @@ import (
"go.opentelemetry.io/collector/component"
"go.opentelemetry.io/collector/consumer/pdata"
"go.opentelemetry.io/collector/consumer/pdatautil"
"go.opentelemetry.io/collector/internal/data"
otlplogs "go.opentelemetry.io/collector/internal/data/opentelemetry-proto-gen/collector/logs/v1"
otlpmetrics "go.opentelemetry.io/collector/internal/data/opentelemetry-proto-gen/collector/metrics/v1"
otlptrace "go.opentelemetry.io/collector/internal/data/opentelemetry-proto-gen/collector/trace/v1"
"go.opentelemetry.io/collector/internal/dataold"
)

// Marshaler configuration used for marhsaling Protobuf to JSON. Use default config.
Expand All @@ -50,7 +50,7 @@ func (e *fileExporter) ConsumeTraces(_ context.Context, td pdata.Traces) error {

func (e *fileExporter) ConsumeMetrics(_ context.Context, md pdata.Metrics) error {
request := otlpmetrics.ExportMetricsServiceRequest{
ResourceMetrics: data.MetricDataToOtlp(pdatautil.MetricsToInternalMetrics(md)),
ResourceMetrics: dataold.MetricDataToOtlp(pdatautil.MetricsToInternalMetrics(md)),
}
return exportMessageAsLine(e, &request)
}
Expand Down
7 changes: 4 additions & 3 deletions exporter/fileexporter/file_exporter_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -24,14 +24,15 @@ import (

"go.opentelemetry.io/collector/consumer/pdata"
"go.opentelemetry.io/collector/consumer/pdatautil"
"go.opentelemetry.io/collector/internal/data"
collectorlogs "go.opentelemetry.io/collector/internal/data/opentelemetry-proto-gen/collector/logs/v1"
collectormetrics "go.opentelemetry.io/collector/internal/data/opentelemetry-proto-gen/collector/metrics/v1"
collectortrace "go.opentelemetry.io/collector/internal/data/opentelemetry-proto-gen/collector/trace/v1"
otlpcommon "go.opentelemetry.io/collector/internal/data/opentelemetry-proto-gen/common/v1"
logspb "go.opentelemetry.io/collector/internal/data/opentelemetry-proto-gen/logs/v1"
otresourcepb "go.opentelemetry.io/collector/internal/data/opentelemetry-proto-gen/resource/v1"
"go.opentelemetry.io/collector/internal/data/testdata"
"go.opentelemetry.io/collector/internal/dataold"
"go.opentelemetry.io/collector/internal/dataold/testdataold"
"go.opentelemetry.io/collector/testutil"
)

Expand All @@ -57,15 +58,15 @@ func TestFileMetricsExporterNoErrors(t *testing.T) {
lme := &fileExporter{file: mf}
require.NotNil(t, lme)

md := pdatautil.MetricsFromInternalMetrics(testdata.GenerateMetricDataTwoMetrics())
md := pdatautil.MetricsFromInternalMetrics(testdataold.GenerateMetricDataTwoMetrics())
assert.NoError(t, lme.ConsumeMetrics(context.Background(), md))
assert.NoError(t, lme.Shutdown(context.Background()))

var unmarshaler = &jsonpb.Unmarshaler{}
var j collectormetrics.ExportMetricsServiceRequest
assert.NoError(t, unmarshaler.Unmarshal(mf, &j))

assert.EqualValues(t, data.MetricDataToOtlp(pdatautil.MetricsToInternalMetrics(md)), j.ResourceMetrics)
assert.EqualValues(t, dataold.MetricDataToOtlp(pdatautil.MetricsToInternalMetrics(md)), j.ResourceMetrics)
}

func TestFileLogsExporterNoErrors(t *testing.T) {
Expand Down
Loading

0 comments on commit 42ac0ee

Please sign in to comment.