@@ -25,6 +25,7 @@ if Code.ensure_loaded?(:telemetry) do
25
25
## Options
26
26
27
27
- `:metadata` - additional metadata passed to telemetry events
28
+ - `:prefix` - prefix for telemetry events. Defaults to `:tesla`
28
29
29
30
## Telemetry Events
30
31
@@ -91,9 +92,10 @@ if Code.ensure_loaded?(:telemetry) do
91
92
@ impl Tesla.Middleware
92
93
def call ( env , next , opts ) do
93
94
metadata = opts [ :metadata ] || % { }
95
+ prefix = opts [ :prefix ] || :tesla
94
96
start_time = System . monotonic_time ( )
95
97
96
- emit_start ( Map . merge ( metadata , % { env: env } ) )
98
+ emit_start ( prefix , Map . merge ( metadata , % { env: env } ) )
97
99
98
100
try do
99
101
Tesla . run ( env , next )
@@ -103,6 +105,7 @@ if Code.ensure_loaded?(:telemetry) do
103
105
duration = System . monotonic_time ( ) - start_time
104
106
105
107
emit_exception (
108
+ prefix ,
106
109
duration ,
107
110
Map . merge ( metadata , % { env: env , kind: kind , reason: reason , stacktrace: stacktrace } )
108
111
)
@@ -112,56 +115,56 @@ if Code.ensure_loaded?(:telemetry) do
112
115
{ :ok , env } = result ->
113
116
duration = System . monotonic_time ( ) - start_time
114
117
115
- emit_stop ( duration , Map . merge ( metadata , % { env: env } ) )
116
- emit_legacy_event ( duration , result )
118
+ emit_stop ( prefix , duration , Map . merge ( metadata , % { env: env } ) )
119
+ emit_legacy_event ( prefix , duration , result )
117
120
118
121
result
119
122
120
123
{ :error , reason } = result ->
121
124
duration = System . monotonic_time ( ) - start_time
122
125
123
- emit_stop ( duration , Map . merge ( metadata , % { env: env , error: reason } ) )
124
- emit_legacy_event ( duration , result )
126
+ emit_stop ( prefix , duration , Map . merge ( metadata , % { env: env , error: reason } ) )
127
+ emit_legacy_event ( prefix , duration , result )
125
128
126
129
result
127
130
end
128
131
end
129
132
130
- defp emit_start ( metadata ) do
133
+ defp emit_start ( prefix , metadata ) do
131
134
:telemetry . execute (
132
- [ :tesla , :request , :start ] ,
135
+ [ prefix , :request , :start ] ,
133
136
% { system_time: System . system_time ( ) } ,
134
137
metadata
135
138
)
136
139
end
137
140
138
- defp emit_stop ( duration , metadata ) do
141
+ defp emit_stop ( prefix , duration , metadata ) do
139
142
:telemetry . execute (
140
- [ :tesla , :request , :stop ] ,
143
+ [ prefix , :request , :stop ] ,
141
144
% { duration: duration } ,
142
145
metadata
143
146
)
144
147
end
145
148
146
149
if @ disable_legacy_event do
147
- defp emit_legacy_event ( _duration , _result ) do
150
+ defp emit_legacy_event ( _prefix , _duration , _result ) do
148
151
:ok
149
152
end
150
153
else
151
- defp emit_legacy_event ( duration , result ) do
154
+ defp emit_legacy_event ( prefix , duration , result ) do
152
155
duration = System . convert_time_unit ( duration , :native , :microsecond )
153
156
154
157
:telemetry . execute (
155
- [ :tesla , :request ] ,
158
+ [ prefix , :request ] ,
156
159
% { request_time: duration } ,
157
160
% { result: result }
158
161
)
159
162
end
160
163
end
161
164
162
- defp emit_exception ( duration , metadata ) do
165
+ defp emit_exception ( prefix , duration , metadata ) do
163
166
:telemetry . execute (
164
- [ :tesla , :request , :exception ] ,
167
+ [ prefix , :request , :exception ] ,
165
168
% { duration: duration } ,
166
169
metadata
167
170
)
0 commit comments