Skip to content

Commit

Permalink
Deprecate pdata Clone methods in favor of CopyTo
Browse files Browse the repository at this point in the history
  • Loading branch information
dmitryax committed Sep 27, 2022
1 parent 380f1b1 commit 996f9bd
Show file tree
Hide file tree
Showing 7 changed files with 44 additions and 28 deletions.
7 changes: 4 additions & 3 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -34,9 +34,10 @@
- Deprecate `pmetric.Metric.DataType()` in favor of `pmetric.Metric.Type()`. (#6127)
- Deprecate `pmetric.NumberDataPoint.[Set]?[Int|Double]Val()` in favor of `pmetric.NumberDataPoint.[Set]?[Int|Double]Value()`. (#6134)
- Deprecate `pmetric.Exemplar.[Set]?[Int|Double]Val()` in favor of `pmetric.Exemplar.[Set]?[Int|Double]Value()`. (#6134)

### 🚩 Deprecations 🚩

- Deprecate pdata Clone methods in favor of CopyTo for consistency with other pdata structs (#6164)
- `pmetric.Metrics.Clone` is deprecated in favor of `pmetric.Metrics.CopyTo`
- `ptrace.Traces.Clone` is deprecated in favor of `pmetric.Traces.CopyTo`
- `plog.Logs.Clone` is deprecated in favor of `plogs.Logs.CopyTo`
- Rename all `pcommon.Value` getter/setter methods by removing `Val` suffix. (#6092)
- Old methods with `Val` suffix are deprecated.
- `StringVal` and `SetStringVal` are deprecated in favor of `Str` and `SetStr` to avoid implementing `fmt.Stringer` interface.
Expand Down
9 changes: 9 additions & 0 deletions pdata/plog/logs.go
Original file line number Diff line number Diff line change
Expand Up @@ -44,7 +44,16 @@ func (ms Logs) MoveTo(dest Logs) {
*ms.getOrig() = otlpcollectorlog.ExportLogsServiceRequest{}
}

// CopyTo copies all logs from ms to dest.
func (ms Logs) CopyTo(dest Logs) {
ms.ResourceLogs().CopyTo(dest.ResourceLogs())
}

// Clone returns a copy of Logs.
// Deprecated: [0.61.0] Replace with:
//
// newLogs := NewLogs()
// ms.CopyTo(newLogs)
func (ms Logs) Clone() Logs {
cloneLd := NewLogs()
ms.ResourceLogs().CopyTo(cloneLd.ResourceLogs())
Expand Down
12 changes: 4 additions & 8 deletions pdata/plog/logs_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -132,14 +132,10 @@ func TestLogsClone(t *testing.T) {
assert.EqualValues(t, logs, logs.Clone())
}

func BenchmarkLogsClone(b *testing.B) {
func TestLogsCopyTo(t *testing.T) {
logs := NewLogs()
internal.FillTestResourceLogsSlice(internal.ResourceLogsSlice(logs.ResourceLogs()))
b.ResetTimer()
for n := 0; n < b.N; n++ {
clone := logs.Clone()
if clone.ResourceLogs().Len() != logs.ResourceLogs().Len() {
b.Fail()
}
}
logsCopy := NewLogs()
logs.CopyTo(logsCopy)
assert.EqualValues(t, logs, logsCopy)
}
11 changes: 10 additions & 1 deletion pdata/pmetric/metrics.go
Original file line number Diff line number Diff line change
Expand Up @@ -37,7 +37,16 @@ func NewMetrics() Metrics {
return newMetrics(&otlpcollectormetrics.ExportMetricsServiceRequest{})
}

// Clone returns a copy of MetricData.
// CopyTo copies all metrics from ms to dest.
func (ms Metrics) CopyTo(dest Metrics) {
ms.ResourceMetrics().CopyTo(dest.ResourceMetrics())
}

// Clone returns a copy of Metrics.
// Deprecated: [0.61.0] Replace with:
//
// newMetrics := NewMetrics()
// ms.CopyTo(newMetrics)
func (ms Metrics) Clone() Metrics {
cloneMd := NewMetrics()
ms.ResourceMetrics().CopyTo(cloneMd.ResourceMetrics())
Expand Down
12 changes: 4 additions & 8 deletions pdata/pmetric/metrics_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -678,16 +678,12 @@ func TestMetricsClone(t *testing.T) {
assert.EqualValues(t, metrics, metrics.Clone())
}

func BenchmarkMetricsClone(b *testing.B) {
func TestMetricsCopyTo(t *testing.T) {
metrics := NewMetrics()
internal.FillTestResourceMetricsSlice(internal.ResourceMetricsSlice(metrics.ResourceMetrics()))
b.ResetTimer()
for n := 0; n < b.N; n++ {
clone := metrics.Clone()
if clone.ResourceMetrics().Len() != metrics.ResourceMetrics().Len() {
b.Fail()
}
}
metricsCopy := NewMetrics()
metrics.CopyTo(metricsCopy)
assert.EqualValues(t, metrics, metricsCopy)
}

func BenchmarkOtlpToFromInternal_PassThrough(b *testing.B) {
Expand Down
9 changes: 9 additions & 0 deletions pdata/ptrace/traces.go
Original file line number Diff line number Diff line change
Expand Up @@ -45,7 +45,16 @@ func (ms Traces) MoveTo(dest Traces) {
*ms.getOrig() = otlpcollectortrace.ExportTraceServiceRequest{}
}

// CopyTo copies all logs from ms to dest.
func (ms Traces) CopyTo(dest Traces) {
ms.ResourceSpans().CopyTo(dest.ResourceSpans())
}

// Clone returns a copy of Traces.
// Deprecated: [0.61.0] Replace with:
//
// newTraces := NewTraces()
// ms.CopyTo(newTraces)
func (ms Traces) Clone() Traces {
cloneTd := NewTraces()
ms.ResourceSpans().CopyTo(cloneTd.ResourceSpans())
Expand Down
12 changes: 4 additions & 8 deletions pdata/ptrace/traces_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -133,14 +133,10 @@ func TestTracesClone(t *testing.T) {
assert.EqualValues(t, traces, traces.Clone())
}

func BenchmarkTracesClone(b *testing.B) {
func TestTracesCopyTo(t *testing.T) {
traces := NewTraces()
internal.FillTestResourceSpansSlice(internal.ResourceSpansSlice(traces.ResourceSpans()))
b.ResetTimer()
for n := 0; n < b.N; n++ {
clone := traces.Clone()
if clone.ResourceSpans().Len() != traces.ResourceSpans().Len() {
b.Fail()
}
}
tracesCopy := NewTraces()
traces.CopyTo(tracesCopy)
assert.EqualValues(t, traces, tracesCopy)
}

0 comments on commit 996f9bd

Please sign in to comment.