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

exporter/metric: Add support to export Distribution with Exemplars and Exponential Histograms. #777

Merged
merged 31 commits into from
Mar 4, 2024

Conversation

franciscovalentecastro
Copy link
Contributor

@franciscovalentecastro franciscovalentecastro commented Dec 11, 2023

This PR adds the following functionality to the exporter/metric :

  • Added support to export ExponentialHistogram.
  • Added support to export TypedValue Distribution (either Histogram and ExponentialHistogram) with exemplars.
  • Adds exponential_histogram example with a dashboard.json to visualize it.

Details :

Copy link

codecov bot commented Dec 11, 2023

Codecov Report

Attention: Patch coverage is 4.21053% with 91 lines in your changes are missing coverage. Please review.

Project coverage is 59.79%. Comparing base (87124ac) to head (1590aff).

Files Patch % Lines
exporter/metric/metric.go 4.21% 89 Missing and 2 partials ⚠️
Additional details and impacted files
@@            Coverage Diff             @@
##             main     #777      +/-   ##
==========================================
- Coverage   60.73%   59.79%   -0.95%     
==========================================
  Files          56       56              
  Lines        5649     5743      +94     
==========================================
+ Hits         3431     3434       +3     
- Misses       2070     2159      +89     
- Partials      148      150       +2     

☔ View full report in Codecov by Sentry.
📢 Have feedback on the report? Share it here.

@franciscovalentecastro franciscovalentecastro changed the title [Draft] exporter/metric: Add support to export Distribution with Exemplars. [Draft] exporter/metric: Add support to export Distribution with Exemplars and Exponential Histograms. Dec 12, 2023
@dashpole
Copy link
Contributor

The feature has been implemented in the OTel SDK: https://github.com/open-telemetry/opentelemetry-go/blob/main/sdk/metric/internal/x/README.md#exemplars

Let me know if you are interested in contributing this.

@franciscovalentecastro franciscovalentecastro changed the title [Draft] exporter/metric: Add support to export Distribution with Exemplars and Exponential Histograms. exporter/metric: Add support to export Distribution with Exemplars and Exponential Histograms. Feb 23, 2024
@franciscovalentecastro franciscovalentecastro marked this pull request as ready for review February 23, 2024 21:49
@franciscovalentecastro franciscovalentecastro requested a review from a team as a code owner February 23, 2024 21:49
func toDistributionExemplar[N int64 | float64](Exemplars []metricdata.Exemplar[N]) []*distribution.Distribution_Exemplar {
var exemplars []*distribution.Distribution_Exemplar
for _, e := range Exemplars {
exemplars = append(exemplars, &distribution.Distribution_Exemplar{Value: float64(e.Value), Timestamp: timestamppb.New(e.Time)})
Copy link
Contributor Author

Choose a reason for hiding this comment

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

Verified Distribution_Exemplar only supports float64 values : https://pkg.go.dev/google.golang.org/genproto/googleapis/api/distribution#Distribution_Exemplar

positiveBucketCounts := 0
growthFactor := math.Exp2(math.Exp2(-float64(hist.Scale)))
for i, v := range hist.PositiveBucket.Counts {
counts[i] = int64(v)
Copy link
Contributor Author

Choose a reason for hiding this comment

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

Opentelemetry Exponential Buckets support Negative Indices too, but AFAIU the TypedValue Exponential Buckets only support Positive Indices.

Copy link
Contributor

Choose a reason for hiding this comment

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

Thats correct. All negative observations and zero observations should be in the underflow bucket. See

underflow := point.ZeroCount()
negativeBuckets := point.Negative().BucketCounts()
for i := 0; i < negativeBuckets.Len(); i++ {
underflow += negativeBuckets.At(i)
}

Copy link
Contributor Author

Choose a reason for hiding this comment

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

Fixed. I used as reference the implementation of collector/metrics.go : exponentialHistogramPoint to reimplement all the mentioned details of expHistToDistribution in this review, but using the correct types and methods.

google.golang.org/protobuf v1.31.0 // indirect
)

replace github.com/GoogleCloudPlatform/opentelemetry-operations-go/exporter/metric => ../../../exporter/metric
Copy link
Contributor Author

Choose a reason for hiding this comment

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

Added this to pick current changes (before merging) to metric.go.

example/metric/exponential_histogram/example.go Outdated Show resolved Hide resolved
example/metric/exponential_histogram/example.go Outdated Show resolved Hide resolved
exporter/metric/metric.go Outdated Show resolved Hide resolved
positiveBucketCounts := 0
growthFactor := math.Exp2(math.Exp2(-float64(hist.Scale)))
for i, v := range hist.PositiveBucket.Counts {
counts[i] = int64(v)
Copy link
Contributor

Choose a reason for hiding this comment

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

Thats correct. All negative observations and zero observations should be in the underflow bucket. See

underflow := point.ZeroCount()
negativeBuckets := point.Negative().BucketCounts()
for i := 0; i < negativeBuckets.Len(); i++ {
underflow += negativeBuckets.At(i)
}

exporter/metric/metric.go Outdated Show resolved Hide resolved
@franciscovalentecastro
Copy link
Contributor Author

franciscovalentecastro commented Mar 1, 2024

@dashpole Update on last changes to PR :

  • Added dashboard.json to visualize the exponential_histogram example. This may conflict/be confused with the other dashboard.json in the above folder. Is this the correct place and/or naming ? (I imported manually to GCM and it works correctly).
  • Fixed the open comments.
  • Added a TODO for the missing []attachments to the exemplar exporting (AFAIU, this will add the span_id, etc, though i'm not very knowledgable about how to do this correctly).

example/metric/exponential_histogram/example.go Outdated Show resolved Hide resolved
example/metric/exponential_histogram/example.go Outdated Show resolved Hide resolved
exporter/metric/metric.go Show resolved Hide resolved
@dashpole
Copy link
Contributor

dashpole commented Mar 4, 2024

Follow-up tasks required:

  • README for the example
  • Unit testing
  • Exemplar attachments
  • SumOfSquaredDeviations

@franciscovalentecastro can you add the README? You are welcome to work on the other tasks as well if you have time. Otherwise, I'll make sure they get done. Just let me know which you'd like to tackle

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.

2 participants