4
4
#
5
5
# SPDX-License-Identifier: Apache-2.0
6
6
7
+ require_relative '../common'
8
+
7
9
module OpenTelemetry
8
10
module Instrumentation
9
11
module Sidekiq
@@ -12,6 +14,7 @@ module Client
12
14
# TracerMiddleware propagates context and instruments Sidekiq client
13
15
# by way of its middleware system
14
16
class TracerMiddleware
17
+ include Common
15
18
include ::Sidekiq ::ClientMiddleware if defined? ( ::Sidekiq ::ClientMiddleware )
16
19
17
20
def call ( _worker_class , job , _queue , _redis_pool )
@@ -33,17 +36,50 @@ def call(_worker_class, job, _queue, _redis_pool)
33
36
OpenTelemetry . propagation . inject ( job )
34
37
span . add_event ( 'created_at' , timestamp : job [ 'created_at' ] )
35
38
yield
39
+ end . tap do
40
+ # FIXME: is it possible/necessary to detect failures here? Does sidekiq bubble them up the middlewares?
41
+ count_sent_message ( job )
36
42
end
37
43
end
38
44
39
45
private
40
46
41
- def instrumentation_config
42
- Sidekiq ::Instrumentation . instance . config
47
+ def count_sent_message ( job )
48
+ with_meter do |_meter |
49
+ counter_attributes = metrics_attributes ( job ) . merge (
50
+ {
51
+ 'messaging.operation.name' => 'create'
52
+ # server.address => # FIXME: required if available
53
+ # messaging.destination.partition.id => FIXME: recommended
54
+ # server.port => # FIXME: recommended
55
+ }
56
+ )
57
+
58
+ counter = messaging_client_sent_messages_counter
59
+ counter . add ( 1 , attributes : counter_attributes )
60
+ end
61
+ end
62
+
63
+ def messaging_client_sent_messages_counter
64
+ instrumentation . counter ( 'messaging.client.sent.messages' )
43
65
end
44
66
45
67
def tracer
46
- Sidekiq ::Instrumentation . instance . tracer
68
+ instrumentation . tracer
69
+ end
70
+
71
+ def with_meter ( &block )
72
+ instrumentation . with_meter ( &block )
73
+ end
74
+
75
+ def metrics_attributes ( job )
76
+ {
77
+ 'messaging.system' => 'sidekiq' , # FIXME: metrics semconv
78
+ 'messaging.destination.name' => job [ 'queue' ] # FIXME: metrics semconv
79
+ # server.address => # FIXME: required if available
80
+ # messaging.destination.partition.id => FIXME: recommended
81
+ # server.port => # FIXME: recommended
82
+ }
47
83
end
48
84
end
49
85
end
0 commit comments