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	// Purposely reuses the count from `ClientRoundtripLatency`, tagging
64	// with method and status to result in ClientCompletedRpcs.
65	ClientCompletedRPCsView = &view.View{
66		Measure:     ClientRoundtripLatency,
67		Name:        "grpc.io/client/completed_rpcs",
68		Description: "Count of RPCs by method and status.",
69		TagKeys:     []tag.Key{KeyClientMethod, KeyClientStatus},
70		Aggregation: view.Count(),
71	}
72
73	ClientSentMessagesPerRPCView = &view.View{
74		Measure:     ClientSentMessagesPerRPC,
75		Name:        "grpc.io/client/sent_messages_per_rpc",
76		Description: "Distribution of sent messages count per RPC, by method.",
77		TagKeys:     []tag.Key{KeyClientMethod},
78		Aggregation: DefaultMessageCountDistribution,
79	}
80
81	ClientReceivedMessagesPerRPCView = &view.View{
82		Measure:     ClientReceivedMessagesPerRPC,
83		Name:        "grpc.io/client/received_messages_per_rpc",
84		Description: "Distribution of received messages count per RPC, by method.",
85		TagKeys:     []tag.Key{KeyClientMethod},
86		Aggregation: DefaultMessageCountDistribution,
87	}
88
89	ClientServerLatencyView = &view.View{
90		Measure:     ClientServerLatency,
91		Name:        "grpc.io/client/server_latency",
92		Description: "Distribution of server latency as viewed by client, by method.",
93		TagKeys:     []tag.Key{KeyClientMethod},
94		Aggregation: DefaultMillisecondsDistribution,
95	}
96)
97
98// DefaultClientViews are the default client views provided by this package.
99var DefaultClientViews = []*view.View{
100	ClientSentBytesPerRPCView,
101	ClientReceivedBytesPerRPCView,
102	ClientRoundtripLatencyView,
103	ClientCompletedRPCsView,
104}
105
106// TODO(jbd): Add roundtrip_latency, uncompressed_request_bytes, uncompressed_response_bytes, request_count, response_count.
107// TODO(acetechnologist): This is temporary and will need to be replaced by a
108// mechanism to load these defaults from a common repository/config shared by
109// all supported languages. Likely a serialized protobuf of these defaults.
110