Skip to content

Conversation

@wsmoak
Copy link
Contributor

@wsmoak wsmoak commented Nov 2, 2025

When collected_metrics is empty, result_code does not get set, and so report_result would log an error with a blank code.

Since the exporters already log if they encounter problems, there is no need to repeat the error logging here.

Reported by: @utay

Related to: #1947 and #1888

When collected_metrics is empty, result_code was not getting set and so report_result would fail.

Reported by: @utay

Related to: open-telemetry#1947 and open-telemetry#1888
@wsmoak wsmoak changed the title Fix debug logging when there are no metrics to export fix: Add a debug log message when there are no metrics to export Nov 2, 2025
@wsmoak wsmoak marked this pull request as ready for review November 3, 2025 15:29
elsif result_code.nil?
OpenTelemetry.logger.debug 'No metrics to export'
else
OpenTelemetry.handle_error(exception: ExportError.new('Unable to export metrics'))
Copy link
Contributor

Choose a reason for hiding this comment

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

Would a case statement here be better and add a debug message for the default?

Copy link
Contributor Author

Choose a reason for hiding this comment

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

I was going for the simplest fix that could possibly work. To cover all the cases, I think it would be...

          def report_result(result_code)
            case result_code
            when Export::SUCCESS
              OpenTelemetry.logger.debug 'Successfully exported metrics'
            when Export::FAILURE
              OpenTelemetry.handle_error(exception: ExportError.new('Unable to export metrics'))
              OpenTelemetry.logger.error("Result code: #{result_code}")
            when Export::TIMEOUT
              OpenTelemetry.handle_error(exception: ExportError.new('Export operation timed out'))
              OpenTelemetry.logger.error("Result code: #{result_code}")
            when nil
              OpenTelemetry.logger.debug 'No metrics to export'
            else
              OpenTelemetry.logger.warn "Unknown result code when exporting metrics: #{result_code}"
            end
          end

Would you prefer that instead?

Copy link
Contributor

Choose a reason for hiding this comment

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

I think so yes.

Although handle_error would be enough for the error cases since it logs by default.

So I would suggest either choosing to use handle_error or logger in those cases but not both.

Copy link
Contributor Author

Choose a reason for hiding this comment

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

I agree that logging the result code doesn't add much value, I removed it.

@wsmoak
Copy link
Contributor Author

wsmoak commented Nov 4, 2025

Tested locally. Started the app, waited a bit and recorded a data point, then turned off the collector. (I am not sure how to make a timeout happen.)

D, [2025-11-03T19:22:15.205141 #59504] DEBUG -- : No metrics to export
D, [2025-11-03T19:23:15.223879 #59504] DEBUG -- : Successfully exported metrics
W, [2025-11-03T19:24:39.281246 #59504]  WARN -- : SystemCallError in MetricsExporter#send_bytes
E, [2025-11-03T19:24:39.284574 #59504] ERROR -- : OpenTelemetry error: Unable to export metrics

@wsmoak wsmoak requested a review from arielvalentin November 4, 2025 00:35
OpenTelemetry.logger.debug 'Successfully exported metrics'
elsif result_code.nil?
when Export::FAILURE
OpenTelemetry.handle_error(exception: ExportError.new('Unable to export metrics'))
Copy link
Contributor

Choose a reason for hiding this comment

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

Something to note here is that since this was not an exception then it will be missing a backtrace and that may confuse someone who is trying to handle the error:

irb(main):001> e = StandardError.new("bad")
=> #<StandardError: bad>
irb(main):002> e.backtrace
=> nil

The handle_error message does not need to have an exception: parameter and in this case may only need a message:.

Then again when I look at the exporter there is already a set of diagnositc errors there:

https://github.com/wsmoak/opentelemetry-ruby/blob/7419ef5888b488dcf2fbee769ae662eb7562c70b/exporter/otlp-metrics/lib/opentelemetry/exporter/otlp/metrics/metrics_exporter.rb#L136

So I guess my follow up question is... why does this also need to report errors in addition to the exporters. Is there some nuance I am missing?

Copy link
Contributor Author

Choose a reason for hiding this comment

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

All I really wanted was the success message. :)

I was following the pattern that Kayla set in the BLRP:

def report_result(result_code, batch)
if result_code == SUCCESS
OpenTelemetry.logger.debug("Successfully exported #{batch.size} log records")
else
OpenTelemetry.handle_error(exception: ExportError.new("Unable to export #{batch.size} log records"))
OpenTelemetry.logger.error("Result code: #{result_code}")
end
end

I can remove the logging for errors, and assume the thing that returned the error has already logged it.

Copy link
Contributor Author

Choose a reason for hiding this comment

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

... and it collapses down to only logging the success message. Since there is no error logging, the nil value doesn't accidentally fall into it. Problem solved!

@wsmoak wsmoak changed the title fix: Add a debug log message when there are no metrics to export fix: Do not log error when there are no metrics to export Nov 4, 2025
@wsmoak
Copy link
Contributor Author

wsmoak commented Nov 4, 2025

Tested running locally. The only output now (again with then without a collector running) is:

D, [2025-11-03T20:34:30.377782 #58928] DEBUG -- : Successfully exported metrics
W, [2025-11-03T20:35:56.420999 #58928]  WARN -- : SystemCallError in MetricsExporter#send_bytes

@wsmoak wsmoak requested a review from arielvalentin November 4, 2025 01:41
@kaylareopelle kaylareopelle merged commit f4f9a1c into open-telemetry:main Nov 4, 2025
65 checks passed
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