Compare commits

..

No commits in common. "v3" and "v3.10.31" have entirely different histories.
v3 ... v3.10.31

2 changed files with 21 additions and 25 deletions

View File

@ -436,12 +436,11 @@ func (h *Server) ServeHTTP(w http.ResponseWriter, r *http.Request) {
var sp tracer.Span var sp tracer.Span
if !match && h.hd != nil { if !match && h.hd != nil {
if hdlr, ok := h.hd.Handler().(http.Handler); ok { if hdlr, ok := h.hd.Handler().(http.Handler); ok {
endpointName := fmt.Sprintf("%s.%s", hldr.name, hldr.mtype.method.Name) if !slices.Contains(tracer.DefaultSkipEndpoints, h.hd.Name()) {
if !slices.Contains(tracer.DefaultSkipEndpoints, endpointName) { ctx, sp = h.opts.Tracer.Start(ctx, h.hd.Name()+" rpc-server",
ctx, sp = h.opts.Tracer.Start(ctx, "rpc-server",
tracer.WithSpanKind(tracer.SpanKindServer), tracer.WithSpanKind(tracer.SpanKindServer),
tracer.WithSpanLabels( tracer.WithSpanLabels(
"endpoint", endpointName, "endpoint", h.hd.Name(),
), ),
) )
defer func() { defer func() {
@ -453,20 +452,20 @@ func (h *Server) ServeHTTP(w http.ResponseWriter, r *http.Request) {
}() }()
} }
if !slices.Contains(meter.DefaultSkipEndpoints, endpointName) { if !slices.Contains(meter.DefaultSkipEndpoints, h.hd.Name()) {
h.opts.Meter.Counter(semconv.ServerRequestInflight, "endpoint", endpointName, "server", "http").Inc() h.opts.Meter.Counter(semconv.ServerRequestInflight, "endpoint", h.hd.Name()).Inc()
defer func() { defer func() {
n := GetRspCode(ctx) n := GetRspCode(ctx)
if n > 399 { if n > 399 {
h.opts.Meter.Counter(semconv.ServerRequestTotal, "endpoint", endpointName, "server", "http", "status", "success", "code", strconv.Itoa(n)).Inc() h.opts.Meter.Counter(semconv.ServerRequestTotal, "endpoint", h.hd.Name(), "status", "success", "code", strconv.Itoa(n)).Inc()
} else { } else {
h.opts.Meter.Counter(semconv.ServerRequestTotal, "endpoint", endpointName, "server", "http", "status", "failure", "code", strconv.Itoa(n)).Inc() h.opts.Meter.Counter(semconv.ServerRequestTotal, "endpoint", h.hd.Name(), "status", "failure", "code", strconv.Itoa(n)).Inc()
} }
te := time.Since(ts) te := time.Since(ts)
h.opts.Meter.Summary(semconv.ServerRequestLatencyMicroseconds, "endpoint", endpointName, "server", "http").Update(te.Seconds()) h.opts.Meter.Summary(semconv.ServerRequestLatencyMicroseconds, "endpoint", h.hd.Name()).Update(te.Seconds())
h.opts.Meter.Histogram(semconv.ServerRequestDurationSeconds, "endpoint", endpointName, "server", "http").Update(te.Seconds()) h.opts.Meter.Histogram(semconv.ServerRequestDurationSeconds, "endpoint", h.hd.Name()).Update(te.Seconds())
h.opts.Meter.Counter(semconv.ServerRequestInflight, "endpoint", endpointName, "server", "http").Dec() h.opts.Meter.Counter(semconv.ServerRequestInflight, "endpoint", h.hd.Name()).Dec()
}() }()
} }
@ -476,11 +475,10 @@ func (h *Server) ServeHTTP(w http.ResponseWriter, r *http.Request) {
} else if !match { } else if !match {
// check for http.HandlerFunc handlers // check for http.HandlerFunc handlers
if !slices.Contains(tracer.DefaultSkipEndpoints, r.URL.Path) { if !slices.Contains(tracer.DefaultSkipEndpoints, r.URL.Path) {
ctx, sp = h.opts.Tracer.Start(ctx, "rpc-server", ctx, sp = h.opts.Tracer.Start(ctx, r.URL.Path+" rpc-server",
tracer.WithSpanKind(tracer.SpanKindServer), tracer.WithSpanKind(tracer.SpanKindServer),
tracer.WithSpanLabels( tracer.WithSpanLabels(
"endpoint", r.URL.Path, "endpoint", r.URL.Path,
"server", "http",
), ),
) )
@ -502,12 +500,10 @@ func (h *Server) ServeHTTP(w http.ResponseWriter, r *http.Request) {
} }
endpointName := fmt.Sprintf("%s.%s", hldr.name, hldr.mtype.method.Name) endpointName := fmt.Sprintf("%s.%s", hldr.name, hldr.mtype.method.Name)
topts := []tracer.SpanOption{ topts := []tracer.SpanOption{
tracer.WithSpanKind(tracer.SpanKindServer), tracer.WithSpanKind(tracer.SpanKindServer),
tracer.WithSpanLabels( tracer.WithSpanLabels(
"endpoint", endpointName, "endpoint", endpointName,
"server", "http",
), ),
} }
@ -515,20 +511,20 @@ func (h *Server) ServeHTTP(w http.ResponseWriter, r *http.Request) {
topts = append(topts, tracer.WithSpanRecord(false)) topts = append(topts, tracer.WithSpanRecord(false))
} }
ctx, sp = h.opts.Tracer.Start(ctx, "rpc-server", topts...) ctx, sp = h.opts.Tracer.Start(ctx, endpointName+" rpc-server", topts...)
if !slices.Contains(meter.DefaultSkipEndpoints, handler.name) { if !slices.Contains(meter.DefaultSkipEndpoints, handler.name) {
defer func() { defer func() {
te := time.Since(ts) te := time.Since(ts)
h.opts.Meter.Summary(semconv.ServerRequestLatencyMicroseconds, "endpoint", handler.name, "server", "http").Update(te.Seconds()) h.opts.Meter.Summary(semconv.ServerRequestLatencyMicroseconds, "endpoint", handler.name).Update(te.Seconds())
h.opts.Meter.Histogram(semconv.ServerRequestDurationSeconds, "endpoint", handler.name, "server", "http").Update(te.Seconds()) h.opts.Meter.Histogram(semconv.ServerRequestDurationSeconds, "endpoint", handler.name).Update(te.Seconds())
h.opts.Meter.Counter(semconv.ServerRequestInflight, "endpoint", handler.name, "server", "http").Dec() h.opts.Meter.Counter(semconv.ServerRequestInflight, "endpoint", handler.name).Dec()
n := GetRspCode(ctx) n := GetRspCode(ctx)
if n > 399 { if n > 399 {
h.opts.Meter.Counter(semconv.ServerRequestTotal, "endpoint", handler.name, "server", "http", "status", "failure", "code", strconv.Itoa(n)).Inc() h.opts.Meter.Counter(semconv.ServerRequestTotal, "endpoint", handler.name, "status", "failure", "code", strconv.Itoa(n)).Inc()
} else { } else {
h.opts.Meter.Counter(semconv.ServerRequestTotal, "endpoint", handler.name, "server", "http", "status", "success", "code", strconv.Itoa(n)).Inc() h.opts.Meter.Counter(semconv.ServerRequestTotal, "endpoint", handler.name, "status", "success", "code", strconv.Itoa(n)).Inc()
} }
}() }()
} }

View File

@ -35,7 +35,7 @@ var bufPool = sync.Pool{
var _ MeterServiceServer = &Handler{} var _ MeterServiceServer = &Handler{}
type Handler struct { type Handler struct {
Options Options opts Options
} }
type Option func(*Options) type Option func(*Options)
@ -81,7 +81,7 @@ func NewOptions(opts ...Option) Options {
func NewHandler(opts ...Option) *Handler { func NewHandler(opts ...Option) *Handler {
options := NewOptions(opts...) options := NewOptions(opts...)
return &Handler{Options: options} return &Handler{opts: options}
} }
func (h *Handler) Metrics(ctx context.Context, req *codecpb.Frame, rsp *codecpb.Frame) error { func (h *Handler) Metrics(ctx context.Context, req *codecpb.Frame, rsp *codecpb.Frame) error {
@ -96,7 +96,7 @@ func (h *Handler) Metrics(ctx context.Context, req *codecpb.Frame, rsp *codecpb.
w := io.Writer(buf) w := io.Writer(buf)
if md, ok := metadata.FromOutgoingContext(ctx); gzipAccepted(md) && ok && !h.Options.DisableCompress { if md, ok := metadata.FromOutgoingContext(ctx); gzipAccepted(md) && ok && !h.opts.DisableCompress {
omd, _ := metadata.FromOutgoingContext(ctx) omd, _ := metadata.FromOutgoingContext(ctx)
omd.Set(contentEncodingHeader, "gzip") omd.Set(contentEncodingHeader, "gzip")
gz := gzipPool.Get().(*gzip.Writer) gz := gzipPool.Get().(*gzip.Writer)
@ -109,7 +109,7 @@ func (h *Handler) Metrics(ctx context.Context, req *codecpb.Frame, rsp *codecpb.
gz.Flush() gz.Flush()
} }
if err := h.Options.Meter.Write(w, h.Options.MeterOptions...); err != nil { if err := h.opts.Meter.Write(w, h.opts.MeterOptions...); err != nil {
log.Error(ctx, "http/meter write failed", err) log.Error(ctx, "http/meter write failed", err)
return nil return nil
} }