fix metadata extract and trace span creating
Signed-off-by: Vasiliy Tolstov <v.tolstov@unistack.org>
This commit is contained in:
parent
0b29668fe5
commit
3fc05ae291
78
grpc.go
78
grpc.go
@ -210,34 +210,11 @@ func (g *Server) handler(srv interface{}, stream grpc.ServerStream) error {
|
|||||||
return status.Errorf(codes.Internal, "method does not exist in context")
|
return status.Errorf(codes.Internal, "method does not exist in context")
|
||||||
}
|
}
|
||||||
|
|
||||||
var sp tracer.Span
|
// get grpc metadata
|
||||||
ctx, sp = g.opts.Tracer.Start(ctx, fullMethod+" rpc-server",
|
gmd, ok := gmetadata.FromIncomingContext(ctx)
|
||||||
tracer.WithSpanKind(tracer.SpanKindServer),
|
if !ok {
|
||||||
tracer.WithSpanLabels(
|
gmd = gmetadata.MD{}
|
||||||
"endpoint", fullMethod,
|
}
|
||||||
),
|
|
||||||
)
|
|
||||||
|
|
||||||
stream = &streamWrapper{ctx, stream}
|
|
||||||
|
|
||||||
ts := time.Now()
|
|
||||||
g.opts.Meter.Counter(semconv.ServerRequestInflight, "endpoint", fullMethod).Inc()
|
|
||||||
|
|
||||||
defer func() {
|
|
||||||
te := time.Since(ts)
|
|
||||||
g.opts.Meter.Summary(semconv.ServerRequestLatencyMicroseconds, "endpoint", fullMethod).Update(te.Seconds())
|
|
||||||
g.opts.Meter.Histogram(semconv.ServerRequestDurationSeconds, "endpoint", fullMethod).Update(te.Seconds())
|
|
||||||
g.opts.Meter.Counter(semconv.ServerRequestInflight, "endpoint", fullMethod).Dec()
|
|
||||||
|
|
||||||
st := status.Convert(err)
|
|
||||||
if st == nil || st.Code() == codes.OK {
|
|
||||||
g.opts.Meter.Counter(semconv.ServerRequestTotal, "endpoint", fullMethod, "status", "success", "code", strconv.Itoa(int(codes.OK))).Inc()
|
|
||||||
} else {
|
|
||||||
sp.SetStatus(tracer.SpanStatusError, err.Error())
|
|
||||||
g.opts.Meter.Counter(semconv.ServerRequestTotal, "endpoint", fullMethod, "status", "failure", "code", strconv.Itoa(int(st.Code()))).Inc()
|
|
||||||
}
|
|
||||||
sp.Finish()
|
|
||||||
}()
|
|
||||||
|
|
||||||
var serviceName, methodName string
|
var serviceName, methodName string
|
||||||
serviceName, methodName, err = serviceMethod(fullMethod)
|
serviceName, methodName, err = serviceMethod(fullMethod)
|
||||||
@ -246,17 +223,6 @@ func (g *Server) handler(srv interface{}, stream grpc.ServerStream) error {
|
|||||||
return err
|
return err
|
||||||
}
|
}
|
||||||
|
|
||||||
if g.opts.Wait != nil {
|
|
||||||
g.opts.Wait.Add(1)
|
|
||||||
defer g.opts.Wait.Done()
|
|
||||||
}
|
|
||||||
|
|
||||||
// get grpc metadata
|
|
||||||
gmd, ok := gmetadata.FromIncomingContext(ctx)
|
|
||||||
if !ok {
|
|
||||||
gmd = gmetadata.MD{}
|
|
||||||
}
|
|
||||||
|
|
||||||
md := metadata.New(len(gmd))
|
md := metadata.New(len(gmd))
|
||||||
for k, v := range gmd {
|
for k, v := range gmd {
|
||||||
md.Set(k, strings.Join(v, ", "))
|
md.Set(k, strings.Join(v, ", "))
|
||||||
@ -304,6 +270,40 @@ func (g *Server) handler(srv interface{}, stream grpc.ServerStream) error {
|
|||||||
// create new context
|
// create new context
|
||||||
ctx = metadata.NewIncomingContext(ctx, md)
|
ctx = metadata.NewIncomingContext(ctx, md)
|
||||||
|
|
||||||
|
var sp tracer.Span
|
||||||
|
ctx, sp = g.opts.Tracer.Start(ctx, fullMethod+" rpc-server",
|
||||||
|
tracer.WithSpanKind(tracer.SpanKindServer),
|
||||||
|
tracer.WithSpanLabels(
|
||||||
|
"endpoint", fullMethod,
|
||||||
|
),
|
||||||
|
)
|
||||||
|
|
||||||
|
stream = &streamWrapper{ctx, stream}
|
||||||
|
|
||||||
|
ts := time.Now()
|
||||||
|
g.opts.Meter.Counter(semconv.ServerRequestInflight, "endpoint", fullMethod).Inc()
|
||||||
|
|
||||||
|
defer func() {
|
||||||
|
te := time.Since(ts)
|
||||||
|
g.opts.Meter.Summary(semconv.ServerRequestLatencyMicroseconds, "endpoint", fullMethod).Update(te.Seconds())
|
||||||
|
g.opts.Meter.Histogram(semconv.ServerRequestDurationSeconds, "endpoint", fullMethod).Update(te.Seconds())
|
||||||
|
g.opts.Meter.Counter(semconv.ServerRequestInflight, "endpoint", fullMethod).Dec()
|
||||||
|
|
||||||
|
st := status.Convert(err)
|
||||||
|
if st == nil || st.Code() == codes.OK {
|
||||||
|
g.opts.Meter.Counter(semconv.ServerRequestTotal, "endpoint", fullMethod, "status", "success", "code", strconv.Itoa(int(codes.OK))).Inc()
|
||||||
|
} else {
|
||||||
|
sp.SetStatus(tracer.SpanStatusError, err.Error())
|
||||||
|
g.opts.Meter.Counter(semconv.ServerRequestTotal, "endpoint", fullMethod, "status", "failure", "code", strconv.Itoa(int(st.Code()))).Inc()
|
||||||
|
}
|
||||||
|
sp.Finish()
|
||||||
|
}()
|
||||||
|
|
||||||
|
if g.opts.Wait != nil {
|
||||||
|
g.opts.Wait.Add(1)
|
||||||
|
defer g.opts.Wait.Done()
|
||||||
|
}
|
||||||
|
|
||||||
// get peer from context
|
// get peer from context
|
||||||
if p, ok := peer.FromContext(ctx); ok {
|
if p, ok := peer.FromContext(ctx); ok {
|
||||||
md.Set("Remote", p.Addr.String())
|
md.Set("Remote", p.Addr.String())
|
||||||
|
Loading…
Reference in New Issue
Block a user