-
Notifications
You must be signed in to change notification settings - Fork 21
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
Viktor Nosov
committed
Jul 16, 2023
1 parent
bb3459d
commit aa6d9b9
Showing
1 changed file
with
106 additions
and
0 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,106 @@ | ||
package hlf | ||
|
||
import ( | ||
"context" | ||
"errors" | ||
|
||
"go.opencensus.io/plugin/ocgrpc" | ||
"go.opencensus.io/trace" | ||
"google.golang.org/grpc/stats" | ||
) | ||
|
||
// Wrap easy way for plug-up grpc.StatsHandler and mixing needed annotations to span | ||
func Wrap(statsHandler *ocgrpc.ClientHandler) stats.Handler { | ||
return &wrapper{ | ||
oc: statsHandler, | ||
} | ||
} | ||
|
||
type wrapper struct { | ||
oc *ocgrpc.ClientHandler | ||
} | ||
|
||
func (w *wrapper) TagConn(ctx context.Context, connInfo *stats.ConnTagInfo) context.Context { | ||
if w.oc != nil { | ||
return w.oc.TagConn(ctx, connInfo) | ||
} | ||
|
||
return ctx | ||
} | ||
|
||
func (w *wrapper) HandleConn(ctx context.Context, cs stats.ConnStats) { | ||
if w.oc != nil { | ||
w.oc.HandleConn(ctx, cs) | ||
} | ||
} | ||
|
||
func (w *wrapper) TagRPC(ctx context.Context, rpcInfo *stats.RPCTagInfo) context.Context { | ||
if w.oc != nil { | ||
return w.oc.TagRPC(ctx, rpcInfo) | ||
} | ||
return ctx | ||
} | ||
|
||
func (w *wrapper) HandleRPC(ctx context.Context, rs stats.RPCStats) { | ||
span := trace.FromContext(ctx) | ||
|
||
switch rs := rs.(type) { | ||
case *stats.InHeader: | ||
if rs.RemoteAddr != nil { | ||
span.AddAttributes( | ||
trace.StringAttribute( | ||
"InHeader.RemoteAddr", | ||
rs.RemoteAddr.String(), | ||
), | ||
) | ||
} | ||
if rs.LocalAddr != nil { | ||
span.AddAttributes( | ||
trace.StringAttribute( | ||
"InHeader.LocalAddr", | ||
rs.LocalAddr.String(), | ||
), | ||
) | ||
} | ||
|
||
span.AddAttributes( | ||
trace.StringAttribute( | ||
"InHeader.Compression", | ||
rs.Compression, | ||
), | ||
) | ||
case *stats.OutHeader: | ||
if rs.RemoteAddr != nil { | ||
span.AddAttributes( | ||
trace.StringAttribute( | ||
"OutHeader.RemoteAddr", | ||
rs.RemoteAddr.String(), | ||
), | ||
) | ||
} | ||
if rs.LocalAddr != nil { | ||
span.AddAttributes( | ||
trace.StringAttribute( | ||
"OutHeader.LocalAddr", | ||
rs.LocalAddr.String(), | ||
), | ||
) | ||
} | ||
|
||
span.AddAttributes( | ||
trace.StringAttribute( | ||
"OutHeader.Compression", | ||
rs.Compression, | ||
), | ||
) | ||
} | ||
// sometimes we get cancelled context if further execution(asking peers etc.) isn't necessary | ||
// but request is fully valid, and we don't want to see confusing errors in jaeger | ||
if errors.Is(ctx.Err(), context.Canceled) { | ||
return | ||
} | ||
|
||
if w.oc != nil { | ||
w.oc.HandleRPC(ctx, rs) | ||
} | ||
} |