Skip to content

Commit

Permalink
pdata: deprecate funcs working with InternalRep
Browse files Browse the repository at this point in the history
This PR tries to respect the rules 100%, but the need to keep the deprecated public funcs is unclear since they should not/ cannot be used externally.

Signed-off-by: Bogdan Drutu <[email protected]>
  • Loading branch information
bogdandrutu committed Mar 4, 2022
1 parent 6898a92 commit b61993c
Show file tree
Hide file tree
Showing 19 changed files with 142 additions and 153 deletions.
1 change: 1 addition & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,7 @@
### 🛑 Breaking changes 🛑

- Remove `Type` funcs in pdata (#4933)
- pdata: deprecate funcs working with InternalRep (#4957)

## 🧰 Bug fixes 🧰

Expand Down
60 changes: 6 additions & 54 deletions model/internal/otlp_wrapper.go
Original file line number Diff line number Diff line change
Expand Up @@ -14,59 +14,11 @@

package internal // import "go.opentelemetry.io/collector/model/internal"

import (
otlplogs "go.opentelemetry.io/collector/model/internal/data/protogen/logs/v1"
otlpmetrics "go.opentelemetry.io/collector/model/internal/data/protogen/metrics/v1"
otlptrace "go.opentelemetry.io/collector/model/internal/data/protogen/trace/v1"
)
// Deprecated: [v0.47.0] will be removed soon, only used internally.
type MetricsWrapper struct{}

// MetricsWrapper is an intermediary struct that is declared in an internal package
// as a way to prevent certain functions of pdata.Metrics data type to be callable by
// any code outside of this module.
type MetricsWrapper struct {
req *otlpmetrics.MetricsData
}
// Deprecated: [v0.47.0] will be removed soon, only used internally.
type TracesWrapper struct{}

// MetricsToOtlp internal helper to convert MetricsWrapper to protobuf representation.
func MetricsToOtlp(mw MetricsWrapper) *otlpmetrics.MetricsData {
return mw.req
}

// MetricsFromOtlp internal helper to convert protobuf representation to MetricsWrapper.
func MetricsFromOtlp(req *otlpmetrics.MetricsData) MetricsWrapper {
return MetricsWrapper{req: req}
}

// TracesWrapper is an intermediary struct that is declared in an internal package
// as a way to prevent certain functions of pdata.Traces data type to be callable by
// any code outside of this module.
type TracesWrapper struct {
req *otlptrace.TracesData
}

// TracesToOtlp internal helper to convert TracesWrapper to protobuf representation.
func TracesToOtlp(mw TracesWrapper) *otlptrace.TracesData {
return mw.req
}

// TracesFromOtlp internal helper to convert protobuf representation to TracesWrapper.
func TracesFromOtlp(req *otlptrace.TracesData) TracesWrapper {
return TracesWrapper{req: req}
}

// LogsWrapper is an intermediary struct that is declared in an internal package
// as a way to prevent certain functions of pdata.Logs data type to be callable by
// any code outside of this module.
type LogsWrapper struct {
req *otlplogs.LogsData
}

// LogsToOtlp internal helper to convert LogsWrapper to protobuf representation.
func LogsToOtlp(l LogsWrapper) *otlplogs.LogsData {
return l.req
}

// LogsFromOtlp internal helper to convert protobuf representation to LogsWrapper.
func LogsFromOtlp(req *otlplogs.LogsData) LogsWrapper {
return LogsWrapper{req: req}
}
// Deprecated: [v0.47.0] will be removed soon, only used internally.
type LogsWrapper struct{}
13 changes: 2 additions & 11 deletions model/internal/pdata/logs.go
Original file line number Diff line number Diff line change
Expand Up @@ -51,18 +51,9 @@ func NewLogs() Logs {
return Logs{orig: &otlplogs.LogsData{}}
}

// LogsFromInternalRep creates the internal Logs representation from the ProtoBuf. Should
// not be used outside this module. This is intended to be used only by OTLP exporter and
// File exporter, which legitimately need to work with OTLP Protobuf structs.
func LogsFromInternalRep(logs internal.LogsWrapper) Logs {
return Logs{orig: internal.LogsToOtlp(logs)}
}

// InternalRep returns internal representation of the logs. Should not be used outside
// this module. This is intended to be used only by OTLP exporter and File exporter,
// which legitimately need to work with OTLP Protobuf structs.
// Deprecated: [v0.47.0] will be removed soon, only used internally.
func (ld Logs) InternalRep() internal.LogsWrapper {
return internal.LogsFromOtlp(ld.orig)
return internal.LogsWrapper{}
}

// MoveTo moves all properties from the current struct to dest
Expand Down
4 changes: 1 addition & 3 deletions model/internal/pdata/logs_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -22,7 +22,6 @@ import (
goproto "google.golang.org/protobuf/proto"
"google.golang.org/protobuf/types/known/emptypb"

"go.opentelemetry.io/collector/model/internal"
otlplogs "go.opentelemetry.io/collector/model/internal/data/protogen/logs/v1"
)

Expand Down Expand Up @@ -76,8 +75,7 @@ func TestLogRecordCountWithEmpty(t *testing.T) {
}

func TestToFromLogProto(t *testing.T) {
wrapper := internal.LogsFromOtlp(&otlplogs.LogsData{})
logs := LogsFromInternalRep(wrapper)
logs := LogsFromOtlp(&otlplogs.LogsData{})
assert.EqualValues(t, NewLogs(), logs)
assert.EqualValues(t, &otlplogs.LogsData{}, logs.orig)
}
Expand Down
11 changes: 2 additions & 9 deletions model/internal/pdata/metrics.go
Original file line number Diff line number Diff line change
Expand Up @@ -51,16 +51,9 @@ func NewMetrics() Metrics {
return Metrics{orig: &otlpmetrics.MetricsData{}}
}

// MetricsFromInternalRep creates Metrics from the internal representation.
// Should not be used outside this module.
func MetricsFromInternalRep(wrapper internal.MetricsWrapper) Metrics {
return Metrics{orig: internal.MetricsToOtlp(wrapper)}
}

// InternalRep returns internal representation of the Metrics.
// Should not be used outside this module.
// Deprecated: [v0.47.0] will be removed soon, only used internally.
func (md Metrics) InternalRep() internal.MetricsWrapper {
return internal.MetricsFromOtlp(md.orig)
return internal.MetricsWrapper{}
}

// Clone returns a copy of MetricData.
Expand Down
47 changes: 23 additions & 24 deletions model/internal/pdata/metrics_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -22,7 +22,6 @@ import (
goproto "google.golang.org/protobuf/proto"
"google.golang.org/protobuf/types/known/emptypb"

"go.opentelemetry.io/collector/model/internal"
otlpcommon "go.opentelemetry.io/collector/model/internal/data/protogen/common/v1"
otlpmetrics "go.opentelemetry.io/collector/model/internal/data/protogen/metrics/v1"
otlpresource "go.opentelemetry.io/collector/model/internal/data/protogen/resource/v1"
Expand Down Expand Up @@ -290,7 +289,7 @@ func TestOtlpToInternalReadOnly(t *testing.T) {
}

func TestOtlpToFromInternalReadOnly(t *testing.T) {
md := MetricsFromInternalRep(internal.MetricsFromOtlp(&otlpmetrics.MetricsData{
md := MetricsFromOtlp(&otlpmetrics.MetricsData{
ResourceMetrics: []*otlpmetrics.ResourceMetrics{
{
Resource: generateTestProtoResource(),
Expand All @@ -302,7 +301,7 @@ func TestOtlpToFromInternalReadOnly(t *testing.T) {
},
},
},
}))
})
// Test that nothing changed
assert.EqualValues(t, &otlpmetrics.MetricsData{
ResourceMetrics: []*otlpmetrics.ResourceMetrics{
Expand All @@ -316,13 +315,13 @@ func TestOtlpToFromInternalReadOnly(t *testing.T) {
},
},
},
}, internal.MetricsToOtlp(md.InternalRep()))
}, MetricsToOtlp(md))
}

