-
Notifications
You must be signed in to change notification settings - Fork 29
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Add otel instrumentation for http-grpc gateway (#522)
- Loading branch information
Showing
5 changed files
with
86 additions
and
4 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,76 @@ | ||
// Copyright 2024 Yahoo Inc. | ||
// Licensed under the terms of the Apache License 2.0. Please see LICENSE file in project root for terms. | ||
|
||
package otellib | ||
|
||
import ( | ||
"fmt" | ||
"log" | ||
"net/http" | ||
"runtime/debug" | ||
|
||
"go.opentelemetry.io/contrib/instrumentation/net/http/otelhttp" | ||
"go.opentelemetry.io/otel" | ||
"go.opentelemetry.io/otel/attribute" | ||
"go.opentelemetry.io/otel/metric" | ||
semconv "go.opentelemetry.io/otel/semconv/v1.20.0" | ||
) | ||
|
||
const scopeName = "github.com/theparanoids/crypki/otellib" | ||
|
||
const handlerPanic = "http.handler.panic" | ||
|
||
type httpMiddleware struct { | ||
next http.Handler | ||
meter metric.Meter | ||
|
||
panicCounter metric.Int64Counter | ||
} | ||
|
||
// NewHTTPMiddleware wraps an http.Handler with OpenTelemetry instrumentation. | ||
func NewHTTPMiddleware(handler http.Handler, operation string) http.Handler { | ||
h := newHTTPMiddleware(handler) | ||
return otelhttp.NewHandler(h, operation) | ||
} | ||
|
||
func newHTTPMiddleware(nextHandler http.Handler) *httpMiddleware { | ||
middleware := &httpMiddleware{ | ||
next: nextHandler, | ||
} | ||
middleware.meter = otel.GetMeterProvider().Meter(scopeName) | ||
|
||
var err error | ||
if middleware.panicCounter, err = middleware.meter.Int64Counter( | ||
handlerPanic, | ||
metric.WithUnit("1"), | ||
metric.WithDescription("Count the number of HTTP handler panic"), | ||
); err != nil { | ||
otel.Handle(err) | ||
} | ||
|
||
return middleware | ||
} | ||
|
||
// ServeHTTP implements http.Handler. | ||
func (h *httpMiddleware) ServeHTTP(w http.ResponseWriter, r *http.Request) { | ||
// A labeler is always available in the request context by otelhttp package. | ||
labeler, _ := otelhttp.LabelerFromContext(r.Context()) | ||
// Add the http.target attribute to the OTel labeler. | ||
if r.URL != nil { | ||
labeler.Add(semconv.HTTPTargetKey.String(r.URL.RequestURI())) | ||
} | ||
defer func() { | ||
if rec := recover(); rec != nil { | ||
log.Printf(`panic captured, %v, %v`, string(debug.Stack()), rec) | ||
|
||
ctx := r.Context() | ||
labels := append(labeler.Get(), []attribute.KeyValue{ | ||
attribute.String("http.method", r.Method), | ||
attribute.String("panic.message", fmt.Sprintf("%v", rec)), | ||
}...) | ||
h.panicCounter.Add(ctx, 1, metric.WithAttributes(labels...)) | ||
http.Error(w, "Internal Server Error", http.StatusInternalServerError) | ||
} | ||
}() | ||
h.next.ServeHTTP(w, r) | ||
} |
File renamed without changes.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters