opentelemetry-ruby
opentelemetry-ruby copied to clipboard
Improper trace format in console
Operating system details: "mac m1 pro" RUBY_ENGINE: "ruby" RUBY_VERSION: "2.7"
Hi, I configured an open telemetry agent in my ruby on rails project. I wanted to know what data to send the collector so I decide to configure the console exporter, the trace was printed, and I didn't know why the trace printing improper format anyone help me with this?
My configuration
# Load the Rails application.
require_relative 'application'
# config/initializers/opentelemetry.rb
require 'opentelemetry/sdk'
require 'opentelemetry/exporter/otlp'
require 'opentelemetry/instrumentation/all'
ENV['OTEL_TRACES_EXPORTER'] ||= 'console'
OpenTelemetry::SDK.configure do |c|
c.service_name = 'sample data'
c.use_all() # enables all instrumentation!
end
# Initialize the Rails application.
Rails.application.initialize!
Output trace
#<struct OpenTelemetry::SDK::Trace::SpanData
name="Film.find_by_sql",
kind=:internal,
status=
#<OpenTelemetry::Trace::Status:0x0000000126d591a8 @code=1, @description="">,
parent_span_id="'\xC5;.g\xA2\v\xEC",
total_recorded_attributes=0,
total_recorded_events=0,
total_recorded_links=0,
start_timestamp=1673509089909971000,
end_timestamp=1673509089910632000,
attributes={},
links=nil,
events=nil,
resource=
#<OpenTelemetry::SDK::Resources::Resource:0x000000011533d9f0
@attributes=
{"service.name"=>"sample data",
"process.pid"=>14671,
"process.command"=>"bin/rails",
"process.runtime.name"=>"ruby",
"process.runtime.version"=>"2.7.0",
"process.runtime.description"=>
"ruby 2.7.0p0 (2019-12-25 revision 647ee6f091) [-darwin21]",
"telemetry.sdk.name"=>"opentelemetry",
"telemetry.sdk.language"=>"ruby",
"telemetry.sdk.version"=>"1.2.0"}>,
instrumentation_scope=
#<struct OpenTelemetry::SDK::InstrumentationScope
name="OpenTelemetry::Instrumentation::ActiveRecord",
version="0.4.0">,
span_id="6`\xFF\x87\a\x18J\xCE",
trace_id="\xD8Z3\xF9'\xDD\x8E\xB38\xB6\xE7\x8EbF\xDC\xFA",
trace_flags=#<OpenTelemetry::Trace::TraceFlags:0x0000000123de1b00 @flags=1>,
tracestate=#<OpenTelemetry::Trace::Tracestate:0x0000000123dc7840 @hash={}>>
Would you be able to share a link or the requirement from the SDK specification thar explains why you think the console output is an improper format?
https://opentelemetry.io/docs/instrumentation/ruby/automatic/
I want to know why trace_id and span_id are encoded format, how to print that in JSON format
The Trace and Span ID is a series of bytes (binary format). We don't currently do anything special in the console exporter to convert the values to hexadecimal strings.
If you want to see the output in the console in JSON you will need to write your own console JSON exporter.
Another option is to patch the to_s method on SpanContext to convert the fields to their hex representation.
All that being said I don't think this is a bug unless there is something in the spec that requires the console exporter to output data in JSON format.
@arielvalentin node.js is printed the proper way like below, here no hexadecimal strings in console exporter
{
traceId: '180be06de9ab6094032f25e5567b3979',
parentId: undefined,
name: 'POST /add',
id: '955fd30dbb866dd9',
kind: 1,
timestamp: 1673430597273822,
duration: 4114749,
attributes: {
'http.url': 'http://127.0.0.1:4200/users/add',
'http.host': '127.0.0.1:4200',
'net.host.name': '127.0.0.1',
'http.method': 'POST',
'http.scheme': 'http',
'http.target': '/users/add',
'http.user_agent': 'Mozilla/5.0 (Macintosh; Intel Mac OS X 10_15_7) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/108.0.0.0 Safari/537.36',
'http.request_content_length_uncompressed': 59,
'http.flavor': '1.1',
'net.transport': 'ip_tcp',
'net.host.ip': '::ffff:127.0.0.1',
'net.host.port': 4200,
'net.peer.ip': '::ffff:127.0.0.1',
'net.peer.port': 62819,
'http.status_code': 302,
'http.status_text': 'FOUND',
'http.route': ''
},
status: { code: 0 },
events: [],
links: []
}
I am sorry that I was not more specific in my previous answer but I will try to explain in more detail why I do not think this is a bug.
here no hexadecimal strings in console exporter
I disagree. This is a hexadecimal representation of a binary object:
id: '955fd30dbb866dd9',
I do not know why the node.js SDK implementation uses strings instead of Binaries for their SpanContext attributes but the specification here states that they values are byte arrays that can be represented in ASCII text as Hex:
The API MUST allow retrieving the TraceId and SpanId in the following forms:
Hex - returns the lowercase hex encoded TraceId (result MUST be a 32-hex-character lowercase string) or SpanId (result MUST be a 16-hex-character lowercase string). Binary - returns the binary representation of the TraceId (result MUST be a 16-byte array) or SpanId (result MUST be an 8-byte array).
https://github.com/open-telemetry/opentelemetry-specification/blob/main/specification/trace/api.md#spancontext
In the case of Ruby, the SpanContext
stores its internal state as a binary object. When we want to convert it to a string, we have convenience methods that convert it to a human readable Hexadecimal representation:
https://github.com/open-telemetry/opentelemetry-ruby/blob/main/api/lib/opentelemetry/trace/span_context.rb#L41 https://github.com/open-telemetry/opentelemetry-ruby/blob/main/api/lib/opentelemetry/trace/span_context.rb#L48
node.js is printed the proper way like below,
There isn't anything in the specification for a Trace/Span Console Exporter so there is not anything "improper" about what the current implementation does, it is what the javascript team chose to use for their output format.
https://github.com/open-telemetry/opentelemetry-specification/blob/main/specification/sdk-environment-variables.md#exporter-selection
I am sympathetic to the fact that pp
isn't ideal and you want to see the output in JSON, so what options do you have?
Here are a few I can think of:
- You may write your own console exporter that outputs the spans to JSON for your applications instead of the console exporter provided by this project
- Submit a PR with a change to the console exporter to use JSON instead of
pp
https://github.com/open-telemetry/opentelemetry-ruby/blob/main/sdk/lib/opentelemetry/sdk/trace/export/console_span_exporter.rb#L24 - Submit a PR to the opentelemetry-specification that describes the behavior of the Trace/Span Console Exporter and what the expected output format should be and follow up with us here
Would you be amenable to trying any or all of these options?
Submit a PR with a change to the console exporter to use JSON instead of pp https://github.com/open-telemetry/opentelemetry-ruby/blob/main/sdk/lib/opentelemetry/sdk/trace/export/console_span_exporter.rb#L24
I think this might be a nice change that improves usability. Initial testing indicates that it's not as simple as changing pp s
to puts s.to_json
, but I think it would otherwise be tractable. Might need to add a few to_json
and to_s
methods around the codebase to achieve it.
👋 This issue has been marked as stale because it has been open with no activity. You can: comment on the issue or remove the stale label to hold stale off for a while, add the keep
label to hold stale off permanently, or do nothing. If you do nothing this issue will be closed eventually by the stale bot.