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 ClientHandler:
25var (
26	ClientSentMessagesPerRPC     = stats.Int64("grpc.io/client/sent_messages_per_rpc", "Number of messages sent in the RPC (always 1 for non-streaming RPCs).", stats.UnitDimensionless)
27	ClientSentBytesPerRPC        = stats.Int64("grpc.io/client/sent_bytes_per_rpc", "Total bytes sent across all request messages per RPC.", stats.UnitBytes)
28	ClientReceivedMessagesPerRPC = stats.Int64("grpc.io/client/received_messages_per_rpc", "Number of response messages received per RPC (always 1 for non-streaming RPCs).", stats.UnitDimensionless)
29	ClientReceivedBytesPerRPC    = stats.Int64("grpc.io/client/received_bytes_per_rpc", "Total bytes received across all response messages per RPC.", stats.UnitBytes)
30	ClientRoundtripLatency       = stats.Float64("grpc.io/client/roundtrip_latency", "Time between first byte of request sent to last byte of response received, or terminal error.", stats.UnitMilliseconds)
31	ClientServerLatency          = stats.Float64("grpc.io/client/server_latency", `Propagated from the server and should have the same value as "grpc.io/server/latency".`, stats.UnitMilliseconds)
32)
33
34// Predefined views may be registered to collect data for the above measures.
35// As always, you may also define your own custom views over measures collected by this
36// package. These are declared as a convenience only; none are registered by
37// default.
38var (
39	ClientSentBytesPerRPCView = &view.View{
40		Measure:     ClientSentBytesPerRPC,
41		Name:        "grpc.io/client/sent_bytes_per_rpc",
42		Description: "Distribution of bytes sent per RPC, by method.",
43		TagKeys:     []tag.Key{KeyClientMethod},
44		Aggregation: DefaultBytesDistribution,
45	}
46
47	ClientReceivedBytesPerRPCView = &view.View{
48		Measure:     ClientReceivedBytesPerRPC,
49		Name:        "grpc.io/client/received_bytes_per_rpc",
50		Description: "Distribution of bytes received per RPC, by method.",
51		TagKeys:     []tag.Key{KeyClientMethod},
52		Aggregation: DefaultBytesDistribution,
53	}
54
55	ClientRoundtripLatencyView = &view.View{
56		Measure:     ClientRoundtripLatency,
57		Name:        "grpc.io/client/roundtrip_latency",
58		Description: "Distribution of round-trip latency, by method.",
59		TagKeys:     []tag.Key{KeyClientMethod},
60		Aggregation: DefaultMillisecondsDistribution,
61	}
62
63	ClientCompletedRPCsView = &view.View{
64		Measure:     ClientRoundtripLatency,
65		Name:        "grpc.io/client/completed_rpcs",
66		Description: "Count of RPCs by method and status.",
67		TagKeys:     []tag.Key{KeyClientMethod, KeyClientStatus},
68		Aggregation: view.Count(),
69	}
70
71	ClientSentMessagesPerRPCView = &view.View{
72		Measure:     ClientSentMessagesPerRPC,
73		Name:        "grpc.io/client/sent_messages_per_rpc",
74		Description: "Distribution of sent messages count per RPC, by method.",
75		TagKeys:     []tag.Key{KeyClientMethod},
76		Aggregation: DefaultMessageCountDistribution,
77	}
78
79	ClientReceivedMessagesPerRPCView = &view.View{
80		Measure:     ClientReceivedMessagesPerRPC,
81		Name:        "grpc.io/client/received_messages_per_rpc",
82		Description: "Distribution of received messages count per RPC, by method.",
83		TagKeys:     []tag.Key{KeyClientMethod},
84		Aggregation: DefaultMessageCountDistribution,
85	}
86
87	ClientServerLatencyView = &view.View{
88		Measure:     ClientServerLatency,
89		Name:        "grpc.io/client/server_latency",
90		Description: "Distribution of server latency as viewed by client, by method.",
91		TagKeys:     []tag.Key{KeyClientMethod},
92		Aggregation: DefaultMillisecondsDistribution,
93	}
94)
95
96// DefaultClientViews are the default client views provided by this package.
97var DefaultClientViews = []*view.View{
98	ClientSentBytesPerRPCView,
99	ClientReceivedBytesPerRPCView,
100	ClientRoundtripLatencyView,
101	ClientCompletedRPCsView,
102}
103
104// TODO(jbd): Add roundtrip_latency, uncompressed_request_bytes, uncompressed_response_bytes, request_count, response_count.
105// TODO(acetechnologist): This is temporary and will need to be replaced by a
106// mechanism to load these defaults from a common repository/config shared by
107// all supported languages. Likely a serialized protobuf of these defaults.
108