interceptor.go 2.6 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677
  1. // Copyright 2016 CoreOS, Inc.
  2. //
  3. // Licensed under the Apache License, Version 2.0 (the "License");
  4. // you may not use this file except in compliance with the License.
  5. // You may obtain a copy of the License at
  6. //
  7. // http://www.apache.org/licenses/LICENSE-2.0
  8. //
  9. // Unless required by applicable law or agreed to in writing, software
  10. // distributed under the License is distributed on an "AS IS" BASIS,
  11. // WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
  12. // See the License for the specific language governing permissions and
  13. // limitations under the License.
  14. package v3rpc
  15. import (
  16. "strings"
  17. "time"
  18. "github.com/coreos/etcd/etcdserver"
  19. "github.com/coreos/etcd/etcdserver/api/v3rpc/rpctypes"
  20. "github.com/coreos/etcd/pkg/types"
  21. "github.com/coreos/etcd/raft"
  22. "golang.org/x/net/context"
  23. "google.golang.org/grpc"
  24. "google.golang.org/grpc/metadata"
  25. )
  26. func newUnaryInterceptor(s *etcdserver.EtcdServer) grpc.UnaryServerInterceptor {
  27. return func(ctx context.Context, req interface{}, info *grpc.UnaryServerInfo, handler grpc.UnaryHandler) (resp interface{}, err error) {
  28. md, ok := metadata.FromContext(ctx)
  29. if ok {
  30. if ks := md[rpctypes.MetadataRequireLeaderKey]; len(ks) > 0 && ks[0] == rpctypes.MetadataHasLeader {
  31. if s.Leader() == types.ID(raft.None) {
  32. return nil, rpctypes.ErrGRPCNoLeader
  33. }
  34. }
  35. }
  36. return metricsUnaryInterceptor(ctx, req, info, handler)
  37. }
  38. }
  39. func metricsUnaryInterceptor(ctx context.Context, req interface{}, info *grpc.UnaryServerInfo, handler grpc.UnaryHandler) (resp interface{}, err error) {
  40. service, method := splitMethodName(info.FullMethod)
  41. receivedCounter.WithLabelValues(service, method).Inc()
  42. start := time.Now()
  43. resp, err = handler(ctx, req)
  44. if err != nil {
  45. failedCounter.WithLabelValues(service, method, grpc.Code(err).String()).Inc()
  46. }
  47. handlingDuration.WithLabelValues(service, method).Observe(time.Since(start).Seconds())
  48. return resp, err
  49. }
  50. func metricsStreamInterceptor(srv interface{}, ss grpc.ServerStream, info *grpc.StreamServerInfo, handler grpc.StreamHandler) error {
  51. service, method := splitMethodName(info.FullMethod)
  52. receivedCounter.WithLabelValues(service, method).Inc()
  53. err := handler(srv, ss)
  54. if err != nil {
  55. failedCounter.WithLabelValues(service, method, grpc.Code(err).String()).Inc()
  56. }
  57. return err
  58. }
  59. func splitMethodName(fullMethodName string) (string, string) {
  60. fullMethodName = strings.TrimPrefix(fullMethodName, "/") // remove leading slash
  61. if i := strings.Index(fullMethodName, "/"); i >= 0 {
  62. return fullMethodName[:i], fullMethodName[i+1:]
  63. }
  64. return "unknown", "unknown"
  65. }