|
| 1 | +package ktracing |
| 2 | + |
| 3 | +import ( |
| 4 | + "context" |
| 5 | + "net" |
| 6 | + "net/url" |
| 7 | + "strings" |
| 8 | + |
| 9 | + "github.com/go-kratos/kratos/v2/metadata" |
| 10 | + "github.com/go-kratos/kratos/v2/transport" |
| 11 | + "github.com/go-kratos/kratos/v2/transport/http" |
| 12 | + |
| 13 | + "go.opentelemetry.io/otel/attribute" |
| 14 | + semconv "go.opentelemetry.io/otel/semconv/v1.4.0" |
| 15 | + "go.opentelemetry.io/otel/trace" |
| 16 | + "google.golang.org/grpc/peer" |
| 17 | + "google.golang.org/protobuf/proto" |
| 18 | +) |
| 19 | + |
| 20 | +func setClientSpan(ctx context.Context, span trace.Span, m interface{}) { |
| 21 | + var ( |
| 22 | + attrs []attribute.KeyValue |
| 23 | + remote string |
| 24 | + operation string |
| 25 | + rpcKind string |
| 26 | + ) |
| 27 | + tr, ok := transport.FromClientContext(ctx) |
| 28 | + if ok { |
| 29 | + operation = tr.Operation() |
| 30 | + rpcKind = tr.Kind().String() |
| 31 | + switch tr.Kind() { |
| 32 | + case transport.KindHTTP: |
| 33 | + if ht, ok := tr.(http.Transporter); ok { |
| 34 | + method := ht.Request().Method |
| 35 | + route := ht.PathTemplate() |
| 36 | + path := ht.Request().URL.Path |
| 37 | + attrs = append(attrs, semconv.HTTPMethodKey.String(method)) |
| 38 | + attrs = append(attrs, semconv.HTTPRouteKey.String(route)) |
| 39 | + attrs = append(attrs, semconv.HTTPTargetKey.String(path)) |
| 40 | + remote = ht.Request().Host |
| 41 | + } |
| 42 | + case transport.KindGRPC: |
| 43 | + remote, _ = parseTarget(tr.Endpoint()) |
| 44 | + } |
| 45 | + } |
| 46 | + attrs = append(attrs, semconv.RPCSystemKey.String(rpcKind)) |
| 47 | + _, mAttrs := parseFullMethod(operation) |
| 48 | + attrs = append(attrs, mAttrs...) |
| 49 | + if remote != "" { |
| 50 | + attrs = append(attrs, peerAttr(remote)...) |
| 51 | + } |
| 52 | + if p, ok := m.(proto.Message); ok { |
| 53 | + attrs = append(attrs, attribute.Key("send_msg.size").Int(proto.Size(p))) |
| 54 | + } |
| 55 | + |
| 56 | + span.SetAttributes(attrs...) |
| 57 | +} |
| 58 | + |
| 59 | +func setServerSpan(ctx context.Context, span trace.Span, m interface{}) { |
| 60 | + var ( |
| 61 | + attrs []attribute.KeyValue |
| 62 | + remote string |
| 63 | + operation string |
| 64 | + rpcKind string |
| 65 | + ) |
| 66 | + tr, ok := transport.FromServerContext(ctx) |
| 67 | + if ok { |
| 68 | + operation = tr.Operation() |
| 69 | + rpcKind = tr.Kind().String() |
| 70 | + switch tr.Kind() { |
| 71 | + case transport.KindHTTP: |
| 72 | + if ht, ok := tr.(http.Transporter); ok { |
| 73 | + method := ht.Request().Method |
| 74 | + route := ht.PathTemplate() |
| 75 | + path := ht.Request().URL.Path |
| 76 | + attrs = append(attrs, semconv.HTTPMethodKey.String(method)) |
| 77 | + attrs = append(attrs, semconv.HTTPRouteKey.String(route)) |
| 78 | + attrs = append(attrs, semconv.HTTPTargetKey.String(path)) |
| 79 | + remote = ht.Request().RemoteAddr |
| 80 | + } |
| 81 | + case transport.KindGRPC: |
| 82 | + if p, ok := peer.FromContext(ctx); ok { |
| 83 | + remote = p.Addr.String() |
| 84 | + } |
| 85 | + } |
| 86 | + } |
| 87 | + attrs = append(attrs, semconv.RPCSystemKey.String(rpcKind)) |
| 88 | + _, mAttrs := parseFullMethod(operation) |
| 89 | + attrs = append(attrs, mAttrs...) |
| 90 | + attrs = append(attrs, peerAttr(remote)...) |
| 91 | + if p, ok := m.(proto.Message); ok { |
| 92 | + attrs = append(attrs, attribute.Key("recv_msg.size").Int(proto.Size(p))) |
| 93 | + } |
| 94 | + if md, ok := metadata.FromServerContext(ctx); ok { |
| 95 | + attrs = append(attrs, semconv.PeerServiceKey.String(md.Get(serviceHeader))) |
| 96 | + } |
| 97 | + |
| 98 | + span.SetAttributes(attrs...) |
| 99 | +} |
| 100 | + |
| 101 | +// parseFullMethod returns a span name following the OpenTelemetry semantic |
| 102 | +// conventions as well as all applicable span attribute.KeyValue attributes based |
| 103 | +// on a gRPC's FullMethod. |
| 104 | +func parseFullMethod(fullMethod string) (string, []attribute.KeyValue) { |
| 105 | + name := strings.TrimLeft(fullMethod, "/") |
| 106 | + parts := strings.SplitN(name, "/", 2) |
| 107 | + if len(parts) != 2 { //nolint:mnd |
| 108 | + // Invalid format, does not follow `/package.service/method`. |
| 109 | + return name, []attribute.KeyValue{attribute.Key("rpc.operation").String(fullMethod)} |
| 110 | + } |
| 111 | + |
| 112 | + var attrs []attribute.KeyValue |
| 113 | + if service := parts[0]; service != "" { |
| 114 | + attrs = append(attrs, semconv.RPCServiceKey.String(service)) |
| 115 | + } |
| 116 | + if method := parts[1]; method != "" { |
| 117 | + attrs = append(attrs, semconv.RPCMethodKey.String(method)) |
| 118 | + } |
| 119 | + return name, attrs |
| 120 | +} |
| 121 | + |
| 122 | +// peerAttr returns attributes about the peer address. |
| 123 | +func peerAttr(addr string) []attribute.KeyValue { |
| 124 | + host, port, err := net.SplitHostPort(addr) |
| 125 | + if err != nil { |
| 126 | + return []attribute.KeyValue(nil) |
| 127 | + } |
| 128 | + |
| 129 | + if host == "" { |
| 130 | + host = "127.0.0.1" |
| 131 | + } |
| 132 | + |
| 133 | + return []attribute.KeyValue{ |
| 134 | + semconv.NetPeerIPKey.String(host), |
| 135 | + semconv.NetPeerPortKey.String(port), |
| 136 | + } |
| 137 | +} |
| 138 | + |
| 139 | +func parseTarget(endpoint string) (address string, err error) { |
| 140 | + var u *url.URL |
| 141 | + u, err = url.Parse(endpoint) |
| 142 | + if err != nil { |
| 143 | + if u, err = url.Parse("http://" + endpoint); err != nil { |
| 144 | + return "", err |
| 145 | + } |
| 146 | + return u.Host, nil |
| 147 | + } |
| 148 | + if len(u.Path) > 1 { |
| 149 | + return u.Path[1:], nil |
| 150 | + } |
| 151 | + return endpoint, nil |
| 152 | +} |
0 commit comments