@@ -50,70 +50,72 @@ unless_disabled?("OTEL_CRYSTAL_DISABLE_INSTRUMENTATION_DB") do
50
50
end
51
51
52
52
if_version?(DB , :>= , " 0.10.0" ) do
53
- class DB::Statement
54
- private def _normalize_scheme_ (scheme )
55
- case scheme
56
- when " postgres"
57
- " postgresql"
58
- # There are probably others for which the scheme doesn't match the OTel prescribed
59
- # labels to be used. Clauses for them should go here.
60
- # See https://github.com/open-telemetry/opentelemetry-specification/blob/main/specification/trace/semantic_conventions/database.md)
61
- else
62
- scheme.to_s
53
+ if_version?(DB , :< , " 0.12.0" ) do
54
+ class DB::Statement
55
+ private def _normalize_scheme_ (scheme )
56
+ case scheme
57
+ when " postgres"
58
+ " postgresql"
59
+ # There are probably others for which the scheme doesn't match the OTel prescribed
60
+ # labels to be used. Clauses for them should go here.
61
+ # See https://github.com/open-telemetry/opentelemetry-specification/blob/main/specification/trace/semantic_conventions/database.md)
62
+ else
63
+ scheme.to_s
64
+ end
63
65
end
64
- end
65
66
66
- private def _normalize_transport_data_ (connection_uri )
67
- scheme = _normalize_scheme_(connection_uri.scheme)
68
- case scheme
69
- when " sqlite3"
70
- {
71
- " net.transport" : " uds" ,
72
- " socket.path" : connection_uri.host.to_s + connection_uri.path.to_s,
73
- }
74
- else
75
- {
76
- " net.transport" : " ip_tcp" ,
77
- " net.peer.name" : connection_uri.host,
78
- " net.peer.port" : connection_uri.port,
79
- }
67
+ private def _normalize_transport_data_ (connection_uri )
68
+ scheme = _normalize_scheme_(connection_uri.scheme)
69
+ case scheme
70
+ when " sqlite3"
71
+ {
72
+ " net.transport" : " uds" ,
73
+ " socket.path" : connection_uri.host.to_s + connection_uri.path.to_s,
74
+ }
75
+ else
76
+ {
77
+ " net.transport" : " ip_tcp" ,
78
+ " net.peer.name" : connection_uri.host,
79
+ " net.peer.port" : connection_uri.port,
80
+ }
81
+ end
80
82
end
81
- end
82
83
83
- def_around_query_or_exec do |args |
84
- query = command.compact
85
- operation = query.strip[0 ...query.index(' ' )] # This is faster than a regex
86
- uri = connection.context.uri.dup.tap(& .password= (" [FILTERED]" )) # redact password from URI
87
- uri.path.lchop # This is dodgy; without doing this, the assignment below will sometimes get a garbled first few characters.
88
- db_name = uri.path.lchop
89
- OpenTelemetry .in_span(" #{ db_name } ->#{ operation.upcase } " ) do |span | # My kingdom for a SQL parser that can extract table names from a SQL query!
90
- query_args = [] of String
91
- args.each do |arg |
92
- # OpenTelemetry::Instrumentation::CrystalDB::ArgFilters.each do |filter|
93
- # filter_result = filter.call(arg)
94
- # arg = filter_result if filter_result
95
- # end
96
- arg = OpenTelemetry ::Instrumentation ::CrystalDB .filter(arg)
84
+ def_around_query_or_exec do |args |
85
+ query = command.compact
86
+ operation = query.strip[0 ...query.index(' ' )] # This is faster than a regex
87
+ uri = connection.context.uri.dup.tap(& .password= (" [FILTERED]" )) # redact password from URI
88
+ uri.path.lchop # This is dodgy; without doing this, the assignment below will sometimes get a garbled first few characters.
89
+ db_name = uri.path.lchop
90
+ OpenTelemetry .in_span(" #{ db_name } ->#{ operation.upcase } " ) do |span | # My kingdom for a SQL parser that can extract table names from a SQL query!
91
+ query_args = [] of String
92
+ args.each do |arg |
93
+ # OpenTelemetry::Instrumentation::CrystalDB::ArgFilters.each do |filter|
94
+ # filter_result = filter.call(arg)
95
+ # arg = filter_result if filter_result
96
+ # end
97
+ arg = OpenTelemetry ::Instrumentation ::CrystalDB .filter(arg)
97
98
98
- arg = arg.to_s unless arg.is_a?(String )
99
- query_args << arg
100
- end
99
+ arg = arg.to_s unless arg.is_a?(String )
100
+ query_args << arg
101
+ end
101
102
102
- transport_data = _normalize_transport_data_(uri)
103
- transport_data.each do |key , value |
104
- span.set_attribute(key.to_s, value.to_s)
105
- end
103
+ transport_data = _normalize_transport_data_(uri)
104
+ transport_data.each do |key , value |
105
+ span.set_attribute(key.to_s, value.to_s)
106
+ end
106
107
107
- span[" db.system" ] = _normalize_scheme_(uri.scheme)
108
- span[" db.connection_string" ] = uri.to_s
109
- span[" db.user" ] = uri.user.to_s if uri.user
110
- span[" db.name" ] = db_name
111
- span[" db.statement" ] = command
112
- span[" db.query_args" ] = query_args
113
- span[" db.operation" ] = operation
114
- span.kind = OpenTelemetry ::Span ::Kind ::Client
108
+ span[" db.system" ] = _normalize_scheme_(uri.scheme)
109
+ span[" db.connection_string" ] = uri.to_s
110
+ span[" db.user" ] = uri.user.to_s if uri.user
111
+ span[" db.name" ] = db_name
112
+ span[" db.statement" ] = command
113
+ span[" db.query_args" ] = query_args
114
+ span[" db.operation" ] = operation
115
+ span.kind = OpenTelemetry ::Span ::Kind ::Client
115
116
116
- yield # Perform the actual query
117
+ yield # Perform the actual query
118
+ end
117
119
end
118
120
end
119
121
end
0 commit comments