Skip to content

Commit 8fae1e3

Browse files
committed
Here be dragons. Contextual span attribute injection.
These changes allow auto-injection of some very useful contextual span attributes. It needs more testing.
1 parent 551543a commit 8fae1e3

File tree

3 files changed

+12
-17
lines changed

3 files changed

+12
-17
lines changed

src/opentelemetry/instrumentation/crystal/db.cr

Lines changed: 1 addition & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -83,18 +83,12 @@ unless_enabled?("OTEL_CRYSTAL_DISABLE_INSTRUMENTATION_DB") do
8383
end
8484

8585
def_around_query_or_exec do |args|
86-
# Place the span in the existing trace, if any. If there is currently no trace running, then
87-
# create a new trace.
88-
unless trace = OpenTelemetry::Trace.current_trace
89-
trace = OpenTelemetry.trace
90-
end
91-
9286
query = command.compact
9387
operation = query.strip[0...query.index(' ')] # This is faster than a regex
9488
uri = connection.context.uri.dup.tap(&.password=("[FILTERED]")) # redact password from URI
9589
uri.path.lchop # This is dodgy; without doing this, the assignment below will sometimes get a garbled first few characters.
9690
db_name = uri.path.lchop
97-
trace.in_span("#{db_name}->#{operation.upcase}") do |span| # My kingdom for a SQL parser that can extract table names from a SQL query!
91+
OpenTelemetry.trace.in_span("#{db_name}->#{operation.upcase}") do |span| # My kingdom for a SQL parser that can extract table names from a SQL query!
9892
query_args = [] of String
9993
args.each do |arg|
10094
OpenTelemetry::Instrumentation::CrystalDB::ArgFilters.each do |filter|

src/opentelemetry/instrumentation/crystal/http_client.cr

Lines changed: 3 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -102,8 +102,7 @@ unless_enabled?("OTEL_CRYSTAL_DISABLE_INSTRUMENTATION_HTTP_CLIENT") do
102102

103103
class HTTP::Client
104104
trace("do_connect") do
105-
trace = OpenTelemetry.trace
106-
trace.in_span("HTTP::Client Connect") do |span|
105+
OpenTelemetry.in_span("HTTP::Client Connect") do |span|
107106
span.client!
108107
io = previous_def
109108
span["net.peer.name"] = @host
@@ -114,15 +113,13 @@ unless_enabled?("OTEL_CRYSTAL_DISABLE_INSTRUMENTATION_HTTP_CLIENT") do
114113
end
115114

116115
trace("do_connect_ssl") do
117-
trace = OpenTelemetry.trace
118-
trace.in_span("Negotiate SSL") do |_span|
116+
OpenTelemetry.in_span("Negotiate SSL") do |_span|
119117
previous_def
120118
end.not_nil!
121119
end
122120

123121
def_around_exec do |request|
124-
trace = OpenTelemetry.trace
125-
trace.in_span("HTTP::Client #{request.method}") do |span|
122+
OpenTelemetry.in_span("HTTP::Client #{request.method}") do |span|
126123
span.client!
127124
span["http.host"] = self.host
128125
span["http.port"] = self.port

src/opentelemetry/instrumentation/crystal/http_server.cr

Lines changed: 8 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -47,7 +47,7 @@ unless_enabled?("OTEL_CRYSTAL_DISABLE_INSTRUMENTATION_HTTP_SERVER") do
4747
module HTTP::Handler
4848
trace("call_next") do
4949
if next_handler = @next
50-
OpenTelemetry.trace.in_span("Invoke handler #{next_handler.class.name}") do |_handler_span|
50+
OpenTelemetry.in_span("Invoke handler #{next_handler.class.name}") do |_handler_span|
5151
previous_def
5252
end
5353
else
@@ -84,7 +84,7 @@ unless_enabled?("OTEL_CRYSTAL_DISABLE_INSTRUMENTATION_HTTP_SERVER") do
8484
# versions, but this is entirely untested.
8585
# Wrap the start of request handling, the call to handle_client, in top-level-instrumentation.
8686
trace("handle_client") do
87-
OpenTelemetry.trace.in_span("HTTP::Server connection") do |span|
87+
OpenTelemetry.in_span("HTTP::Server connection") do |span|
8888
span.server!
8989
if io.responds_to?(:remote_address)
9090
if remote_addr = io.remote_address
@@ -135,7 +135,7 @@ unless_enabled?("OTEL_CRYSTAL_DISABLE_INSTRUMENTATION_HTTP_SERVER") do
135135
trace.span_context.trace_id = traceparent.trace_id
136136
end
137137

138-
trace.in_span(trace_name) do |span|
138+
OpenTelemetry.in_span(trace_name) do |span|
139139
if request.is_a?(HTTP::Request) && request.headers["traceparent"]?
140140
parent = OpenTelemetry::Span.build do |pspan|
141141
pspan.is_recording = false
@@ -187,7 +187,7 @@ unless_enabled?("OTEL_CRYSTAL_DISABLE_INSTRUMENTATION_HTTP_SERVER") do
187187
span["guid"] = span.span_id.hexstring
188188
end
189189

190-
OpenTelemetry::Trace.current_trace.not_nil!.in_span("Invoke handler #{@handler.class.name}") do |handler_span|
190+
OpenTelemetry.in_span("Invoke handler #{@handler.class.name}") do |handler_span|
191191
Log.with_context do
192192
@handler.call(context)
193193
rescue ex : ClientError
@@ -200,6 +200,10 @@ unless_enabled?("OTEL_CRYSTAL_DISABLE_INSTRUMENTATION_HTTP_SERVER") do
200200
handler_span.add_event("Unhandled exception on HTTP::Handler") do |event|
201201
event["message"] = ex.message.to_s
202202
end
203+
span.status.error!(ex.message.to_s)
204+
handler_span["exception.type"] = ex.class.name
205+
handler_span["exception.message"] = ex.message.to_s
206+
handler_span["exception.stacktrace"] = ex.backtrace.join("\n")
203207
unless response.closed?
204208
unless response.wrote_headers?
205209
span["http.status_code"] = HTTP::Status::INTERNAL_SERVER_ERROR.value if span

0 commit comments

Comments
 (0)