#348-v3 add check method in should be skipped #189

Merged
vtolstov merged 3 commits from kgorbunov/micro-server-http:#348-v3 into v3 2024-09-20 16:52:49 +03:00

View File

@ -15,6 +15,7 @@ import (
"go.unistack.org/micro/v3/errors" "go.unistack.org/micro/v3/errors"
"go.unistack.org/micro/v3/logger" "go.unistack.org/micro/v3/logger"
"go.unistack.org/micro/v3/metadata" "go.unistack.org/micro/v3/metadata"
"go.unistack.org/micro/v3/meter"
"go.unistack.org/micro/v3/options" "go.unistack.org/micro/v3/options"
"go.unistack.org/micro/v3/register" "go.unistack.org/micro/v3/register"
"go.unistack.org/micro/v3/semconv" "go.unistack.org/micro/v3/semconv"
@ -435,20 +436,29 @@ 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 {
h.opts.Meter.Counter(semconv.ServerRequestInflight, "endpoint", h.hd.Name()).Inc() if !slices.Contains(tracer.DefaultSkipEndpoints, h.hd.Name()) {
ctx, sp = h.opts.Tracer.Start(ctx, h.hd.Name()+" rpc-server", ctx, sp = h.opts.Tracer.Start(ctx, h.hd.Name()+" rpc-server",
tracer.WithSpanKind(tracer.SpanKindServer), tracer.WithSpanKind(tracer.SpanKindServer),
tracer.WithSpanLabels( tracer.WithSpanLabels(
"endpoint", h.hd.Name(), "endpoint", h.hd.Name(),
), ),
) )
hdlr.ServeHTTP(w, r.WithContext(ctx)) defer func() {
n := GetRspCode(ctx)
if s, _ := sp.Status(); s != tracer.SpanStatusError && n > 399 {
sp.SetStatus(tracer.SpanStatusError, http.StatusText(n))
}
sp.Finish()
}()
}
if !slices.Contains(meter.DefaultSkipEndpoints, h.hd.Name()) {
h.opts.Meter.Counter(semconv.ServerRequestInflight, "endpoint", h.hd.Name()).Inc()
defer func() {
n := GetRspCode(ctx) n := GetRspCode(ctx)
if n > 399 { if n > 399 {
h.opts.Meter.Counter(semconv.ServerRequestTotal, "endpoint", h.hd.Name(), "status", "success", "code", strconv.Itoa(n)).Inc() h.opts.Meter.Counter(semconv.ServerRequestTotal, "endpoint", h.hd.Name(), "status", "success", "code", strconv.Itoa(n)).Inc()
if s, _ := sp.Status(); s != tracer.SpanStatusError {
sp.SetStatus(tracer.SpanStatusError, http.StatusText(n))
}
} else { } else {
h.opts.Meter.Counter(semconv.ServerRequestTotal, "endpoint", h.hd.Name(), "status", "failure", "code", strconv.Itoa(n)).Inc() h.opts.Meter.Counter(semconv.ServerRequestTotal, "endpoint", h.hd.Name(), "status", "failure", "code", strconv.Itoa(n)).Inc()
} }
@ -456,28 +466,36 @@ func (h *Server) ServeHTTP(w http.ResponseWriter, r *http.Request) {
h.opts.Meter.Summary(semconv.ServerRequestLatencyMicroseconds, "endpoint", h.hd.Name()).Update(te.Seconds()) h.opts.Meter.Summary(semconv.ServerRequestLatencyMicroseconds, "endpoint", h.hd.Name()).Update(te.Seconds())
h.opts.Meter.Histogram(semconv.ServerRequestDurationSeconds, "endpoint", h.hd.Name()).Update(te.Seconds()) h.opts.Meter.Histogram(semconv.ServerRequestDurationSeconds, "endpoint", h.hd.Name()).Update(te.Seconds())
h.opts.Meter.Counter(semconv.ServerRequestInflight, "endpoint", h.hd.Name()).Dec() h.opts.Meter.Counter(semconv.ServerRequestInflight, "endpoint", h.hd.Name()).Dec()
sp.Finish() }()
}
hdlr.ServeHTTP(w, r.WithContext(ctx))
return return
} }
} else if !match { } else if !match {
// check for http.HandlerFunc handlers // check for http.HandlerFunc handlers
if !slices.Contains(tracer.DefaultSkipEndpoints, r.URL.Path) {
ctx, sp = h.opts.Tracer.Start(ctx, r.URL.Path+" 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,
), ),
) )
if ph, _, err := h.pathHandlers.Search(r.Method, r.URL.Path); err == nil {
ph.(http.HandlerFunc)(w, r.WithContext(ctx)) defer func() {
if n := GetRspCode(ctx); n > 399 { if n := GetRspCode(ctx); n > 399 {
sp.SetStatus(tracer.SpanStatusError, http.StatusText(n)) sp.SetStatus(tracer.SpanStatusError, http.StatusText(n))
} else {
sp.SetStatus(tracer.SpanStatusError, http.StatusText(http.StatusNotFound))
} }
sp.Finish() sp.Finish()
}()
}
if ph, _, err := h.pathHandlers.Search(r.Method, r.URL.Path); err == nil {
ph.(http.HandlerFunc)(w, r.WithContext(ctx))
return return
} }
h.errorHandler(ctx, nil, w, r, fmt.Errorf("not matching route found"), http.StatusNotFound) h.errorHandler(ctx, nil, w, r, fmt.Errorf("not matching route found"), http.StatusNotFound)
sp.SetStatus(tracer.SpanStatusError, http.StatusText(http.StatusNotFound))
sp.Finish()
return return
} }
@ -495,6 +513,7 @@ func (h *Server) ServeHTTP(w http.ResponseWriter, r *http.Request) {
ctx, sp = h.opts.Tracer.Start(ctx, endpointName+" rpc-server", topts...) ctx, sp = h.opts.Tracer.Start(ctx, endpointName+" rpc-server", topts...)
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).Update(te.Seconds()) h.opts.Meter.Summary(semconv.ServerRequestLatencyMicroseconds, "endpoint", handler.name).Update(te.Seconds())
@ -503,13 +522,20 @@ func (h *Server) ServeHTTP(w http.ResponseWriter, r *http.Request) {
n := GetRspCode(ctx) n := GetRspCode(ctx)
if n > 399 { if n > 399 {
if s, _ := sp.Status(); s != tracer.SpanStatusError {
sp.SetStatus(tracer.SpanStatusError, http.StatusText(n))
}
h.opts.Meter.Counter(semconv.ServerRequestTotal, "endpoint", handler.name, "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, "status", "success", "code", strconv.Itoa(n)).Inc() h.opts.Meter.Counter(semconv.ServerRequestTotal, "endpoint", handler.name, "status", "success", "code", strconv.Itoa(n)).Inc()
} }
}()
}
defer func() {
n := GetRspCode(ctx)
if n > 399 {
if s, _ := sp.Status(); s != tracer.SpanStatusError {
sp.SetStatus(tracer.SpanStatusError, http.StatusText(n))
}
}
sp.Finish() sp.Finish()
}() }()