|
| 1 | +/* |
| 2 | + * === This file is part of ALICE O² === |
| 3 | + * |
| 4 | + * Copyright 2025 CERN and copyright holders of ALICE O². |
| 5 | + * Author: Michal Tichak <michal.tichak@cern.ch> |
| 6 | + * |
| 7 | + * This program is free software: you can redistribute it and/or modify |
| 8 | + * it under the terms of the GNU General Public License as published by |
| 9 | + * the Free Software Foundation, either version 3 of the License, or |
| 10 | + * (at your option) any later version. |
| 11 | + * |
| 12 | + * This program is distributed in the hope that it will be useful, |
| 13 | + * but WITHOUT ANY WARRANTY; without even the implied warranty of |
| 14 | + * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the |
| 15 | + * GNU General Public License for more details. |
| 16 | + * |
| 17 | + * You should have received a copy of the GNU General Public License |
| 18 | + * along with this program. If not, see <http://www.gnu.org/licenses/>. |
| 19 | + * |
| 20 | + * In applying this license CERN does not waive the privileges and |
| 21 | + * immunities granted to it by virtue of its status as an |
| 22 | + * Intergovernmental Organization or submit itself to any jurisdiction. |
| 23 | + */ |
| 24 | + |
| 25 | +package monitoring |
| 26 | + |
| 27 | +import ( |
| 28 | + "context" |
| 29 | + |
| 30 | + "google.golang.org/grpc" |
| 31 | +) |
| 32 | + |
| 33 | +type measuredClientStream struct { |
| 34 | + grpc.ClientStream |
| 35 | + method string |
| 36 | + metricName string |
| 37 | +} |
| 38 | + |
| 39 | +func (t *measuredClientStream) RecvMsg(m interface{}) error { |
| 40 | + metric := NewMetric(t.metricName) |
| 41 | + metric.AddTag("method", t.method) |
| 42 | + defer TimerSendSingle(&metric, Millisecond)() |
| 43 | + |
| 44 | + err := t.ClientStream.RecvMsg(m) |
| 45 | + return err |
| 46 | +} |
| 47 | + |
| 48 | +type NameConvertType func(string) string |
| 49 | + |
| 50 | +func SetupStreamClientInterceptor(metricName string, convert NameConvertType) grpc.StreamClientInterceptor { |
| 51 | + return func( |
| 52 | + ctx context.Context, |
| 53 | + desc *grpc.StreamDesc, |
| 54 | + cc *grpc.ClientConn, |
| 55 | + method string, |
| 56 | + streamer grpc.Streamer, |
| 57 | + opts ...grpc.CallOption, |
| 58 | + ) (grpc.ClientStream, error) { |
| 59 | + clientStream, err := streamer(ctx, desc, cc, method, opts...) |
| 60 | + if err != nil { |
| 61 | + return nil, err |
| 62 | + } |
| 63 | + |
| 64 | + return &measuredClientStream{ |
| 65 | + ClientStream: clientStream, |
| 66 | + method: convert(method), |
| 67 | + metricName: metricName, |
| 68 | + }, nil |
| 69 | + } |
| 70 | +} |
| 71 | + |
| 72 | +func SetupUnaryClientInterceptor(name string, convert NameConvertType) grpc.UnaryClientInterceptor { |
| 73 | + return func(ctx context.Context, method string, req, reply any, cc *grpc.ClientConn, invoker grpc.UnaryInvoker, opts ...grpc.CallOption) error { |
| 74 | + metric := NewMetric(name) |
| 75 | + metric.AddTag("method", convert(method)) |
| 76 | + defer TimerSendSingle(&metric, Millisecond)() |
| 77 | + return invoker(ctx, method, req, reply, cc, opts...) |
| 78 | + } |
| 79 | +} |
0 commit comments