Skip to content

Commit

Permalink
fix linter
Browse files Browse the repository at this point in the history
  • Loading branch information
Viktor Nosov committed Jul 16, 2023
1 parent bb3459d commit aa6d9b9
Showing 1 changed file with 106 additions and 0 deletions.
106 changes: 106 additions & 0 deletions opencensus/hlf/hlf.go
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)
}
}

0 comments on commit aa6d9b9

Please sign in to comment.