1// Copyright 2017, OpenCensus Authors
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//
15
16package ocgrpc
17
18import (
19	"go.opencensus.io/stats"
20	"go.opencensus.io/stats/view"
21	"go.opencensus.io/tag"
22)
23
24// The following variables are measures are recorded by ServerHandler:
25var (
26	ServerReceivedMessagesPerRPC = stats.Int64("grpc.io/server/received_messages_per_rpc", "Number of messages received in each RPC. Has value 1 for non-streaming RPCs.", stats.UnitDimensionless)
27	ServerReceivedBytesPerRPC    = stats.Int64("grpc.io/server/received_bytes_per_rpc", "Total bytes received across all messages per RPC.", stats.UnitBytes)
28	ServerSentMessagesPerRPC     = stats.Int64("grpc.io/server/sent_messages_per_rpc", "Number of messages sent in each RPC. Has value 1 for non-streaming RPCs.", stats.UnitDimensionless)
29	ServerSentBytesPerRPC        = stats.Int64("grpc.io/server/sent_bytes_per_rpc", "Total bytes sent in across all response messages per RPC.", stats.UnitBytes)
30	ServerLatency                = stats.Float64("grpc.io/server/server_latency", "Time between first byte of request received to last byte of response sent, or terminal error.", stats.UnitMilliseconds)
31)
32
33// TODO(acetechnologist): This is temporary and will need to be replaced by a
34// mechanism to load these defaults from a common repository/config shared by
35// all supported languages. Likely a serialized protobuf of these defaults.
36
37// Predefined views may be registered to collect data for the above measures.
38// As always, you may also define your own custom views over measures collected by this
39// package. These are declared as a convenience only; none are registered by
40// default.
41var (
42	ServerReceivedBytesPerRPCView = &view.View{
43		Name:        "grpc.io/server/received_bytes_per_rpc",
44		Description: "Distribution of received bytes per RPC, by method.",
45		Measure:     ServerReceivedBytesPerRPC,
46		TagKeys:     []tag.Key{KeyServerMethod},
47		Aggregation: DefaultBytesDistribution,
48	}
49
50	ServerSentBytesPerRPCView = &view.View{
51		Name:        "grpc.io/server/sent_bytes_per_rpc",
52		Description: "Distribution of total sent bytes per RPC, by method.",
53		Measure:     ServerSentBytesPerRPC,
54		TagKeys:     []tag.Key{KeyServerMethod},
55		Aggregation: DefaultBytesDistribution,
56	}
57
58	ServerLatencyView = &view.View{
59		Name:        "grpc.io/server/server_latency",
60		Description: "Distribution of server latency in milliseconds, by method.",
61		TagKeys:     []tag.Key{KeyServerMethod},
62		Measure:     ServerLatency,
63		Aggregation: DefaultMillisecondsDistribution,
64	}
65
66	// Purposely reuses the count from `ServerLatency`, tagging
67	// with method and status to result in ServerCompletedRpcs.
68	ServerCompletedRPCsView = &view.View{
69		Name:        "grpc.io/server/completed_rpcs",
70		Description: "Count of RPCs by method and status.",
71		TagKeys:     []tag.Key{KeyServerMethod, KeyServerStatus},
72		Measure:     ServerLatency,
73		Aggregation: view.Count(),
74	}
75
76	ServerReceivedMessagesPerRPCView = &view.View{
77		Name:        "grpc.io/server/received_messages_per_rpc",
78		Description: "Distribution of messages received count per RPC, by method.",
79		TagKeys:     []tag.Key{KeyServerMethod},
80		Measure:     ServerReceivedMessagesPerRPC,
81		Aggregation: DefaultMessageCountDistribution,
82	}
83
84	ServerSentMessagesPerRPCView = &view.View{
85		Name:        "grpc.io/server/sent_messages_per_rpc",
86		Description: "Distribution of messages sent count per RPC, by method.",
87		TagKeys:     []tag.Key{KeyServerMethod},
88		Measure:     ServerSentMessagesPerRPC,
89		Aggregation: DefaultMessageCountDistribution,
90	}
91)
92
93// DefaultServerViews are the default server views provided by this package.
94var DefaultServerViews = []*view.View{
95	ServerReceivedBytesPerRPCView,
96	ServerSentBytesPerRPCView,
97	ServerLatencyView,
98	ServerCompletedRPCsView,
99}
100