Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

pdata: Add support for optional double fields #4495

Closed
Closed
Show file tree
Hide file tree
Changes from 1 commit
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
4 changes: 2 additions & 2 deletions internal/testdata/metric.go
Original file line number Diff line number Diff line change
Expand Up @@ -234,14 +234,14 @@ func initHistogramMetric(hm pdata.Metric) {
hdp0.SetStartTimestamp(TestMetricStartTimestamp)
hdp0.SetTimestamp(TestMetricTimestamp)
hdp0.SetCount(1)
hdp0.SetSum(15)
hdp0.SetSum(pdata.NewOptionalDouble(15))

hdp1 := hdps.AppendEmpty()
initMetricAttributes2(hdp1.Attributes())
hdp1.SetStartTimestamp(TestMetricStartTimestamp)
hdp1.SetTimestamp(TestMetricTimestamp)
hdp1.SetCount(1)
hdp1.SetSum(15)
hdp1.SetSum(pdata.NewOptionalDouble(15))
hdp1.SetBucketCounts([]uint64{0, 1})
exemplar := hdp1.Exemplars().AppendEmpty()
exemplar.SetTimestamp(TestMetricExemplarTimestamp)
Expand Down
12 changes: 11 additions & 1 deletion model/internal/cmd/pdatagen/internal/metrics_structs.go
Original file line number Diff line number Diff line change
Expand Up @@ -19,13 +19,15 @@ var metricsFile = &File{
imports: []string{
`"sort"`,
``,
`"go.opentelemetry.io/collector/model/internal/data"`,
`otlpmetrics "go.opentelemetry.io/collector/model/internal/data/protogen/metrics/v1"`,
},
testImports: []string{
`"testing"`,
``,
`"github.com/stretchr/testify/assert"`,
``,
`"go.opentelemetry.io/collector/model/internal/data"`,
`otlpmetrics "go.opentelemetry.io/collector/model/internal/data/protogen/metrics/v1"`,
},
structs: []baseStruct{
Expand Down Expand Up @@ -250,14 +252,22 @@ var histogramDataPoint = &messageValueStruct{
startTimeField,
timeField,
countField,
doubleSumField,
optionalSumField,
bucketCountsField,
explicitBoundsField,
exemplarsField,
dataPointFlagsField,
},
}

var optionalSumField = &primitiveField{
fieldName: "Sum",
originFieldName: "Sum",
returnType: "*data.OptionalDouble",
defaultVal: "(*data.OptionalDouble)(nil)",
testVal: "NewOptionalDouble(17.13)",
}

var exponentialHistogramDataPointSlice = &sliceOfPtrs{
structName: "ExponentialHistogramDataPointSlice",
element: exponentialHistogramDataPoint,
Expand Down
52 changes: 52 additions & 0 deletions model/internal/data/optional_double.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,52 @@
// 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 data // import "go.opentelemetry.io/collector/model/internal/data"

// OptionalDouble is a custom data type that is used for all optional double fields in OTLP
// Protobuf messages.
type OptionalDouble struct {
orig float64
}

// Equal returns true if values are equal.
func (t OptionalDouble) Equal(that OptionalDouble) bool {
return t.orig == that.orig
}

// NewOptionalDouble creates an OptionalDouble from a float64.
func NewOptionalDouble(d float64) *OptionalDouble {
return &OptionalDouble{orig: d}
}

// TODO: implement all the methods below this line
func (t OptionalDouble) Marshal() ([]byte, error) {
return []byte{}, nil
}
func (t *OptionalDouble) MarshalTo(data []byte) (n int, err error) {
return 0, nil
}
func (t *OptionalDouble) Unmarshal(data []byte) error {
return nil
}
func (t *OptionalDouble) Size() int {
return 0
codeboten marked this conversation as resolved.
Show resolved Hide resolved
}

func (t OptionalDouble) MarshalJSON() ([]byte, error) {
return []byte{}, nil
}
func (t *OptionalDouble) UnmarshalJSON(data []byte) error {
return nil
}
446 changes: 310 additions & 136 deletions model/internal/data/protogen/metrics/v1/metrics.pb.go

Large diffs are not rendered by default.

4 changes: 3 additions & 1 deletion model/internal/data/protogen/trace/v1/trace.pb.go

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

3 changes: 2 additions & 1 deletion model/internal/otlp_wrapper.go
Original file line number Diff line number Diff line change
Expand Up @@ -15,6 +15,7 @@
package internal // import "go.opentelemetry.io/collector/model/internal"

import (
"go.opentelemetry.io/collector/model/internal/data"
otlpcommon "go.opentelemetry.io/collector/model/internal/data/protogen/common/v1"
otlplogs "go.opentelemetry.io/collector/model/internal/data/protogen/logs/v1"
otlpmetrics "go.opentelemetry.io/collector/model/internal/data/protogen/metrics/v1"
Expand Down Expand Up @@ -163,7 +164,7 @@ func intHistogramToHistogram(src *otlpmetrics.Metric_IntHistogram) *otlpmetrics.
TimeUnixNano: datapoint.TimeUnixNano,
Count: datapoint.Count,
StartTimeUnixNano: datapoint.StartTimeUnixNano,
Sum: float64(datapoint.Sum),
Sum: data.NewOptionalDouble(float64(datapoint.Sum)),
BucketCounts: datapoint.BucketCounts,
ExplicitBounds: datapoint.ExplicitBounds,
Exemplars: intExemplarToExemplar(datapoint.Exemplars),
Expand Down
9 changes: 5 additions & 4 deletions model/internal/otlp_wrappers_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -19,6 +19,7 @@ import (

"github.com/stretchr/testify/assert"

"go.opentelemetry.io/collector/model/internal/data"
otlpcommon "go.opentelemetry.io/collector/model/internal/data/protogen/common/v1"
otlpmetrics "go.opentelemetry.io/collector/model/internal/data/protogen/metrics/v1"
otlptrace "go.opentelemetry.io/collector/model/internal/data/protogen/trace/v1"
Expand Down Expand Up @@ -134,7 +135,7 @@ func TestDeprecatedIntHistogram(t *testing.T) {
},
BucketCounts: []uint64{11, 16, 2},
ExplicitBounds: []float64{3, 4},
Sum: 10.1,
Sum: *data.NewOptionalDouble(10.1),
StartTimeUnixNano: 0,
TimeUnixNano: 1,
Count: 29,
Expand All @@ -155,7 +156,7 @@ func TestDeprecatedIntHistogram(t *testing.T) {
},
BucketCounts: []uint64{11, 16, 2},
ExplicitBounds: []float64{3, 4},
Sum: 10.1,
Sum: *data.NewOptionalDouble(10.1),
StartTimeUnixNano: 0,
TimeUnixNano: 1,
Count: 29,
Expand Down Expand Up @@ -202,7 +203,7 @@ func TestDeprecatedIntHistogram(t *testing.T) {
},
BucketCounts: []uint64{10, 15, 1},
ExplicitBounds: []float64{1, 2},
Sum: 10.0,
Sum: *data.NewOptionalDouble(10.0),
StartTimeUnixNano: 2,
TimeUnixNano: 3,
Count: 26,
Expand Down Expand Up @@ -249,7 +250,7 @@ func TestDeprecatedIntHistogram(t *testing.T) {
},
BucketCounts: []uint64{10, 15, 1},
ExplicitBounds: []float64{1, 2},
Sum: 10.0,
Sum: *data.NewOptionalDouble(10.0),
StartTimeUnixNano: 2,
TimeUnixNano: 3,
Count: 26,
Expand Down
5 changes: 3 additions & 2 deletions model/pdata/generated_metrics.go

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

7 changes: 4 additions & 3 deletions model/pdata/generated_metrics_test.go

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

6 changes: 6 additions & 0 deletions model/pdata/metrics.go
Original file line number Diff line number Diff line change
Expand Up @@ -16,6 +16,7 @@ package pdata // import "go.opentelemetry.io/collector/model/pdata"

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

Expand Down Expand Up @@ -350,3 +351,8 @@ func (ms Exemplar) Type() MetricValueType {
}
return MetricValueTypeNone
}

// NewOptionalDouble creates an data.OptionalDouble from a float64.
func NewOptionalDouble(val float64) *data.OptionalDouble {
return data.NewOptionalDouble(val)
}
Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

  1. "*data.OptionalDouble" is internal so you need to have something like "type OptionalDouble = data.OptionalDouble" and return that at least.
  2. I think instead of changing HistogramDataPoint.Sum(OptionalDouble) we can have the same HistogramDataPoint.Sum() -> float64 and have a HistogramDataPoint.HasSum() -> bool?

11 changes: 10 additions & 1 deletion proto_patch.sed
Original file line number Diff line number Diff line change
Expand Up @@ -50,4 +50,13 @@ s+repeated Exemplar exemplars = \(.*\);+repeated Exemplar exemplars = \1\
[ (gogoproto.nullable) = false ];+g

s+Buckets \(.*\)tive = \(.*\);+Buckets \1tive = \2\
[ (gogoproto.nullable) = false ];+g
[ (gogoproto.nullable) = false ];+g

s+optional double \(.*\);+OptionalDouble \1\
[\
(gogoproto.customtype) = "go.opentelemetry.io/collector/model/internal/data.OptionalDouble"\
bogdandrutu marked this conversation as resolved.
Show resolved Hide resolved
];\
\
message OptionalDouble {\
double Sum = 1;\
}+g