Skip to content

Commit

Permalink
fix(crystal/db): breaking change for crystal db
Browse files Browse the repository at this point in the history
no longer exposes the URI
  • Loading branch information
stakach committed Jun 26, 2023
1 parent 327d335 commit 5c0323d
Show file tree
Hide file tree
Showing 2 changed files with 59 additions and 57 deletions.
2 changes: 1 addition & 1 deletion shard.yml
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
name: opentelemetry-instrumentation
version: 0.5.2
version: 0.5.3

authors:
- Kirk Haines <[email protected]>
Expand Down
114 changes: 58 additions & 56 deletions src/opentelemetry/instrumentation/crystal/db.cr
Original file line number Diff line number Diff line change
Expand Up @@ -50,70 +50,72 @@ unless_disabled?("OTEL_CRYSTAL_DISABLE_INSTRUMENTATION_DB") do
end

if_version?(DB, :>=, "0.10.0") do
class DB::Statement
private def _normalize_scheme_(scheme)
case scheme
when "postgres"
"postgresql"
# There are probably others for which the scheme doesn't match the OTel prescribed
# labels to be used. Clauses for them should go here.
# See https://github.com/open-telemetry/opentelemetry-specification/blob/main/specification/trace/semantic_conventions/database.md)
else
scheme.to_s
if_version?(DB, :<, "0.12.0") do
class DB::Statement
private def _normalize_scheme_(scheme)
case scheme
when "postgres"
"postgresql"
# There are probably others for which the scheme doesn't match the OTel prescribed
# labels to be used. Clauses for them should go here.
# See https://github.com/open-telemetry/opentelemetry-specification/blob/main/specification/trace/semantic_conventions/database.md)
else
scheme.to_s
end
end
end

private def _normalize_transport_data_(connection_uri)
scheme = _normalize_scheme_(connection_uri.scheme)
case scheme
when "sqlite3"
{
"net.transport": "uds",
"socket.path": connection_uri.host.to_s + connection_uri.path.to_s,
}
else
{
"net.transport": "ip_tcp",
"net.peer.name": connection_uri.host,
"net.peer.port": connection_uri.port,
}
private def _normalize_transport_data_(connection_uri)
scheme = _normalize_scheme_(connection_uri.scheme)
case scheme
when "sqlite3"
{
"net.transport": "uds",
"socket.path": connection_uri.host.to_s + connection_uri.path.to_s,
}
else
{
"net.transport": "ip_tcp",
"net.peer.name": connection_uri.host,
"net.peer.port": connection_uri.port,
}
end
end
end

def_around_query_or_exec do |args|
query = command.compact
operation = query.strip[0...query.index(' ')] # This is faster than a regex
uri = connection.context.uri.dup.tap(&.password=("[FILTERED]")) # redact password from URI
uri.path.lchop # This is dodgy; without doing this, the assignment below will sometimes get a garbled first few characters.
db_name = uri.path.lchop
OpenTelemetry.in_span("#{db_name}->#{operation.upcase}") do |span| # My kingdom for a SQL parser that can extract table names from a SQL query!
query_args = [] of String
args.each do |arg|
# OpenTelemetry::Instrumentation::CrystalDB::ArgFilters.each do |filter|
# filter_result = filter.call(arg)
# arg = filter_result if filter_result
# end
arg = OpenTelemetry::Instrumentation::CrystalDB.filter(arg)
def_around_query_or_exec do |args|
query = command.compact
operation = query.strip[0...query.index(' ')] # This is faster than a regex
uri = connection.context.uri.dup.tap(&.password=("[FILTERED]")) # redact password from URI
uri.path.lchop # This is dodgy; without doing this, the assignment below will sometimes get a garbled first few characters.
db_name = uri.path.lchop
OpenTelemetry.in_span("#{db_name}->#{operation.upcase}") do |span| # My kingdom for a SQL parser that can extract table names from a SQL query!
query_args = [] of String
args.each do |arg|
# OpenTelemetry::Instrumentation::CrystalDB::ArgFilters.each do |filter|
# filter_result = filter.call(arg)
# arg = filter_result if filter_result
# end
arg = OpenTelemetry::Instrumentation::CrystalDB.filter(arg)

arg = arg.to_s unless arg.is_a?(String)
query_args << arg
end
arg = arg.to_s unless arg.is_a?(String)
query_args << arg
end

transport_data = _normalize_transport_data_(uri)
transport_data.each do |key, value|
span.set_attribute(key.to_s, value.to_s)
end
transport_data = _normalize_transport_data_(uri)
transport_data.each do |key, value|
span.set_attribute(key.to_s, value.to_s)
end

span["db.system"] = _normalize_scheme_(uri.scheme)
span["db.connection_string"] = uri.to_s
span["db.user"] = uri.user.to_s if uri.user
span["db.name"] = db_name
span["db.statement"] = command
span["db.query_args"] = query_args
span["db.operation"] = operation
span.kind = OpenTelemetry::Span::Kind::Client
span["db.system"] = _normalize_scheme_(uri.scheme)
span["db.connection_string"] = uri.to_s
span["db.user"] = uri.user.to_s if uri.user
span["db.name"] = db_name
span["db.statement"] = command
span["db.query_args"] = query_args
span["db.operation"] = operation
span.kind = OpenTelemetry::Span::Kind::Client

yield # Perform the actual query
yield # Perform the actual query
end
end
end
end
Expand Down

0 comments on commit 5c0323d

Please sign in to comment.