func TestOtlpToFromInternalGaugeMutating(t *testing.T) {
newAttributes := NewAttributeMapFromMap(map[string]AttributeValue{"k": NewAttributeValueString("v")})

md := MetricsFromInternalRep(internal.MetricsFromOtlp(&otlpmetrics.MetricsData{
md := MetricsFromOtlp(&otlpmetrics.MetricsData{
ResourceMetrics: []*otlpmetrics.ResourceMetrics{
{
Resource: generateTestProtoResource(),
Expand All @@ -334,7 +333,7 @@ func TestOtlpToFromInternalGaugeMutating(t *testing.T) {
},
},
},
}))
})
resourceMetrics := md.ResourceMetrics()
metric := resourceMetrics.At(0).InstrumentationLibraryMetrics().At(0).Metrics().At(0)
// Mutate MetricDescriptor
Expand Down Expand Up @@ -399,13 +398,13 @@ func TestOtlpToFromInternalGaugeMutating(t *testing.T) {
},
},
},
}, internal.MetricsToOtlp(md.InternalRep()))
}, MetricsToOtlp(md))
}

func TestOtlpToFromInternalSumMutating(t *testing.T) {
newAttributes := NewAttributeMapFromMap(map[string]AttributeValue{"k": NewAttributeValueString("v")})

md := MetricsFromInternalRep(internal.MetricsFromOtlp(&otlpmetrics.MetricsData{
md := MetricsFromOtlp(&otlpmetrics.MetricsData{
ResourceMetrics: []*otlpmetrics.ResourceMetrics{
{
Resource: generateTestProtoResource(),
Expand All @@ -417,7 +416,7 @@ func TestOtlpToFromInternalSumMutating(t *testing.T) {
},
},
},
}))
})
resourceMetrics := md.ResourceMetrics()
metric := resourceMetrics.At(0).InstrumentationLibraryMetrics().At(0).Metrics().At(0)
// Mutate MetricDescriptor
Expand Down Expand Up @@ -484,13 +483,13 @@ func TestOtlpToFromInternalSumMutating(t *testing.T) {
},
},
},
}, internal.MetricsToOtlp(md.InternalRep()))
}, MetricsToOtlp(md))
}

