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

Conversation

bogdandrutu
Copy link
Member

@bogdandrutu bogdandrutu commented Dec 1, 2021

Description:
This is the second attempt at adding support for optional double fields in opentelemetry-proto. This implementation uses a customtype field in gogo protos.

Link to tracking Issue: #4258

Testing: Added unit tests

This is the second attempt at adding support for `optional double` fields in opentelemetry-proto. This implementation uses a customtype field in gogo protos.
@bogdandrutu bogdandrutu requested review from a team and dmitryax December 1, 2021 00:25
@bogdandrutu bogdandrutu marked this pull request as draft December 1, 2021 00:25
Copy link
Member Author

@bogdandrutu bogdandrutu left a comment

Choose a reason for hiding this comment

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

The overall design is what I had in mind, only small details about how the "pdata" API exposes this.

model/internal/data/optional_double.go Outdated Show resolved Hide resolved
proto_patch.sed Show resolved Hide resolved
Comment on lines 356 to 358
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?

The following changes were made:
- make optional double field non-nullable
- update pdata interface to use float64 instead of OptionalDouble type
- added HasSum method to HistogramDataPoint
- added serialization methods to OptionalDouble
@codeboten
Copy link
Contributor

Updated the code based on the feedback. The following changes were made:

  • make optional double field non-nullable
  • update pdata interface to use float64 instead of OptionalDouble type
  • added HasSum method to HistogramDataPoint
  • added serialization methods to OptionalDouble

NOTE: If this looks like the way we want to go, I still need to update the template for generating the SetSum and Sum methods correctly for HistogramDataPoint.

@codecov
Copy link

codecov bot commented Dec 1, 2021

Codecov Report

Merging #4495 (7165cb5) into main (49b1fd6) will decrease coverage by 0.03%.
The diff coverage is 82.35%.

Impacted file tree graph

@@            Coverage Diff             @@
##             main    #4495      +/-   ##
==========================================
- Coverage   90.77%   90.74%   -0.04%     
==========================================
  Files         179      180       +1     
  Lines       10412    10443      +31     
==========================================
+ Hits         9452     9477      +25     
- Misses        743      749       +6     
  Partials      217      217              
Impacted Files Coverage Δ
model/pdata/metrics.go 97.40% <0.00%> (-1.29%) ⬇️
model/internal/data/optional_double.go 86.20% <86.20%> (ø)
model/internal/otlp_wrapper.go 92.02% <100.00%> (ø)
model/pdata/generated_metrics.go 96.98% <100.00%> (ø)

Continue to review full report at Codecov.

Legend - Click here to learn more
Δ = absolute <relative> (impact), ø = not affected, ? = missing data
Powered by Codecov. Last update 49b1fd6...7165cb5. Read the comment docs.

Copy link
Member Author

@bogdandrutu bogdandrutu left a comment

Choose a reason for hiding this comment

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

I tried a lot to make this work with typecast as well as customtype, none of them really worked :( since the generated code either changes the field type (which will change the generated code) or does not know how to handle a pointer for a primitive float64:

See:

	if m.Sum != nil {
		if m.Sum != 0 {
			i -= 8
			encoding_binary.LittleEndian.PutUint64(dAtA[i:], uint64(math.Float64bits(float64(m.Sum))))
			i--
			dAtA[i] = 0x29
		}
	}

This should be:

	if m.Sum != nil {
		i -= 8
		encoding_binary.LittleEndian.PutUint64(dAtA[i:], uint64(math.Float64bits(float64(*m.Sum))))
		i--
		dAtA[i] = 0x29
	}

We have some options:

  1. Change the generate code as well since there are 1-2 presence. We can do this to unblock the optional.
  2. Move forward with @jsuereth prototype.
  3. Try to use https://github.com/planetscale/vtprotobuf, it seems to be an active community and probably we can make further improvements there.
  4. My long-term dream do a lazy custom parser. Probably lots of work for the moment.

@@ -234,14 +234,14 @@ func initHistogramMetric(hm pdata.Metric) {
hdp0.SetStartTimestamp(TestMetricStartTimestamp)
hdp0.SetTimestamp(TestMetricTimestamp)
hdp0.SetCount(1)
hdp0.SetSum(15)
hdp0.SetSum(float64(15))
Copy link
Member Author

Choose a reason for hiding this comment

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

Why this change?

// OptionalDouble is a custom data type that is used for all optional double fields in OTLP
// Protobuf messages.
type OptionalDouble struct {
orig *types.DoubleValue
Copy link
Member Author

Choose a reason for hiding this comment

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

Why pointer? Feels like unnecessary allocation.


// MarshalTo converts OptionalDouble into a binary representation. Called by Protobuf serialization.
func (m *OptionalDouble) MarshalTo(data []byte) (n int, err error) {
return m.orig.MarshalTo(data)
Copy link
Member Author

Choose a reason for hiding this comment

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

This is incorrect since the types.DoubleValue is a message with the float at ID=1 (see the https://github.com/gogo/protobuf/blob/v1.3.2/types/wrappers.pb.go#L1263). You need to only put the float value the ID will be filled by the parent.

@codeboten
Copy link
Contributor

  1. Change the generate code as well since there are 1-2 presence. We can do this to unblock the optional.

    1. Move forward with @jsuereth prototype.

    2. Try to use https://github.com/planetscale/vtprotobuf, it seems to be an active community and probably we can make further improvements there.

    3. My long-term dream do a lazy custom parser. Probably lots of work for the moment.

@bogdandrutu @jsuereth

1 : so far this option seems pretty hacky, I could continue down this road but I'm not sure if I should.

2 & 3: if optional field support is just a one off that will allow us to move forward for some amount of time, then maybe it's not worth the effort to move off of gogo. On the other hand if it's going to be a repeated amount of work to continue adding features as protos evolve, maybe it's worth the investigation to see if the performance of vtproto would be acceptable for the collector.

4: my long term dream is not thinking about protobuf 🤣

Copy link
Contributor

@MovieStoreGuy MovieStoreGuy left a comment

Choose a reason for hiding this comment

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

I just had a comment on the library being used here.

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

import (
"github.com/gogo/protobuf/types"
Copy link
Contributor

Choose a reason for hiding this comment

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

Hey @bogdandrutu ,

This project is currently unmaintained, is there any reason why not to use https://github.com/golang/protobuf ?

Copy link
Contributor

@codeboten codeboten Dec 3, 2021

Choose a reason for hiding this comment

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

@MovieStoreGuy you can see more details on the discussion about what to do w/ gogo in the proto repo PR open-telemetry/opentelemetry-proto#336

Ultimately it comes down to performance, which is why we're investigating alternatives.

@github-actions
Copy link
Contributor

This PR was marked stale due to lack of activity. It will be closed in 7 days.

@github-actions
Copy link
Contributor

This PR was marked stale due to lack of activity. It will be closed in 14 days.

@bogdandrutu
Copy link
Member Author

@codeboten not sure if this is still needed, I think we discussed what we needed. Feel free to reopen.

@CatherineF-dev
Copy link
Contributor

CatherineF-dev commented May 23, 2022

Thx, it's not related.


Hi Bogdan, do you think it's related to #5409 or not? Thanks!

@codeboten codeboten deleted the codeboten/support-optional-2 branch June 22, 2023 15:29
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

4 participants