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:
commit
57a7ee9b50
52
handler.go
52
handler.go
@ -15,6 +15,7 @@ import (
|
||||
"go.unistack.org/micro/v3/errors"
|
||||
"go.unistack.org/micro/v3/logger"
|
||||
"go.unistack.org/micro/v3/metadata"
|
||||
"go.unistack.org/micro/v3/meter"
|
||||
"go.unistack.org/micro/v3/options"
|
||||
"go.unistack.org/micro/v3/register"
|
||||
"go.unistack.org/micro/v3/semconv"
|
||||
@ -435,20 +436,29 @@ func (h *Server) ServeHTTP(w http.ResponseWriter, r *http.Request) {
|
||||
var sp tracer.Span
|
||||
if !match && h.hd != nil {
|
||||
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",
|
||||
tracer.WithSpanKind(tracer.SpanKindServer),
|
||||
tracer.WithSpanLabels(
|
||||
"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)
|
||||
if n > 399 {
|
||||
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 {
|
||||
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.Histogram(semconv.ServerRequestDurationSeconds, "endpoint", h.hd.Name()).Update(te.Seconds())
|
||||
h.opts.Meter.Counter(semconv.ServerRequestInflight, "endpoint", h.hd.Name()).Dec()
|
||||
sp.Finish()
|
||||
}()
|
||||
}
|
||||
|
||||
hdlr.ServeHTTP(w, r.WithContext(ctx))
|
||||
return
|
||||
}
|
||||
} else if !match {
|
||||
// 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",
|
||||
tracer.WithSpanKind(tracer.SpanKindServer),
|
||||
tracer.WithSpanLabels(
|
||||
"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 {
|
||||
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 {
|
||||
ph.(http.HandlerFunc)(w, r.WithContext(ctx))
|
||||
return
|
||||
}
|
||||
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
|
||||
}
|
||||
|
||||
@ -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...)
|
||||
|
||||
if !slices.Contains(meter.DefaultSkipEndpoints, handler.name) {
|
||||
defer func() {
|
||||
te := time.Since(ts)
|
||||
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)
|
||||
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()
|
||||
} else {
|
||||
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()
|
||||
}()
|
||||
|
||||
|
Loading…
Reference in New Issue
Block a user