[feature] upgrade opentelemetry version v1.0.0

This commit is contained in:
houseme 2021-09-21 15:15:37 +08:00
parent 01a3dd1eb0
commit 9d67cb4f34
10 changed files with 42 additions and 34 deletions

5
go.mod
View File

@ -12,9 +12,8 @@ require (
github.com/gorilla/websocket v1.4.2
github.com/grokify/html-strip-tags-go v0.0.0-20190921062105-daaa06bf1aaf
github.com/olekukonko/tablewriter v0.0.5
go.opentelemetry.io/otel v1.0.0-RC3
go.opentelemetry.io/otel/oteltest v1.0.0-RC3
go.opentelemetry.io/otel/trace v1.0.0-RC3
go.opentelemetry.io/otel v1.0.0
go.opentelemetry.io/otel/trace v1.0.0
golang.org/x/net v0.0.0-20210520170846-37e1c6afe023
golang.org/x/text v0.3.6
gopkg.in/yaml.v3 v3.0.0-20210107192922-496545a6307b

10
go.sum
View File

@ -32,12 +32,10 @@ github.com/stretchr/objx v0.1.0/go.mod h1:HFkY916IF+rwdDfMAkV7OtwuqBVzrE8GR6GFx+
github.com/stretchr/testify v1.5.1/go.mod h1:5W2xD1RspED5o8YsWQXVCued0rvSQ+mT+I5cxcmMvtA=
github.com/stretchr/testify v1.7.0 h1:nwc3DEeHmmLAfoZucVR881uASk0Mfjw8xYJ99tb5CcY=
github.com/stretchr/testify v1.7.0/go.mod h1:6Fq8oRcR53rry900zMqJjRRixrwX3KX962/h/Wwjteg=
go.opentelemetry.io/otel v1.0.0-RC3 h1:kvwiyEkiUT/JaadXzVLI/R1wDO934A7r3Bs2wEe6wqA=
go.opentelemetry.io/otel v1.0.0-RC3/go.mod h1:Ka5j3ua8tZs4Rkq4Ex3hwgBgOchyPVq5S6P2lz//nKQ=
go.opentelemetry.io/otel/oteltest v1.0.0-RC3 h1:MjaeegZTaX0Bv9uB9CrdVjOFM/8slRjReoWoV9xDCpY=
go.opentelemetry.io/otel/oteltest v1.0.0-RC3/go.mod h1:xpzajI9JBRr7gX63nO6kAmImmYIAtuQblZ36Z+LfCjE=
go.opentelemetry.io/otel/trace v1.0.0-RC3 h1:9F0ayEvlxv8BmNmPbU005WK7hC+7KbOazCPZjNa1yME=
go.opentelemetry.io/otel/trace v1.0.0-RC3/go.mod h1:VUt2TUYd8S2/ZRX09ZDFZQwn2RqfMB5MzO17jBojGxo=
go.opentelemetry.io/otel v1.0.0 h1:qTTn6x71GVBvoafHK/yaRUmFzI4LcONZD0/kXxl5PHI=
go.opentelemetry.io/otel v1.0.0/go.mod h1:AjRVh9A5/5DE7S+mZtTR6t8vpKKryam+0lREnfmS4cg=
go.opentelemetry.io/otel/trace v1.0.0 h1:TSBr8GTEtKevYMG/2d21M989r5WJYVimhTHBKVEZuh4=
go.opentelemetry.io/otel/trace v1.0.0/go.mod h1:PXTWqayeFUlJV1YDNhsJYB184+IvAH814St6o6ajzIs=
golang.org/x/net v0.0.0-20210520170846-37e1c6afe023 h1:ADo5wSpq2gqaCGQWzk7S5vd//0iyyLeAratkEoG5dLE=
golang.org/x/net v0.0.0-20210520170846-37e1c6afe023/go.mod h1:9nx3DQGgdP8bBQD5qxJ1jj9UTztislL4KSBs9R2vV5Y=
golang.org/x/sys v0.0.0-20200116001909-b77594299b42/go.mod h1:h1NjWce9XRLGQEsW7wpKNCjG9DtNlClVuFLEZdDNbEs=

View File

@ -8,7 +8,11 @@ package ghttp
import (
"fmt"
"io/ioutil"
"net/http"
"github.com/gogf/gf"
"github.com/gogf/gf/encoding/gjson"
"github.com/gogf/gf/internal/utils"
"github.com/gogf/gf/net/ghttp/internal/client"
"github.com/gogf/gf/net/ghttp/internal/httputil"
@ -19,8 +23,6 @@ import (
"go.opentelemetry.io/otel/codes"
"go.opentelemetry.io/otel/propagation"
"go.opentelemetry.io/otel/trace"
"io/ioutil"
"net/http"
)
const (
@ -56,8 +58,8 @@ func MiddlewareServerTracing(r *Request) {
r.Body = utils.NewReadCloser(reqBodyContentBytes, false)
span.AddEvent(tracingEventHttpRequest, trace.WithAttributes(
attribute.Any(tracingEventHttpRequestHeaders, httputil.HeaderToMap(r.Header)),
attribute.Any(tracingEventHttpRequestBaggage, gtrace.GetBaggageMap(ctx)),
attribute.String(tracingEventHttpRequestHeaders, gjson.New(httputil.HeaderToMap(r.Header)).MustToJsonString()),
attribute.String(tracingEventHttpRequestBaggage, gtrace.GetBaggageMap(ctx).String()),
attribute.String(tracingEventHttpRequestBody, gstr.StrLimit(
string(reqBodyContentBytes),
gtrace.MaxContentLogSize(),
@ -82,7 +84,7 @@ func MiddlewareServerTracing(r *Request) {
)
span.AddEvent(tracingEventHttpResponse, trace.WithAttributes(
attribute.Any(tracingEventHttpResponseHeaders, httputil.HeaderToMap(r.Response.Header())),
attribute.String(tracingEventHttpResponseHeaders, gjson.New(httputil.HeaderToMap(r.Response.Header())).MustToJsonString()),
attribute.String(tracingEventHttpResponseBody, resBodyContent),
))
return

View File

@ -8,7 +8,12 @@ package client
import (
"fmt"
"io/ioutil"
"net/http"
"net/http/httptrace"
"github.com/gogf/gf"
"github.com/gogf/gf/encoding/gjson"
"github.com/gogf/gf/internal/utils"
"github.com/gogf/gf/net/ghttp/internal/httputil"
"github.com/gogf/gf/net/gtrace"
@ -18,9 +23,6 @@ import (
"go.opentelemetry.io/otel/codes"
"go.opentelemetry.io/otel/propagation"
"go.opentelemetry.io/otel/trace"
"io/ioutil"
"net/http"
"net/http/httptrace"
)
const (
@ -70,7 +72,7 @@ func MiddlewareTracing(c *Client, r *http.Request) (response *Response, err erro
response.Body = utils.NewReadCloser(reqBodyContentBytes, false)
span.AddEvent(tracingEventHttpResponse, trace.WithAttributes(
attribute.Any(tracingEventHttpResponseHeaders, httputil.HeaderToMap(response.Header)),
attribute.String(tracingEventHttpResponseHeaders, gjson.New(httputil.HeaderToMap(response.Header)).MustToJsonString()),
attribute.String(tracingEventHttpResponseBody, gstr.StrLimit(
string(reqBodyContentBytes),
gtrace.MaxContentLogSize(),

View File

@ -10,18 +10,20 @@ import (
"context"
"crypto/tls"
"fmt"
"github.com/gogf/gf/internal/utils"
"github.com/gogf/gf/net/gtrace"
"github.com/gogf/gf/text/gstr"
"go.opentelemetry.io/otel/attribute"
"go.opentelemetry.io/otel/codes"
"go.opentelemetry.io/otel/trace"
"io/ioutil"
"net/http"
"net/http/httptrace"
"net/textproto"
"strings"
"sync"
"github.com/gogf/gf/encoding/gjson"
"github.com/gogf/gf/internal/utils"
"github.com/gogf/gf/net/gtrace"
"github.com/gogf/gf/text/gstr"
"go.opentelemetry.io/otel/attribute"
"go.opentelemetry.io/otel/codes"
"go.opentelemetry.io/otel/trace"
)
type clientTracer struct {
@ -147,8 +149,8 @@ func (ct *clientTracer) wroteRequest(info httptrace.WroteRequestInfo) {
}
ct.span.AddEvent(tracingEventHttpRequest, trace.WithAttributes(
attribute.Any(tracingEventHttpRequestHeaders, ct.headers),
attribute.Any(tracingEventHttpRequestBaggage, gtrace.GetBaggageMap(ct.Context)),
attribute.String(tracingEventHttpRequestHeaders, gjson.New(ct.headers).MustToJsonString()),
attribute.String(tracingEventHttpRequestBaggage, gtrace.GetBaggageMap(ct.Context).String()),
attribute.String(tracingEventHttpRequestBody, gstr.StrLimit(
string(ct.requestBody),
gtrace.MaxContentLogSize(),

View File

@ -9,6 +9,9 @@ package gtrace
import (
"context"
"os"
"strings"
"github.com/gogf/gf/container/gmap"
"github.com/gogf/gf/container/gvar"
"github.com/gogf/gf/net/gipv4"
@ -17,8 +20,6 @@ import (
"go.opentelemetry.io/otel/attribute"
"go.opentelemetry.io/otel/propagation"
"go.opentelemetry.io/otel/trace"
"os"
"strings"
)
const (

View File

@ -8,6 +8,7 @@ package gtrace
import (
"context"
"github.com/gogf/gf/container/gmap"
"github.com/gogf/gf/container/gvar"
"github.com/gogf/gf/util/gconv"

View File

@ -8,9 +8,11 @@ package gtrace
import (
"context"
"go.opentelemetry.io/otel/trace"
)
// Span 。
type Span struct {
trace.Span
}

View File

@ -15,7 +15,7 @@ type Tracer struct {
trace.Tracer
}
// Tracer is a short function for retrieving Tracer.
// NewTracer Tracer is a short function for retrieving Tracer.
func NewTracer(name ...string) *Tracer {
tracerName := ""
if len(name) > 0 {

View File

@ -8,12 +8,12 @@ package gtrace_test
import (
"context"
"testing"
"github.com/gogf/gf/net/gtrace"
"github.com/gogf/gf/test/gtest"
"go.opentelemetry.io/otel"
"go.opentelemetry.io/otel/oteltest"
"go.opentelemetry.io/otel/trace"
"testing"
)
const (
@ -51,14 +51,15 @@ func TestNewCarrier(t *testing.T) {
SpanID: spanID,
TraceFlags: trace.FlagsSampled,
}))
ctx, _ = oteltest.DefaultTracer().Start(ctx, "inject")
ctx, _ = otel.Tracer("").Start(ctx, "inject")
carrier1 := gtrace.NewCarrier()
otel.GetTextMapPropagator().Inject(ctx, carrier1)
t.Assert(carrier1.String(), `{"traceparent":"00-4bf92f3577b34da6a3ce929d0e0e4736-0000000000000002-01"}`)
t.Assert(carrier1.String(), `{"traceparent":"00-4bf92f3577b34da6a3ce929d0e0e4736-00f067aa0ba902b7-01"}`)
ctx = otel.GetTextMapPropagator().Extract(ctx, carrier1)
gotSc := trace.SpanContextFromContext(ctx)
t.Assert(gotSc.TraceID().String(), traceID.String())
t.Assert(gotSc.SpanID().String(), "0000000000000002")
t.Assert(gotSc.SpanID().String(), "00f067aa0ba902b7")
})
}