func TestOtlpToFromInternalHistogramMutating(t *testing.T) {
newAttributes := NewAttributeMapFromMap(map[string]AttributeValue{"k": NewAttributeValueString("v")})

md := MetricsFromInternalRep(internal.MetricsFromOtlp(&otlpmetrics.MetricsData{
md := MetricsFromOtlp(&otlpmetrics.MetricsData{
ResourceMetrics: []*otlpmetrics.ResourceMetrics{
{
Resource: generateTestProtoResource(),
Expand All @@ -502,7 +501,7 @@ func TestOtlpToFromInternalHistogramMutating(t *testing.T) {
},
},
},
}))
})
resourceMetrics := md.ResourceMetrics()
metric := resourceMetrics.At(0).InstrumentationLibraryMetrics().At(0).Metrics().At(0)
// Mutate MetricDescriptor
Expand Down Expand Up @@ -568,13 +567,13 @@ func TestOtlpToFromInternalHistogramMutating(t *testing.T) {
},
},
},
}, internal.MetricsToOtlp(md.InternalRep()))
}, MetricsToOtlp(md))
}

func TestOtlpToFromInternalExponentialHistogramMutating(t *testing.T) {
newAttributes := NewAttributeMapFromMap(map[string]AttributeValue{"k": NewAttributeValueString("v")})

md := MetricsFromInternalRep(internal.MetricsFromOtlp(&otlpmetrics.MetricsData{
md := MetricsFromOtlp(&otlpmetrics.MetricsData{
ResourceMetrics: []*otlpmetrics.ResourceMetrics{
{
Resource: generateTestProtoResource(),
Expand All @@ -586,7 +585,7 @@ func TestOtlpToFromInternalExponentialHistogramMutating(t *testing.T) {
},
},
},
}))
})
resourceMetrics := md.ResourceMetrics()
metric := resourceMetrics.At(0).InstrumentationLibraryMetrics().At(0).Metrics().At(0)
// Mutate MetricDescriptor
Expand Down Expand Up @@ -647,7 +646,7 @@ func TestOtlpToFromInternalExponentialHistogramMutating(t *testing.T) {
},
},
},
}, internal.MetricsToOtlp(md.InternalRep()))
}, MetricsToOtlp(md))
}

