Merge pull request '#348-v3 add check method in should be skipped' (#189) from kgorbunov/micro-server-http:#348-v3 into v3

Reviewed-on: #189
Reviewed-by: Василий Толстов <v.tolstov@unistack.org>
This commit is contained in:
Василий Толстов 2024-09-20 16:52:49 +03:00
commit 57a7ee9b50

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,49 +436,66 @@ 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) n := GetRspCode(ctx)
if n > 399 { if s, _ := sp.Status(); s != tracer.SpanStatusError && n > 399 {
h.opts.Meter.Counter(semconv.ServerRequestTotal, "endpoint", h.hd.Name(), "status", "success", "code", strconv.Itoa(n)).Inc() sp.SetStatus(tracer.SpanStatusError, http.StatusText(n))
if s, _ := sp.Status(); s != tracer.SpanStatusError { }
sp.SetStatus(tracer.SpanStatusError, http.StatusText(n)) sp.Finish()
} }()
} else {
h.opts.Meter.Counter(semconv.ServerRequestTotal, "endpoint", h.hd.Name(), "status", "failure", "code", strconv.Itoa(n)).Inc()
} }
te := time.Since(ts)
h.opts.Meter.Summary(semconv.ServerRequestLatencyMicroseconds, "endpoint", h.hd.Name()).Update(te.Seconds()) if !slices.Contains(meter.DefaultSkipEndpoints, h.hd.Name()) {
h.opts.Meter.Histogram(semconv.ServerRequestDurationSeconds, "endpoint", h.hd.Name()).Update(te.Seconds()) h.opts.Meter.Counter(semconv.ServerRequestInflight, "endpoint", h.hd.Name()).Inc()
h.opts.Meter.Counter(semconv.ServerRequestInflight, "endpoint", h.hd.Name()).Dec()
sp.Finish() defer func() {
n := GetRspCode(ctx)
if n > 399 {
h.opts.Meter.Counter(semconv.ServerRequestTotal, "endpoint", h.hd.Name(), "status", "success", "code", strconv.Itoa(n)).Inc()
} else {
h.opts.Meter.Counter(semconv.ServerRequestTotal, "endpoint", h.hd.Name(), "status", "failure", "code", strconv.Itoa(n)).Inc()
}
te := time.Since(ts)
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.Counter(semconv.ServerRequestInflight, "endpoint", h.hd.Name()).Dec()
}()
}
hdlr.ServeHTTP(w, r.WithContext(ctx))
return return
} }
} else if !match { } else if !match {
// check for http.HandlerFunc handlers // check for http.HandlerFunc handlers
ctx, sp = h.opts.Tracer.Start(ctx, r.URL.Path+" rpc-server", if !slices.Contains(tracer.DefaultSkipEndpoints, r.URL.Path) {
tracer.WithSpanKind(tracer.SpanKindServer), ctx, sp = h.opts.Tracer.Start(ctx, r.URL.Path+" rpc-server",
tracer.WithSpanLabels( tracer.WithSpanKind(tracer.SpanKindServer),
"endpoint", r.URL.Path, tracer.WithSpanLabels(
), "endpoint", r.URL.Path,
) ),
)
defer func() {
if n := GetRspCode(ctx); n > 399 {
sp.SetStatus(tracer.SpanStatusError, http.StatusText(n))
} else {
sp.SetStatus(tracer.SpanStatusError, http.StatusText(http.StatusNotFound))
}
sp.Finish()
}()
}
if ph, _, err := h.pathHandlers.Search(r.Method, r.URL.Path); err == nil { if ph, _, err := h.pathHandlers.Search(r.Method, r.URL.Path); err == nil {
ph.(http.HandlerFunc)(w, r.WithContext(ctx)) ph.(http.HandlerFunc)(w, r.WithContext(ctx))
if n := GetRspCode(ctx); n > 399 {
sp.SetStatus(tracer.SpanStatusError, http.StatusText(n))
}
sp.Finish()
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,20 +513,28 @@ 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...)
defer func() { if !slices.Contains(meter.DefaultSkipEndpoints, handler.name) {
te := time.Since(ts) defer func() {
h.opts.Meter.Summary(semconv.ServerRequestLatencyMicroseconds, "endpoint", handler.name).Update(te.Seconds()) te := time.Since(ts)
h.opts.Meter.Histogram(semconv.ServerRequestDurationSeconds, "endpoint", handler.name).Update(te.Seconds()) h.opts.Meter.Summary(semconv.ServerRequestLatencyMicroseconds, "endpoint", handler.name).Update(te.Seconds())
h.opts.Meter.Counter(semconv.ServerRequestInflight, "endpoint", handler.name).Dec() h.opts.Meter.Histogram(semconv.ServerRequestDurationSeconds, "endpoint", handler.name).Update(te.Seconds())
h.opts.Meter.Counter(semconv.ServerRequestInflight, "endpoint", handler.name).Dec()
n := GetRspCode(ctx)
if n > 399 {
h.opts.Meter.Counter(semconv.ServerRequestTotal, "endpoint", handler.name, "status", "failure", "code", strconv.Itoa(n)).Inc()
} else {
h.opts.Meter.Counter(semconv.ServerRequestTotal, "endpoint", handler.name, "status", "success", "code", strconv.Itoa(n)).Inc()
}
}()
}
defer func() {
n := GetRspCode(ctx) n := GetRspCode(ctx)
if n > 399 { if n > 399 {
if s, _ := sp.Status(); s != tracer.SpanStatusError { if s, _ := sp.Status(); s != tracer.SpanStatusError {
sp.SetStatus(tracer.SpanStatusError, http.StatusText(n)) sp.SetStatus(tracer.SpanStatusError, http.StatusText(n))
} }
h.opts.Meter.Counter(semconv.ServerRequestTotal, "endpoint", handler.name, "status", "failure", "code", strconv.Itoa(n)).Inc()
} else {
h.opts.Meter.Counter(semconv.ServerRequestTotal, "endpoint", handler.name, "status", "success", "code", strconv.Itoa(n)).Inc()
} }
sp.Finish() sp.Finish()
}() }()