2021-01-25 14:54:38 +08:00
|
|
|
// Copyright GoFrame Author(https://goframe.org). All Rights Reserved.
|
|
|
|
//
|
|
|
|
// This Source Code Form is subject to the terms of the MIT License.
|
|
|
|
// If a copy of the MIT was not distributed with this file,
|
|
|
|
// You can obtain one at https://github.com/gogf/gf.
|
|
|
|
|
|
|
|
package ghttp
|
|
|
|
|
|
|
|
import (
|
2021-12-10 18:08:36 +08:00
|
|
|
"context"
|
2021-01-25 18:43:47 +08:00
|
|
|
"fmt"
|
2021-09-21 15:15:37 +08:00
|
|
|
"io/ioutil"
|
|
|
|
|
2021-12-10 18:08:36 +08:00
|
|
|
"github.com/gogf/gf/v2/os/gctx"
|
2021-11-16 19:43:02 +08:00
|
|
|
"go.opentelemetry.io/otel"
|
|
|
|
"go.opentelemetry.io/otel/attribute"
|
|
|
|
"go.opentelemetry.io/otel/codes"
|
|
|
|
"go.opentelemetry.io/otel/propagation"
|
|
|
|
"go.opentelemetry.io/otel/trace"
|
|
|
|
|
2021-10-11 21:41:56 +08:00
|
|
|
"github.com/gogf/gf/v2"
|
2021-12-03 23:32:00 +08:00
|
|
|
"github.com/gogf/gf/v2/internal/httputil"
|
2021-10-11 21:41:56 +08:00
|
|
|
"github.com/gogf/gf/v2/internal/utils"
|
|
|
|
"github.com/gogf/gf/v2/net/gtrace"
|
|
|
|
"github.com/gogf/gf/v2/text/gstr"
|
|
|
|
"github.com/gogf/gf/v2/util/gconv"
|
2021-01-25 14:54:38 +08:00
|
|
|
)
|
|
|
|
|
2021-01-25 18:43:47 +08:00
|
|
|
const (
|
2021-12-10 18:08:36 +08:00
|
|
|
tracingInstrumentName = "github.com/gogf/gf/v2/net/ghttp.Server"
|
|
|
|
tracingEventHttpRequest = "http.request"
|
|
|
|
tracingEventHttpRequestHeaders = "http.request.headers"
|
|
|
|
tracingEventHttpRequestBaggage = "http.request.baggage"
|
|
|
|
tracingEventHttpRequestBody = "http.request.body"
|
|
|
|
tracingEventHttpResponse = "http.response"
|
|
|
|
tracingEventHttpResponseHeaders = "http.response.headers"
|
|
|
|
tracingEventHttpResponseBody = "http.response.body"
|
|
|
|
tracingMiddlewareHandled gctx.StrKey = `MiddlewareServerTracingHandled`
|
2021-01-25 18:43:47 +08:00
|
|
|
)
|
|
|
|
|
2021-12-16 23:09:37 +08:00
|
|
|
// internalMiddlewareServerTracing is a serer middleware that enables tracing feature using standards of OpenTelemetry.
|
|
|
|
func internalMiddlewareServerTracing(r *Request) {
|
2021-10-30 15:36:10 +08:00
|
|
|
var (
|
2021-12-10 18:08:36 +08:00
|
|
|
ctx = r.Context()
|
|
|
|
)
|
2021-12-10 23:26:53 +08:00
|
|
|
// Mark this request is handled by server tracing middleware,
|
|
|
|
// to avoid repeated handling by the same middleware.
|
2021-12-10 18:08:36 +08:00
|
|
|
if ctx.Value(tracingMiddlewareHandled) != nil {
|
|
|
|
r.Middleware.Next()
|
|
|
|
return
|
|
|
|
}
|
|
|
|
|
|
|
|
ctx = context.WithValue(ctx, tracingMiddlewareHandled, 1)
|
|
|
|
var (
|
|
|
|
span trace.Span
|
|
|
|
tr = otel.GetTracerProvider().Tracer(
|
|
|
|
tracingInstrumentName,
|
|
|
|
trace.WithInstrumentationVersion(gf.VERSION),
|
2021-10-30 15:36:10 +08:00
|
|
|
)
|
|
|
|
)
|
2021-12-10 18:08:36 +08:00
|
|
|
ctx, span = tr.Start(
|
|
|
|
otel.GetTextMapPropagator().Extract(
|
|
|
|
ctx,
|
|
|
|
propagation.HeaderCarrier(r.Header),
|
|
|
|
),
|
|
|
|
r.URL.String(),
|
|
|
|
trace.WithSpanKind(trace.SpanKindServer),
|
|
|
|
)
|
2021-01-25 18:43:47 +08:00
|
|
|
defer span.End()
|
|
|
|
|
2021-01-27 19:15:14 +08:00
|
|
|
span.SetAttributes(gtrace.CommonLabels()...)
|
|
|
|
|
2021-01-25 18:43:47 +08:00
|
|
|
// Inject tracing context.
|
|
|
|
r.SetCtx(ctx)
|
|
|
|
|
2022-01-10 16:42:30 +08:00
|
|
|
// If it is now using default trace provider, it then does no complex tracing jobs.
|
2021-12-16 23:09:37 +08:00
|
|
|
if gtrace.IsUsingDefaultProvider() {
|
|
|
|
r.Middleware.Next()
|
|
|
|
return
|
|
|
|
}
|
|
|
|
|
2021-01-25 18:43:47 +08:00
|
|
|
// Request content logging.
|
2021-03-18 10:39:23 +08:00
|
|
|
reqBodyContentBytes, _ := ioutil.ReadAll(r.Body)
|
|
|
|
r.Body = utils.NewReadCloser(reqBodyContentBytes, false)
|
|
|
|
|
2021-01-27 20:25:57 +08:00
|
|
|
span.AddEvent(tracingEventHttpRequest, trace.WithAttributes(
|
2021-09-21 23:53:03 +08:00
|
|
|
attribute.String(tracingEventHttpRequestHeaders, gconv.String(httputil.HeaderToMap(r.Header))),
|
2021-09-21 15:15:37 +08:00
|
|
|
attribute.String(tracingEventHttpRequestBaggage, gtrace.GetBaggageMap(ctx).String()),
|
2021-03-18 10:39:23 +08:00
|
|
|
attribute.String(tracingEventHttpRequestBody, gstr.StrLimit(
|
|
|
|
string(reqBodyContentBytes),
|
|
|
|
gtrace.MaxContentLogSize(),
|
|
|
|
"...",
|
|
|
|
)),
|
2021-01-25 18:43:47 +08:00
|
|
|
))
|
|
|
|
|
|
|
|
// Continue executing.
|
|
|
|
r.Middleware.Next()
|
|
|
|
|
|
|
|
// Error logging.
|
|
|
|
if err := r.GetError(); err != nil {
|
|
|
|
span.SetStatus(codes.Error, fmt.Sprintf(`%+v`, err))
|
|
|
|
}
|
|
|
|
// Response content logging.
|
2021-11-13 23:23:55 +08:00
|
|
|
var resBodyContent = gstr.StrLimit(r.Response.BufferString(), gtrace.MaxContentLogSize(), "...")
|
2021-03-18 10:39:23 +08:00
|
|
|
|
2021-01-27 20:25:57 +08:00
|
|
|
span.AddEvent(tracingEventHttpResponse, trace.WithAttributes(
|
2021-09-22 14:54:42 +08:00
|
|
|
attribute.String(tracingEventHttpResponseHeaders, gconv.String(httputil.HeaderToMap(r.Response.Header()))),
|
2021-03-18 10:39:23 +08:00
|
|
|
attribute.String(tracingEventHttpResponseBody, resBodyContent),
|
2021-01-25 18:43:47 +08:00
|
|
|
))
|
|
|
|
return
|
|
|
|
}
|