func TestMetricsClone(t *testing.T) {
Expand Down Expand Up @@ -699,8 +698,8 @@ func BenchmarkOtlpToFromInternal_PassThrough(b *testing.B) {

b.ResetTimer()
for n := 0; n < b.N; n++ {
md := MetricsFromInternalRep(internal.MetricsFromOtlp(req))
newReq := internal.MetricsToOtlp(md.InternalRep())
md := MetricsFromOtlp(req)
newReq := MetricsToOtlp(md)
if len(req.ResourceMetrics) != len(newReq.ResourceMetrics) {
b.Fail()
}
Expand All @@ -724,9 +723,9 @@ func BenchmarkOtlpToFromInternal_Gauge_MutateOneLabel(b *testing.B) {

b.ResetTimer()
for n := 0; n < b.N; n++ {
md := MetricsFromInternalRep(internal.MetricsFromOtlp(req))
md := MetricsFromOtlp(req)
md.ResourceMetrics().At(0).InstrumentationLibraryMetrics().At(0).Metrics().At(0).Gauge().DataPoints().At(0).Attributes().UpsertString("key0", "value2")
newReq := internal.MetricsToOtlp(md.InternalRep())
newReq := MetricsToOtlp(md)
if len(req.ResourceMetrics) != len(newReq.ResourceMetrics) {
b.Fail()
}
Expand All @@ -750,9 +749,9 @@ func BenchmarkOtlpToFromInternal_Sum_MutateOneLabel(b *testing.B) {

b.ResetTimer()
for n := 0; n < b.N; n++ {
md := MetricsFromInternalRep(internal.MetricsFromOtlp(req))
md := MetricsFromOtlp(req)
md.ResourceMetrics().At(0).InstrumentationLibraryMetrics().At(0).Metrics().At(0).Sum().DataPoints().At(0).Attributes().UpsertString("key0", "value2")
newReq := internal.MetricsToOtlp(md.InternalRep())
newReq := MetricsToOtlp(md)
if len(req.ResourceMetrics) != len(newReq.ResourceMetrics) {
b.Fail()
}
Expand All @@ -776,9 +775,9 @@ func BenchmarkOtlpToFromInternal_HistogramPoints_MutateOneLabel(b *testing.B) {

b.ResetTimer()
for n := 0; n < b.N; n++ {
md := MetricsFromInternalRep(internal.MetricsFromOtlp(req))
md := MetricsFromOtlp(req)
md.ResourceMetrics().At(0).InstrumentationLibraryMetrics().At(0).Metrics().At(0).Histogram().DataPoints().At(0).Attributes().UpsertString("key0", "value2")
newReq := internal.MetricsToOtlp(md.InternalRep())
newReq := MetricsToOtlp(md)
if len(req.ResourceMetrics) != len(newReq.ResourceMetrics) {
b.Fail()
}
Expand Down
51 changes: 51 additions & 0 deletions model/internal/pdata/otlp_wrapper.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,51 @@
// 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 pdata // import "go.opentelemetry.io/collector/model/internal"

import (
otlplogs "go.opentelemetry.io/collector/model/internal/data/protogen/logs/v1"
otlpmetrics "go.opentelemetry.io/collector/model/internal/data/protogen/metrics/v1"
otlptrace "go.opentelemetry.io/collector/model/internal/data/protogen/trace/v1"
)

// MetricsToOtlp internal helper to convert Metrics to protobuf representation.
func MetricsToOtlp(mw Metrics) *otlpmetrics.MetricsData {
return mw.orig
}

// MetricsFromOtlp internal helper to convert protobuf representation to Metrics.
func MetricsFromOtlp(orig *otlpmetrics.MetricsData) Metrics {
return Metrics{orig: orig}
}

// TracesToOtlp internal helper to convert Traces to protobuf representation.
func TracesToOtlp(mw Traces) *otlptrace.TracesData {
return mw.orig
}

// TracesFromOtlp internal helper to convert protobuf representation to Traces.
func TracesFromOtlp(orig *otlptrace.TracesData) Traces {
return Traces{orig: orig}
}

// LogsToOtlp internal helper to convert Logs to protobuf representation.
func LogsToOtlp(l Logs) *otlplogs.LogsData {
return l.orig
}

// LogsFromOtlp internal helper to convert protobuf representation to Logs.
func LogsFromOtlp(orig *otlplogs.LogsData) Logs {
return Logs{orig: orig}
}
11 changes: 2 additions & 9 deletions model/internal/pdata/traces.go
Original file line number Diff line number Diff line change
Expand Up @@ -51,16 +51,9 @@ func NewTraces() Traces {
return Traces{orig: &otlptrace.TracesData{}}
}

// TracesFromInternalRep creates Traces from the internal representation.
// Should not be used outside this module.
func TracesFromInternalRep(wrapper internal.TracesWrapper) Traces {
return Traces{orig: internal.TracesToOtlp(wrapper)}
}

// InternalRep returns internal representation of the Traces.
// Should not be used outside this module.
// Deprecated: [v0.47.0] will be removed soon, only used internally.
func (td Traces) InternalRep() internal.TracesWrapper {
return internal.TracesFromOtlp(td.orig)
return internal.TracesWrapper{}
}

// MoveTo moves all properties from the current struct to dest
Expand Down
Loading

0 comments on commit b61993c

Please sign in to comment.