|
| 1 | +package otelgrpc |
| 2 | + |
| 3 | +import ( |
| 4 | + "context" |
| 5 | + "net" |
| 6 | + "strings" |
| 7 | + "time" |
| 8 | + |
| 9 | + "github.com/raystack/meteor/utils" |
| 10 | + "go.opentelemetry.io/otel" |
| 11 | + "go.opentelemetry.io/otel/attribute" |
| 12 | + "go.opentelemetry.io/otel/metric" |
| 13 | + semconv "go.opentelemetry.io/otel/semconv/v1.20.0" |
| 14 | + "google.golang.org/grpc" |
| 15 | + "google.golang.org/grpc/peer" |
| 16 | + "google.golang.org/protobuf/proto" |
| 17 | +) |
| 18 | + |
| 19 | +type UnaryParams struct { |
| 20 | + Start time.Time |
| 21 | + Method string |
| 22 | + Req any |
| 23 | + Res any |
| 24 | + Err error |
| 25 | +} |
| 26 | + |
| 27 | +type Monitor struct { |
| 28 | + duration metric.Int64Histogram |
| 29 | + requestSize metric.Int64Histogram |
| 30 | + responseSize metric.Int64Histogram |
| 31 | + attributes []attribute.KeyValue |
| 32 | +} |
| 33 | + |
| 34 | +func NewOtelGRPCMonitor(hostName string) Monitor { |
| 35 | + meter := otel.Meter("github.com/raystack/meteor/metrics/otelgrpc") |
| 36 | + |
| 37 | + duration, err := meter.Int64Histogram("rpc.client.duration", metric.WithUnit("ms")) |
| 38 | + handleOtelErr(err) |
| 39 | + |
| 40 | + requestSize, err := meter.Int64Histogram("rpc.client.request.size", metric.WithUnit("By")) |
| 41 | + handleOtelErr(err) |
| 42 | + |
| 43 | + responseSize, err := meter.Int64Histogram("rpc.client.response.size", metric.WithUnit("By")) |
| 44 | + handleOtelErr(err) |
| 45 | + |
| 46 | + addr, port := ExtractAddress(hostName) |
| 47 | + |
| 48 | + return Monitor{ |
| 49 | + duration: duration, |
| 50 | + requestSize: requestSize, |
| 51 | + responseSize: responseSize, |
| 52 | + attributes: []attribute.KeyValue{ |
| 53 | + semconv.RPCSystemGRPC, |
| 54 | + attribute.String("network.transport", "tcp"), |
| 55 | + attribute.String("server.address", addr), |
| 56 | + attribute.String("server.port", port), |
| 57 | + }, |
| 58 | + } |
| 59 | +} |
| 60 | + |
| 61 | +func GetProtoSize(p any) int { |
| 62 | + if p == nil { |
| 63 | + return 0 |
| 64 | + } |
| 65 | + |
| 66 | + size := proto.Size(p.(proto.Message)) |
| 67 | + return size |
| 68 | +} |
| 69 | + |
| 70 | +func (m *Monitor) RecordUnary(ctx context.Context, p UnaryParams) { |
| 71 | + reqSize := GetProtoSize(p.Req) |
| 72 | + resSize := GetProtoSize(p.Res) |
| 73 | + |
| 74 | + attrs := make([]attribute.KeyValue, len(m.attributes)) |
| 75 | + copy(attrs, m.attributes) |
| 76 | + attrs = append(attrs, attribute.String("rpc.grpc.status_text", utils.StatusText(p.Err))) |
| 77 | + attrs = append(attrs, attribute.String("network.type", netTypeFromCtx(ctx))) |
| 78 | + attrs = append(attrs, ParseFullMethod(p.Method)...) |
| 79 | + |
| 80 | + m.duration.Record(ctx, |
| 81 | + time.Since(p.Start).Milliseconds(), |
| 82 | + metric.WithAttributes(attrs...)) |
| 83 | + |
| 84 | + m.requestSize.Record(ctx, |
| 85 | + int64(reqSize), |
| 86 | + metric.WithAttributes(attrs...)) |
| 87 | + |
| 88 | + m.responseSize.Record(ctx, |
| 89 | + int64(resSize), |
| 90 | + metric.WithAttributes(attrs...)) |
| 91 | +} |
| 92 | + |
| 93 | +func (m *Monitor) RecordStream(ctx context.Context, start time.Time, method string, err error) { |
| 94 | + attrs := make([]attribute.KeyValue, len(m.attributes)) |
| 95 | + copy(attrs, m.attributes) |
| 96 | + attrs = append(attrs, attribute.String("rpc.grpc.status_text", utils.StatusText(err))) |
| 97 | + attrs = append(attrs, attribute.String("network.type", netTypeFromCtx(ctx))) |
| 98 | + attrs = append(attrs, ParseFullMethod(method)...) |
| 99 | + |
| 100 | + m.duration.Record(ctx, |
| 101 | + time.Since(start).Milliseconds(), |
| 102 | + metric.WithAttributes(attrs...)) |
| 103 | +} |
| 104 | + |
| 105 | +func (m *Monitor) UnaryClientInterceptor() grpc.UnaryClientInterceptor { |
| 106 | + return func(ctx context.Context, |
| 107 | + method string, |
| 108 | + req, reply interface{}, |
| 109 | + cc *grpc.ClientConn, |
| 110 | + invoker grpc.UnaryInvoker, |
| 111 | + opts ...grpc.CallOption, |
| 112 | + ) (err error) { |
| 113 | + defer func(start time.Time) { |
| 114 | + m.RecordUnary(ctx, UnaryParams{ |
| 115 | + Start: start, |
| 116 | + Req: req, |
| 117 | + Res: reply, |
| 118 | + Err: err, |
| 119 | + }) |
| 120 | + }(time.Now()) |
| 121 | + |
| 122 | + return invoker(ctx, method, req, reply, cc, opts...) |
| 123 | + } |
| 124 | +} |
| 125 | + |
| 126 | +func (m *Monitor) StreamClientInterceptor() grpc.StreamClientInterceptor { |
| 127 | + return func(ctx context.Context, |
| 128 | + desc *grpc.StreamDesc, |
| 129 | + cc *grpc.ClientConn, |
| 130 | + method string, |
| 131 | + streamer grpc.Streamer, |
| 132 | + opts ...grpc.CallOption, |
| 133 | + ) (s grpc.ClientStream, err error) { |
| 134 | + defer func(start time.Time) { |
| 135 | + m.RecordStream(ctx, start, method, err) |
| 136 | + }(time.Now()) |
| 137 | + |
| 138 | + return streamer(ctx, desc, cc, method, opts...) |
| 139 | + } |
| 140 | +} |
| 141 | + |
| 142 | +func (m *Monitor) GetAttributes() []attribute.KeyValue { |
| 143 | + return m.attributes |
| 144 | +} |
| 145 | + |
| 146 | +func ParseFullMethod(fullMethod string) []attribute.KeyValue { |
| 147 | + name := strings.TrimLeft(fullMethod, "/") |
| 148 | + service, method, found := strings.Cut(name, "/") |
| 149 | + if !found { |
| 150 | + return nil |
| 151 | + } |
| 152 | + |
| 153 | + var attrs []attribute.KeyValue |
| 154 | + if service != "" { |
| 155 | + attrs = append(attrs, semconv.RPCService(service)) |
| 156 | + } |
| 157 | + if method != "" { |
| 158 | + attrs = append(attrs, semconv.RPCMethod(method)) |
| 159 | + } |
| 160 | + return attrs |
| 161 | +} |
| 162 | + |
| 163 | +func handleOtelErr(err error) { |
| 164 | + if err != nil { |
| 165 | + otel.Handle(err) |
| 166 | + } |
| 167 | +} |
| 168 | + |
| 169 | +func ExtractAddress(addr string) (host, port string) { |
| 170 | + host, port, err := net.SplitHostPort(addr) |
| 171 | + if err != nil { |
| 172 | + return addr, "80" |
| 173 | + } |
| 174 | + |
| 175 | + return host, port |
| 176 | +} |
| 177 | + |
| 178 | +func netTypeFromCtx(ctx context.Context) (ipType string) { |
| 179 | + ipType = "unknown" |
| 180 | + p, ok := peer.FromContext(ctx) |
| 181 | + if !ok { |
| 182 | + return ipType |
| 183 | + } |
| 184 | + |
| 185 | + clientIP, _, err := net.SplitHostPort(p.Addr.String()) |
| 186 | + if err != nil { |
| 187 | + return ipType |
| 188 | + } |
| 189 | + |
| 190 | + ip := net.ParseIP(clientIP) |
| 191 | + if ip.To4() != nil { |
| 192 | + ipType = "ipv4" |
| 193 | + } else if ip.To16() != nil { |
| 194 | + ipType = "ipv6" |
| 195 | + } |
| 196 | + |
| 197 | + return ipType |
| 198 | +} |
0 commit comments