util.go 1.3 KB

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