|
|
@ -1,24 +1,18 @@
|
|
|
|
package handler
|
|
|
|
package handler
|
|
|
|
|
|
|
|
|
|
|
|
import (
|
|
|
|
import (
|
|
|
|
"bufio"
|
|
|
|
|
|
|
|
"errors"
|
|
|
|
|
|
|
|
"net"
|
|
|
|
|
|
|
|
"net/http"
|
|
|
|
"net/http"
|
|
|
|
"sync"
|
|
|
|
"sync"
|
|
|
|
|
|
|
|
|
|
|
|
"github.com/zeromicro/go-zero/core/lang"
|
|
|
|
"github.com/zeromicro/go-zero/core/lang"
|
|
|
|
"github.com/zeromicro/go-zero/core/trace"
|
|
|
|
"github.com/zeromicro/go-zero/core/trace"
|
|
|
|
|
|
|
|
"github.com/zeromicro/go-zero/rest/internal/response"
|
|
|
|
"go.opentelemetry.io/otel"
|
|
|
|
"go.opentelemetry.io/otel"
|
|
|
|
"go.opentelemetry.io/otel/attribute"
|
|
|
|
|
|
|
|
"go.opentelemetry.io/otel/codes"
|
|
|
|
|
|
|
|
"go.opentelemetry.io/otel/propagation"
|
|
|
|
"go.opentelemetry.io/otel/propagation"
|
|
|
|
semconv "go.opentelemetry.io/otel/semconv/v1.4.0"
|
|
|
|
semconv "go.opentelemetry.io/otel/semconv/v1.4.0"
|
|
|
|
oteltrace "go.opentelemetry.io/otel/trace"
|
|
|
|
oteltrace "go.opentelemetry.io/otel/trace"
|
|
|
|
)
|
|
|
|
)
|
|
|
|
|
|
|
|
|
|
|
|
const traceKeyStatusCode = "http.status_code"
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
var notTracingSpans sync.Map
|
|
|
|
var notTracingSpans sync.Map
|
|
|
|
|
|
|
|
|
|
|
|
// DontTraceSpan disable tracing for the specified span name.
|
|
|
|
// DontTraceSpan disable tracing for the specified span name.
|
|
|
@ -26,40 +20,6 @@ func DontTraceSpan(spanName string) {
|
|
|
|
notTracingSpans.Store(spanName, lang.Placeholder)
|
|
|
|
notTracingSpans.Store(spanName, lang.Placeholder)
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
type traceResponseWriter struct {
|
|
|
|
|
|
|
|
w http.ResponseWriter
|
|
|
|
|
|
|
|
code int
|
|
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
// Flush implements the http.Flusher interface.
|
|
|
|
|
|
|
|
func (w *traceResponseWriter) Flush() {
|
|
|
|
|
|
|
|
if flusher, ok := w.w.(http.Flusher); ok {
|
|
|
|
|
|
|
|
flusher.Flush()
|
|
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
// Hijack implements the http.Hijacker interface.
|
|
|
|
|
|
|
|
func (w *traceResponseWriter) Hijack() (net.Conn, *bufio.ReadWriter, error) {
|
|
|
|
|
|
|
|
if hijacked, ok := w.w.(http.Hijacker); ok {
|
|
|
|
|
|
|
|
return hijacked.Hijack()
|
|
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
return nil, nil, errors.New("server doesn't support hijacking")
|
|
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
func (w *traceResponseWriter) Header() http.Header {
|
|
|
|
|
|
|
|
return w.w.Header()
|
|
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
func (w *traceResponseWriter) Write(data []byte) (int, error) {
|
|
|
|
|
|
|
|
return w.w.Write(data)
|
|
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
func (w *traceResponseWriter) WriteHeader(statusCode int) {
|
|
|
|
|
|
|
|
w.w.WriteHeader(statusCode)
|
|
|
|
|
|
|
|
w.code = statusCode
|
|
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
// TracingHandler return a middleware that process the opentelemetry.
|
|
|
|
// TracingHandler return a middleware that process the opentelemetry.
|
|
|
|
func TracingHandler(serviceName, path string) func(http.Handler) http.Handler {
|
|
|
|
func TracingHandler(serviceName, path string) func(http.Handler) http.Handler {
|
|
|
|
return func(next http.Handler) http.Handler {
|
|
|
|
return func(next http.Handler) http.Handler {
|
|
|
@ -89,21 +49,12 @@ func TracingHandler(serviceName, path string) func(http.Handler) http.Handler {
|
|
|
|
|
|
|
|
|
|
|
|
// convenient for tracking error messages
|
|
|
|
// convenient for tracking error messages
|
|
|
|
propagator.Inject(spanCtx, propagation.HeaderCarrier(w.Header()))
|
|
|
|
propagator.Inject(spanCtx, propagation.HeaderCarrier(w.Header()))
|
|
|
|
trw := &traceResponseWriter{
|
|
|
|
|
|
|
|
w: w,
|
|
|
|
trw := &response.WithCodeResponseWriter{Writer: w, Code: http.StatusOK}
|
|
|
|
code: http.StatusOK,
|
|
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
next.ServeHTTP(trw, r.WithContext(spanCtx))
|
|
|
|
next.ServeHTTP(trw, r.WithContext(spanCtx))
|
|
|
|
|
|
|
|
|
|
|
|
span.SetAttributes(attribute.KeyValue{
|
|
|
|
span.SetAttributes(semconv.HTTPAttributesFromHTTPStatusCode(trw.Code)...)
|
|
|
|
Key: traceKeyStatusCode,
|
|
|
|
span.SetStatus(semconv.SpanStatusFromHTTPStatusCodeAndSpanKind(trw.Code, oteltrace.SpanKindServer))
|
|
|
|
Value: attribute.IntValue(trw.code),
|
|
|
|
|
|
|
|
})
|
|
|
|
|
|
|
|
if trw.code >= http.StatusBadRequest {
|
|
|
|
|
|
|
|
span.SetStatus(codes.Error, http.StatusText(trw.code))
|
|
|
|
|
|
|
|
} else {
|
|
|
|
|
|
|
|
span.SetStatus(codes.Ok, http.StatusText(trw.code))
|
|
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
})
|
|
|
|
})
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|