Skip to content

Commit

Permalink
fix CI
Browse files Browse the repository at this point in the history
  • Loading branch information
smx-Morgan committed Aug 15, 2024
1 parent 0e8ecb7 commit 458b34a
Show file tree
Hide file tree
Showing 16 changed files with 78 additions and 151 deletions.
3 changes: 1 addition & 2 deletions instrumentation/hertzobs/example_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -16,7 +16,6 @@ package hertzobs

import (
"context"
"github.com/cloudwego-contrib/cwgo-pkg/instrumentation/hertzobs/oteltracer"
"github.com/cloudwego-contrib/cwgo-pkg/instrumentation/hertzobs/testutil"
"github.com/cloudwego/hertz/pkg/app"
"github.com/cloudwego/hertz/pkg/app/client"
Expand All @@ -40,7 +39,7 @@ func TestMetricsExample(t *testing.T) {
otel.SetMeterProvider(meterProvider)

// server example
tracer, cfg := oteltracer.NewServerTracer()
tracer, cfg := NewServerTracer()
h := server.Default(tracer, server.WithHostPorts(":39888"))
h.Use(ServerMiddleware(cfg))
h.GET("/ping", func(c context.Context, ctx *app.RequestContext) {
Expand Down
5 changes: 2 additions & 3 deletions instrumentation/hertzobs/extractlabels.go
Original file line number Diff line number Diff line change
Expand Up @@ -17,7 +17,6 @@ package hertzobs

import (
"context"
"github.com/cloudwego-contrib/cwgo-pkg/instrumentation/hertzobs/oteltracer"
"github.com/cloudwego-contrib/cwgo-pkg/instrumentation/internal"
"github.com/cloudwego-contrib/cwgo-pkg/log/logging"
"github.com/cloudwego-contrib/cwgo-pkg/meter/label"
Expand All @@ -37,11 +36,11 @@ var _ label.LabelControl = OtelLabelControl{}

type OtelLabelControl struct {
tracer trace.Tracer
shouldIgnore oteltracer.ConditionFunc
shouldIgnore ConditionFunc
serverHttpRouteFormatter func(c *app.RequestContext) string
}

func NewOtelLabelControl(tracer trace.Tracer, shouldIgnore oteltracer.ConditionFunc, serverHttpRouteFormatter func(c *app.RequestContext) string) OtelLabelControl {
func NewOtelLabelControl(tracer trace.Tracer, shouldIgnore ConditionFunc, serverHttpRouteFormatter func(c *app.RequestContext) string) OtelLabelControl {
return OtelLabelControl{
tracer: tracer,
shouldIgnore: shouldIgnore,
Expand Down
43 changes: 32 additions & 11 deletions instrumentation/hertzobs/middleware.go
Original file line number Diff line number Diff line change
Expand Up @@ -16,7 +16,7 @@ package hertzobs

import (
"context"
"github.com/cloudwego-contrib/cwgo-pkg/instrumentation/hertzobs/oteltracer"

"github.com/cloudwego-contrib/cwgo-pkg/instrumentation/internal"
"github.com/cloudwego-contrib/cwgo-pkg/semantic"
"time"
Expand Down Expand Up @@ -47,12 +47,24 @@ func (sh *StringHeader) Visit(f func(k, v string)) {
})
}

func ClientMiddleware(opts ...oteltracer.Option) client.Middleware {
cfg := oteltracer.NewConfig(opts)
func ClientMiddleware(opts ...Option) client.Middleware {
cfg := NewConfig(opts)
histogramRecorder := make(map[string]metric.Float64Histogram)
counters := make(map[string]metric.Int64Counter)

clientRequestCountMeasure, clientLatencyMeasure := oteltracer.ClientCounter(cfg)
clientRequestCountMeasure, err := cfg.meter.Int64Counter(
semantic.ClientRequestCount,
metric.WithUnit("count"),
metric.WithDescription("measures the client request count total"),
)
handleErr(err)

clientLatencyMeasure, err := cfg.meter.Float64Histogram(
semantic.ClientLatency,
metric.WithUnit("ms"),
metric.WithDescription("measures the duration outbound HTTP requests"),
)
handleErr(err)

counters[semantic.ClientRequestCount] = clientRequestCountMeasure
histogramRecorder[semantic.ClientLatency] = clientLatencyMeasure
Expand All @@ -64,7 +76,13 @@ func ClientMiddleware(opts ...oteltracer.Option) client.Middleware {
}

// trace start
ctx, span, startTime := oteltracer.SpanFromReq(cfg, ctx, req)
start := time.Now()
ctx, span := cfg.tracer.Start(
ctx,
cfg.clientSpanNameFormatter(req),
oteltrace.WithTimestamp(start),
oteltrace.WithSpanKind(oteltrace.SpanKindClient),
)
defer span.End()

// inject client service resource attributes (canonical service) to meta map
Expand All @@ -82,7 +100,7 @@ func ClientMiddleware(opts ...oteltracer.Option) client.Middleware {
if httpReq, err := adaptor.GetCompatRequest(req); err == nil {
span.SetAttributes(semconv.NetAttributesFromHTTPRequest("tcp", httpReq)...)
span.SetAttributes(semconv.EndUserAttributesFromHTTPRequest(httpReq)...)
span.SetAttributes(semconv.HTTPServerAttributesFromHTTPRequest("", oteltracer.GetClientSpanNameFormatter(cfg, req), httpReq)...)
span.SetAttributes(semconv.HTTPServerAttributesFromHTTPRequest("", cfg.clientSpanNameFormatter(req), httpReq)...)
}

// span attributes
Expand All @@ -106,7 +124,7 @@ func ClientMiddleware(opts ...oteltracer.Option) client.Middleware {
counters[semantic.ClientRequestCount].Add(ctx, 1, metric.WithAttributes(metricsAttributes...))
histogramRecorder[semantic.ClientLatency].Record(
ctx,
float64(time.Since(startTime))/float64(time.Millisecond),
float64(time.Since(start))/float64(time.Millisecond),
metric.WithAttributes(metricsAttributes...),
)

Expand All @@ -115,9 +133,9 @@ func ClientMiddleware(opts ...oteltracer.Option) client.Middleware {
}
}

func ServerMiddleware(cfg *oteltracer.Config) app.HandlerFunc {
func ServerMiddleware(cfg *Config) app.HandlerFunc {
return func(ctx context.Context, c *app.RequestContext) {
if oteltracer.ShouldIgnore(cfg, ctx, c) {
if cfg.shouldIgnore(ctx, c) {
c.Next(ctx)
return
}
Expand Down Expand Up @@ -149,7 +167,7 @@ func ServerMiddleware(cfg *oteltracer.Config) app.HandlerFunc {
// set baggage
ctx = baggage.ContextWithBaggage(ctx, bags)

ctx, span := sTracer.Start(oteltrace.ContextWithRemoteSpanContext(ctx, spanCtx), oteltracer.GetServerSpanNameFormatter(cfg, c), opts...)
ctx, span := sTracer.Start(oteltrace.ContextWithRemoteSpanContext(ctx, spanCtx), cfg.serverSpanNameFormatter(c), opts...)

// peer service attributes
span.SetAttributes(peerServiceAttributes...)
Expand All @@ -159,6 +177,9 @@ func ServerMiddleware(cfg *oteltracer.Config) app.HandlerFunc {

c.Next(ctx)

oteltracer.HandleCustomResponseHandlere(cfg, ctx, c)
if cfg.customResponseHandler != nil {
// execute custom response handler
cfg.customResponseHandler(ctx, c)
}
}
}
5 changes: 2 additions & 3 deletions instrumentation/hertzobs/middleware_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -16,7 +16,6 @@ package hertzobs

import (
"context"
"github.com/cloudwego-contrib/cwgo-pkg/instrumentation/hertzobs/oteltracer"
"net/http"
"testing"
"time"
Expand All @@ -34,7 +33,7 @@ import (
func TestServerMiddleware(t *testing.T) {
sr := tracetest.NewSpanRecorder()
otel.SetTracerProvider(sdktrace.NewTracerProvider(sdktrace.WithSpanProcessor(sr)))
tracer, cfg := oteltracer.NewServerTracer(oteltracer.WithCustomResponseHandler(func(c context.Context, ctx *app.RequestContext) {
tracer, cfg := NewServerTracer(WithCustomResponseHandler(func(c context.Context, ctx *app.RequestContext) {
ctx.Header("trace-id", oteltrace.SpanFromContext(c).SpanContext().TraceID().String())
}))
h := server.Default(tracer, server.WithHostPorts("127.0.0.1:6666"))
Expand All @@ -52,7 +51,7 @@ func TestServerMiddleware(t *testing.T) {
func TestServerMiddlewareDisableTrace(t *testing.T) {
sr := tracetest.NewSpanRecorder()
otel.SetTracerProvider(sdktrace.NewTracerProvider(sdktrace.WithSpanProcessor(sr)))
tracer, cfg := oteltracer.NewServerTracer(oteltracer.WithCustomResponseHandler(func(c context.Context, ctx *app.RequestContext) {
tracer, cfg := NewServerTracer(WithCustomResponseHandler(func(c context.Context, ctx *app.RequestContext) {
ctx.Header("trace-id", oteltrace.SpanFromContext(c).SpanContext().TraceID().String())
}))
h := server.Default(tracer,
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -12,11 +12,10 @@
// See the License for the specific language governing permissions and
// limitations under the License.

package oteltracer
package hertzobs

import (
"context"
"github.com/cloudwego-contrib/cwgo-pkg/instrumentation/hertzobs"
"github.com/cloudwego/hertz/pkg/app"
"github.com/cloudwego/hertz/pkg/protocol"
"go.opentelemetry.io/otel"
Expand Down Expand Up @@ -71,12 +70,12 @@ func NewConfig(opts []Option) *Config {

cfg.meter = cfg.meterProvider.Meter(
instrumentationName,
metric.WithInstrumentationVersion(hertzobs.SemVersion()),
metric.WithInstrumentationVersion(SemVersion()),
)

cfg.tracer = cfg.tracerProvider.Tracer(
instrumentationName,
trace.WithInstrumentationVersion(hertzobs.SemVersion()),
trace.WithInstrumentationVersion(SemVersion()),
)

return cfg
Expand Down
72 changes: 0 additions & 72 deletions instrumentation/hertzobs/oteltracer/utils.go

This file was deleted.

5 changes: 2 additions & 3 deletions instrumentation/hertzobs/propagator.go
Original file line number Diff line number Diff line change
Expand Up @@ -16,7 +16,6 @@ package hertzobs

import (
"context"
"github.com/cloudwego-contrib/cwgo-pkg/instrumentation/hertzobs/oteltracer"
"github.com/cloudwego/hertz/pkg/protocol"
"go.opentelemetry.io/otel/baggage"
"go.opentelemetry.io/otel/propagation"
Expand Down Expand Up @@ -52,12 +51,12 @@ func (m *metadataProvider) Keys() []string {
}

// Inject injects span context into the hertzobs metadata info
func Inject(ctx context.Context, c *oteltracer.Config, headers *protocol.RequestHeader) {
func Inject(ctx context.Context, c *Config, headers *protocol.RequestHeader) {
c.GetTextMapPropagator().Inject(ctx, &metadataProvider{headers: headers})
}

// Extract returns the baggage and span context
func Extract(ctx context.Context, c *oteltracer.Config, headers *protocol.RequestHeader) (baggage.Baggage, trace.SpanContext) {
func Extract(ctx context.Context, c *Config, headers *protocol.RequestHeader) (baggage.Baggage, trace.SpanContext) {
ctx = c.GetTextMapPropagator().Extract(ctx, &metadataProvider{headers: headers})
return baggage.FromContext(ctx), trace.SpanContextFromContext(ctx)
}
9 changes: 4 additions & 5 deletions instrumentation/hertzobs/propagator_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -17,7 +17,6 @@ package hertzobs
import (
"context"
"github.com/bytedance/gopkg/cloud/metainfo"
"github.com/cloudwego-contrib/cwgo-pkg/instrumentation/hertzobs/oteltracer"
"reflect"
"testing"

Expand All @@ -41,7 +40,7 @@ func TestExtract(t *testing.T) {

type args struct {
ctx context.Context
c *oteltracer.Config
c *Config
metadata *protocol.RequestHeader
}
tests := []struct {
Expand All @@ -54,7 +53,7 @@ func TestExtract(t *testing.T) {
name: "extract successful",
args: args{
ctx: ctx,
c: oteltracer.DefaultConfig(),
c: DefaultConfig(),
metadata: headers,
},
want: bags,
Expand All @@ -75,7 +74,7 @@ func TestExtract(t *testing.T) {
}

func TestInject(t *testing.T) {
cfg := oteltracer.NewConfig([]oteltracer.Option{oteltracer.WithTextMapPropagator(propagation.NewCompositeTextMapPropagator(
cfg := NewConfig([]Option{WithTextMapPropagator(propagation.NewCompositeTextMapPropagator(
b3.New(),
ot.OT{},
propagation.Baggage{},
Expand All @@ -97,7 +96,7 @@ func TestInject(t *testing.T) {

type args struct {
ctx context.Context
c *oteltracer.Config
c *Config
metadata *protocol.RequestHeader
}
tests := []struct {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -12,13 +12,11 @@
// See the License for the specific language governing permissions and
// limitations under the License.

package oteltracer
package hertzobs

import (
"github.com/cloudwego-contrib/cwgo-pkg/instrumentation/hertzobs"
cwmetric "github.com/cloudwego-contrib/cwgo-pkg/meter/metric"
"github.com/cloudwego-contrib/cwgo-pkg/semantic"
"go.opentelemetry.io/otel"
"go.opentelemetry.io/otel/metric"

"github.com/cloudwego/hertz/pkg/app/server"
Expand All @@ -27,7 +25,7 @@ import (

func NewServerTracer(opts ...Option) (serverconfig.Option, *Config) {
cfg := NewConfig(opts)
st := &hertzobs.HertzTracer{}
st := &HertzTracer{}

serverRequestCountMeasure, err := cfg.meter.Int64Counter(
semantic.ServerRequestCount,
Expand All @@ -42,14 +40,8 @@ func NewServerTracer(opts ...Option) (serverconfig.Option, *Config) {
metric.WithDescription("measures th incoming end to end duration"),
)
handleErr(err)
labelControl := hertzobs.NewOtelLabelControl(cfg.tracer, cfg.shouldIgnore, cfg.serverHttpRouteFormatter)
labelControl := NewOtelLabelControl(cfg.tracer, cfg.shouldIgnore, cfg.serverHttpRouteFormatter)
st.Measure = cwmetric.NewMeasure(cwmetric.NewOtelCounter(serverRequestCountMeasure), cwmetric.NewOtelRecorder(serverLatencyMeasure), labelControl)

return server.WithTracer(st), cfg
}

func handleErr(err error) {
if err != nil {
otel.Handle(err)
}
}
Loading

0 comments on commit 458b34a

Please sign in to comment.