| OLD | NEW |
| 1 // Copyright 2016 The LUCI Authors. All rights reserved. | 1 // Copyright 2016 The LUCI Authors. All rights reserved. |
| 2 // Use of this source code is governed under the Apache License, Version 2.0 | 2 // Use of this source code is governed under the Apache License, Version 2.0 |
| 3 // that can be found in the LICENSE file. | 3 // that can be found in the LICENSE file. |
| 4 | 4 |
| 5 package tsmon | 5 package tsmon |
| 6 | 6 |
| 7 import ( | 7 import ( |
| 8 "time" | 8 "time" |
| 9 | 9 |
| 10 "golang.org/x/net/context" | 10 "golang.org/x/net/context" |
| 11 "google.golang.org/grpc" | 11 "google.golang.org/grpc" |
| 12 | 12 |
| 13 "github.com/luci/luci-go/common/clock" | 13 "github.com/luci/luci-go/common/clock" |
| 14 "github.com/luci/luci-go/common/tsmon/distribution" | 14 "github.com/luci/luci-go/common/tsmon/distribution" |
| 15 "github.com/luci/luci-go/common/tsmon/field" | 15 "github.com/luci/luci-go/common/tsmon/field" |
| 16 "github.com/luci/luci-go/common/tsmon/metric" | 16 "github.com/luci/luci-go/common/tsmon/metric" |
| 17 "github.com/luci/luci-go/common/tsmon/types" |
| 17 ) | 18 ) |
| 18 | 19 |
| 19 var ( | 20 var ( |
| 20 grpcServerCount = metric.NewCounter( | 21 grpcServerCount = metric.NewCounter( |
| 21 "grpc/server/count", | 22 "grpc/server/count", |
| 22 "Total number of RPCs.", | 23 "Total number of RPCs.", |
| 24 types.MetricMetadata{}, |
| 23 field.String("method"), // full name of the grpc method | 25 field.String("method"), // full name of the grpc method |
| 24 field.Int("code")) // grpc.Code of the result | 26 field.Int("code")) // grpc.Code of the result |
| 25 | 27 |
| 26 grpcServerDuration = metric.NewCumulativeDistribution( | 28 grpcServerDuration = metric.NewCumulativeDistribution( |
| 27 "grpc/server/duration", | 29 "grpc/server/duration", |
| 28 "Distribution of server-side RPC duration (in milliseconds).", | 30 "Distribution of server-side RPC duration (in milliseconds).", |
| 31 types.MetricMetadata{}, |
| 29 distribution.DefaultBucketer, | 32 distribution.DefaultBucketer, |
| 30 field.String("method"), // full name of the grpc method | 33 field.String("method"), // full name of the grpc method |
| 31 field.Int("code")) // grpc.Code of the result | 34 field.Int("code")) // grpc.Code of the result |
| 32 ) | 35 ) |
| 33 | 36 |
| 34 // NewGrpcUnaryInterceptor returns an interceptor that gathers RPC handler | 37 // NewGrpcUnaryInterceptor returns an interceptor that gathers RPC handler |
| 35 // metrics and sends them to tsmon. | 38 // metrics and sends them to tsmon. |
| 36 // | 39 // |
| 37 // It can be optionally chained with other interceptor. The reported metrics | 40 // It can be optionally chained with other interceptor. The reported metrics |
| 38 // include time spent in this other interceptor too. | 41 // include time spent in this other interceptor too. |
| (...skipping 15 matching lines...) Expand all Loading... |
| 54 | 57 |
| 55 // reportRPCMetrics sends metrics after RPC handler has finished. | 58 // reportRPCMetrics sends metrics after RPC handler has finished. |
| 56 func reportRPCMetrics(ctx context.Context, method string, err error, dur time.Du
ration) { | 59 func reportRPCMetrics(ctx context.Context, method string, err error, dur time.Du
ration) { |
| 57 code := 0 | 60 code := 0 |
| 58 if err != nil { | 61 if err != nil { |
| 59 code = int(grpc.Code(err)) | 62 code = int(grpc.Code(err)) |
| 60 } | 63 } |
| 61 grpcServerCount.Add(ctx, 1, method, code) | 64 grpcServerCount.Add(ctx, 1, method, code) |
| 62 grpcServerDuration.Add(ctx, float64(dur.Nanoseconds()/1000000), method,
code) | 65 grpcServerDuration.Add(ctx, float64(dur.Nanoseconds()/1000000), method,
code) |
| 63 } | 66 } |
| OLD | NEW |