Skip to content

Commit

Permalink
Fix: handle float value in NumberDataPoint
Browse files Browse the repository at this point in the history
It's currently possible to have a `Counter` with a `Float` value, but
this fails to export because the `NumberDataPoint` is always assumed to
be an `Integer` for the purposes of ProtoBuf encoding. The `.proto` for
a `NumberDataPoint` allows for a float value in the `as_double` key, so
there should be no reason that we should not be able to support `Float`
`Counter`s.

This commit adds support for `Float` values in `NumberDataPoint`, which
in turn adds support for `Float` `Counter`s. I also updated the
documentation in the structs for both `NumberDataPoint.value` and
`HistogramDataPoint.sum` to match the expected type based on the
`.proto`.

Adds a test to confirm that the metrics_exporter handles both `Integer`
and `Float` values for a `NumberDataPoint`.
  • Loading branch information
percygrunwald committed Nov 22, 2024
1 parent 08b1130 commit c703e36
Show file tree
Hide file tree
Showing 4 changed files with 29 additions and 5 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -284,13 +284,20 @@ def histogram_data_point(hdp)
end

def number_data_point(ndp)
Opentelemetry::Proto::Metrics::V1::NumberDataPoint.new(
args = {
attributes: ndp.attributes.map { |k, v| as_otlp_key_value(k, v) },
as_int: ndp.value,
start_time_unix_nano: ndp.start_time_unix_nano,
time_unix_nano: ndp.time_unix_nano,
exemplars: ndp.exemplars # exemplars not implemented yet from metrics sdk
)
}

if ndp.value.is_a?(Float)
args[:as_double] = ndp.value
else
args[:as_int] = ndp.value
end

Opentelemetry::Proto::Metrics::V1::NumberDataPoint.new(**args)
end

# may not need this
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -463,6 +463,23 @@
OpenTelemetry.logger = logger
end

it 'is able to encode NumberDataPoint with Integer or Float value' do
stub_request(:post, 'http://localhost:4318/v1/metrics').to_return(status: 200)

[1, 0.1234].each do |value|
ndp = OpenTelemetry::SDK::Metrics::Aggregation::NumberDataPoint.new
ndp.attributes = { 'a' => 'b' }
ndp.start_time_unix_nano = 0
ndp.time_unix_nano = 0
ndp.value = value

metrics_data = create_metrics_data(data_points: [ndp])

result = exporter.export([metrics_data])
_(result).must_equal(METRICS_SUCCESS)
end
end

it 'logs rpc.Status on bad request' do
log_stream = StringIO.new
logger = OpenTelemetry.logger
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -14,7 +14,7 @@ module Aggregation
:start_time_unix_nano, # Integer nanoseconds since Epoch
:time_unix_nano, # Integer nanoseconds since Epoch
:count, # Integer count is the number of values in the population. Must be non-negative.
:sum, # Integer sum of the values in the population. If count is zero then this field then this field must be zero
:sum, # Float sum of the values in the population. If count is zero then this field then this field must be zero
:bucket_counts, # optional Array[Integer] field contains the count values of histogram for each bucket.
:explicit_bounds, # Array[Float] specifies buckets with explicitly defined bounds for values.
:exemplars, # optional List of exemplars collected from measurements that were used to form the data point
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -11,7 +11,7 @@ module Aggregation
NumberDataPoint = Struct.new(:attributes, # Hash{String => String, Numeric, Boolean, Array<String, Numeric, Boolean>}
:start_time_unix_nano, # Integer nanoseconds since Epoch
:time_unix_nano, # Integer nanoseconds since Epoch
:value, # Integer
:value, # Numeric
:exemplars) # optional List of exemplars collected from measurements that were used to form the data point
end
end
Expand Down

0 comments on commit c703e36

Please sign in to comment.