@@ -168,6 +168,9 @@ func HandlerForTransactional(reg prometheus.TransactionalGatherer, opts HandlerO
168
168
if opts .ErrorLog != nil {
169
169
opts .ErrorLog .Println ("error gathering metrics:" , err )
170
170
}
171
+ if opts .StructuredErrorLog != nil {
172
+ opts .StructuredErrorLog .Error ("error gathering metrics" , "error" , err )
173
+ }
171
174
errCnt .WithLabelValues ("gathering" ).Inc ()
172
175
switch opts .ErrorHandling {
173
176
case PanicOnError :
@@ -197,6 +200,9 @@ func HandlerForTransactional(reg prometheus.TransactionalGatherer, opts HandlerO
197
200
if opts .ErrorLog != nil {
198
201
opts .ErrorLog .Println ("error getting writer" , err )
199
202
}
203
+ if opts .StructuredErrorLog != nil {
204
+ opts .StructuredErrorLog .Error ("error getting writer" , "error" , err )
205
+ }
200
206
w = io .Writer (rsp )
201
207
encodingHeader = string (Identity )
202
208
}
@@ -218,6 +224,9 @@ func HandlerForTransactional(reg prometheus.TransactionalGatherer, opts HandlerO
218
224
if opts .ErrorLog != nil {
219
225
opts .ErrorLog .Println ("error encoding and sending metric family:" , err )
220
226
}
227
+ if opts .StructuredErrorLog != nil {
228
+ opts .StructuredErrorLog .Error ("error encoding and sending metric family" , "error" , err )
229
+ }
221
230
errCnt .WithLabelValues ("encoding" ).Inc ()
222
231
switch opts .ErrorHandling {
223
232
case PanicOnError :
@@ -344,6 +353,12 @@ type Logger interface {
344
353
Println (v ... interface {})
345
354
}
346
355
356
+ // StructuredLogger is a minimal interface HandlerOpts needs for structured
357
+ // logging. This is implementd by the standard library log/slog.Logger type.
358
+ type StructuredLogger interface {
359
+ Error (msg string , args ... any )
360
+ }
361
+
347
362
// HandlerOpts specifies options how to serve metrics via an http.Handler. The
348
363
// zero value of HandlerOpts is a reasonable default.
349
364
type HandlerOpts struct {
@@ -354,6 +369,9 @@ type HandlerOpts struct {
354
369
// latter, create a Logger implementation that detects a
355
370
// prometheus.MultiError and formats the contained errors into one line.
356
371
ErrorLog Logger
372
+ // StructuredErrorLog StructuredLogger specifies an optional structured log
373
+ // handler.
374
+ StructuredErrorLog StructuredLogger
357
375
// ErrorHandling defines how errors are handled. Note that errors are
358
376
// logged regardless of the configured ErrorHandling provided ErrorLog
359
377
// is not nil.
0 commit comments