gf/net/ghttp/ghttp_middleware_tracing.go

93 lines
3.1 KiB
Go
Raw Normal View History

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 (
"fmt"
"io/ioutil"
"net/http"
"github.com/gogf/gf"
"github.com/gogf/gf/encoding/gjson"
"github.com/gogf/gf/internal/utils"
2021-01-25 14:54:38 +08:00
"github.com/gogf/gf/net/ghttp/internal/client"
"github.com/gogf/gf/net/ghttp/internal/httputil"
2021-01-27 19:15:14 +08:00
"github.com/gogf/gf/net/gtrace"
2021-03-18 10:39:23 +08:00
"github.com/gogf/gf/text/gstr"
"github.com/gogf/gf/util/gconv"
"go.opentelemetry.io/otel"
2021-03-18 10:39:23 +08:00
"go.opentelemetry.io/otel/attribute"
"go.opentelemetry.io/otel/codes"
2021-03-18 10:39:23 +08:00
"go.opentelemetry.io/otel/propagation"
"go.opentelemetry.io/otel/trace"
2021-01-25 14:54:38 +08:00
)
const (
2021-02-01 17:10:50 +08:00
tracingInstrumentName = "github.com/gogf/gf/net/ghttp.Server"
tracingEventHttpRequest = "http.request"
tracingEventHttpRequestHeaders = "http.request.headers"
2021-01-28 13:51:23 +08:00
tracingEventHttpRequestBaggage = "http.request.baggage"
tracingEventHttpRequestBody = "http.request.body"
tracingEventHttpResponse = "http.response"
tracingEventHttpResponseHeaders = "http.response.headers"
tracingEventHttpResponseBody = "http.response.body"
)
// MiddlewareClientTracing is a client middleware that enables tracing feature using standards of OpenTelemetry.
2021-01-25 14:54:38 +08:00
func MiddlewareClientTracing(c *Client, r *http.Request) (*ClientResponse, error) {
return client.MiddlewareTracing(c, r)
}
// MiddlewareServerTracing is a serer middleware that enables tracing feature using standards of OpenTelemetry.
func MiddlewareServerTracing(r *Request) {
2021-02-01 17:10:50 +08:00
tr := otel.GetTracerProvider().Tracer(tracingInstrumentName, trace.WithInstrumentationVersion(gf.VERSION))
2021-03-18 10:39:23 +08:00
ctx := otel.GetTextMapPropagator().Extract(r.Context(), propagation.HeaderCarrier(r.Header))
2021-01-27 19:50:32 +08:00
ctx, span := tr.Start(ctx, r.URL.String(), trace.WithSpanKind(trace.SpanKindServer))
defer span.End()
2021-01-27 19:15:14 +08:00
span.SetAttributes(gtrace.CommonLabels()...)
// Inject tracing context.
r.SetCtx(ctx)
// Request content logging.
2021-03-18 10:39:23 +08:00
reqBodyContentBytes, _ := ioutil.ReadAll(r.Body)
r.Body = utils.NewReadCloser(reqBodyContentBytes, false)
span.AddEvent(tracingEventHttpRequest, trace.WithAttributes(
attribute.String(tracingEventHttpRequestHeaders, gconv.String(httputil.HeaderToMap(r.Header))),
attribute.String(tracingEventHttpRequestBaggage, gtrace.GetBaggageMap(ctx).String()),
2021-03-18 10:39:23 +08:00
attribute.String(tracingEventHttpRequestBody, gstr.StrLimit(
string(reqBodyContentBytes),
gtrace.MaxContentLogSize(),
"...",
)),
))
// Continue executing.
r.Middleware.Next()
// Error logging.
if err := r.GetError(); err != nil {
span.SetStatus(codes.Error, fmt.Sprintf(`%+v`, err))
}
// Response content logging.
var resBodyContent string
2021-03-18 10:39:23 +08:00
resBodyContent = r.Response.BufferString()
resBodyContent = gstr.StrLimit(
r.Response.BufferString(),
gtrace.MaxContentLogSize(),
"...",
)
span.AddEvent(tracingEventHttpResponse, trace.WithAttributes(
attribute.String(tracingEventHttpResponseHeaders, gjson.New(httputil.HeaderToMap(r.Response.Header())).MustToJsonString()),
2021-03-18 10:39:23 +08:00
attribute.String(tracingEventHttpResponseBody, resBodyContent),
))
return
}