khenaidoo | ab1f7bd | 2019-11-14 14:00:27 -0500 | [diff] [blame] | 1 | // Copyright 2016 Michal Witkowski. All Rights Reserved. |
| 2 | // See LICENSE for licensing terms. |
| 3 | |
| 4 | package grpc_prometheus |
| 5 | |
| 6 | import ( |
| 7 | "strings" |
| 8 | |
| 9 | "google.golang.org/grpc" |
| 10 | "google.golang.org/grpc/codes" |
| 11 | ) |
| 12 | |
| 13 | type grpcType string |
| 14 | |
| 15 | const ( |
| 16 | Unary grpcType = "unary" |
| 17 | ClientStream grpcType = "client_stream" |
| 18 | ServerStream grpcType = "server_stream" |
| 19 | BidiStream grpcType = "bidi_stream" |
| 20 | ) |
| 21 | |
| 22 | var ( |
| 23 | allCodes = []codes.Code{ |
| 24 | codes.OK, codes.Canceled, codes.Unknown, codes.InvalidArgument, codes.DeadlineExceeded, codes.NotFound, |
| 25 | codes.AlreadyExists, codes.PermissionDenied, codes.Unauthenticated, codes.ResourceExhausted, |
| 26 | codes.FailedPrecondition, codes.Aborted, codes.OutOfRange, codes.Unimplemented, codes.Internal, |
| 27 | codes.Unavailable, codes.DataLoss, |
| 28 | } |
| 29 | ) |
| 30 | |
| 31 | func splitMethodName(fullMethodName string) (string, string) { |
| 32 | fullMethodName = strings.TrimPrefix(fullMethodName, "/") // remove leading slash |
| 33 | if i := strings.Index(fullMethodName, "/"); i >= 0 { |
| 34 | return fullMethodName[:i], fullMethodName[i+1:] |
| 35 | } |
| 36 | return "unknown", "unknown" |
| 37 | } |
| 38 | |
| 39 | func typeFromMethodInfo(mInfo *grpc.MethodInfo) grpcType { |
| 40 | if !mInfo.IsClientStream && !mInfo.IsServerStream { |
| 41 | return Unary |
| 42 | } |
| 43 | if mInfo.IsClientStream && !mInfo.IsServerStream { |
| 44 | return ClientStream |
| 45 | } |
| 46 | if !mInfo.IsClientStream && mInfo.IsServerStream { |
| 47 | return ServerStream |
| 48 | } |
| 49 | return BidiStream |
| 50 | } |