Skip to content
This repository was archived by the owner on Jul 31, 2023. It is now read-only.

Commit 9a3f609

Browse files
author
Ian Sturdy
authored
Update the gRPC stats based on the new spec. (#138)
1 parent 11f1a46 commit 9a3f609

15 files changed

Lines changed: 476 additions & 666 deletions

examples/grpc/hello_server.cc

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -108,8 +108,8 @@ int main(int argc, char** argv) {
108108
// Register the OpenCensus gRPC plugin to enable stats and tracing in gRPC.
109109
opencensus::RegisterGrpcPlugin();
110110

111-
// Register the experimental gRPC views (latency, error count, etc).
112-
opencensus::ExperimentalRegisterGrpcViewsForExport();
111+
// Register the gRPC views (latency, error count, etc).
112+
opencensus::RegisterGrpcViewsForExport();
113113

114114
// Register exporters for Stackdriver.
115115
RegisterStackdriverExporters();

opencensus/plugins/grpc/grpc_plugin.h

Lines changed: 61 additions & 75 deletions
Original file line numberDiff line numberDiff line change
@@ -30,99 +30,85 @@ namespace opencensus {
3030
// defined below.
3131
void RegisterGrpcPlugin();
3232

33+
// RPC stats definitions, defined by
34+
// https://github.com/census-instrumentation/opencensus-specs/blob/master/stats/gRPC.md
35+
3336
// Registers the cumulative gRPC views so that they will be exported by any
3437
// registered stats exporter.
3538
// For on-task stats, construct a View using the ViewDescriptors below.
36-
// Experimental: These view definitions are subject to change.
37-
void ExperimentalRegisterGrpcViewsForExport();
39+
void RegisterGrpcViewsForExport();
3840

3941
// Returns the tracing Span for the current RPC.
4042
opencensus::trace::Span GetSpanFromServerContext(grpc::ServerContext* context);
4143

42-
// The tag key for the RPC method and status, set for all values recorded for
43-
// the following measures.
44-
opencensus::stats::TagKey MethodTagKey();
45-
opencensus::stats::TagKey StatusTagKey();
44+
// The tag keys set when recording RPC stats.
45+
opencensus::stats::TagKey ClientMethodTagKey();
46+
opencensus::stats::TagKey ClientStatusTagKey();
47+
opencensus::stats::TagKey ServerMethodTagKey();
48+
opencensus::stats::TagKey ServerStatusTagKey();
4649

4750
// Names of measures used by the plugin--users can create views on these
4851
// measures but should not record data for them.
49-
extern const absl::string_view kRpcClientErrorCountMeasureName;
50-
extern const absl::string_view kRpcClientRequestBytesMeasureName;
51-
extern const absl::string_view kRpcClientResponseBytesMeasureName;
52+
extern const absl::string_view kRpcClientSentMessagesPerRpcMeasureName;
53+
extern const absl::string_view kRpcClientSentBytesPerRpcMeasureName;
54+
extern const absl::string_view kRpcClientReceivedMessagesPerRpcMeasureName;
55+
extern const absl::string_view kRpcClientReceivedBytesPerRpcMeasureName;
5256
extern const absl::string_view kRpcClientRoundtripLatencyMeasureName;
53-
extern const absl::string_view kRpcClientServerElapsedTimeMeasureName;
54-
extern const absl::string_view kRpcClientStartedCountMeasureName;
55-
extern const absl::string_view kRpcClientFinishedCountMeasureName;
56-
extern const absl::string_view kRpcClientRequestCountMeasureName;
57-
extern const absl::string_view kRpcClientResponseCountMeasureName;
58-
59-
extern const absl::string_view kRpcServerErrorCountMeasureName;
60-
extern const absl::string_view kRpcServerRequestBytesMeasureName;
61-
extern const absl::string_view kRpcServerResponseBytesMeasureName;
62-
extern const absl::string_view kRpcServerServerElapsedTimeMeasureName;
63-
extern const absl::string_view kRpcServerStartedCountMeasureName;
64-
extern const absl::string_view kRpcServerFinishedCountMeasureName;
65-
extern const absl::string_view kRpcServerRequestCountMeasureName;
66-
extern const absl::string_view kRpcServerResponseCountMeasureName;
57+
extern const absl::string_view kRpcClientServerLatencyMeasureName;
58+
59+
extern const absl::string_view kRpcServerSentMessagesPerRpcMeasureName;
60+
extern const absl::string_view kRpcServerSentBytesPerRpcMeasureName;
61+
extern const absl::string_view kRpcServerReceivedMessagesPerRpcMeasureName;
62+
extern const absl::string_view kRpcServerReceivedBytesPerRpcMeasureName;
63+
extern const absl::string_view kRpcServerServerLatencyMeasureName;
6764

6865
// Canonical gRPC view definitions.
69-
// These view definitions are subject to change.
70-
const stats::ViewDescriptor& ClientErrorCountCumulative();
71-
const stats::ViewDescriptor& ClientRequestBytesCumulative();
72-
const stats::ViewDescriptor& ClientResponseBytesCumulative();
66+
const stats::ViewDescriptor& ClientSentMessagesPerRpcCumulative();
67+
const stats::ViewDescriptor& ClientSentBytesPerRpcCumulative();
68+
const stats::ViewDescriptor& ClientReceivedMessagesPerRpcCumulative();
69+
const stats::ViewDescriptor& ClientReceivedBytesPerRpcCumulative();
7370
const stats::ViewDescriptor& ClientRoundtripLatencyCumulative();
74-
const stats::ViewDescriptor& ClientServerElapsedTimeCumulative();
75-
const stats::ViewDescriptor& ClientStartedCountCumulative();
76-
const stats::ViewDescriptor& ClientFinishedCountCumulative();
77-
const stats::ViewDescriptor& ClientRequestCountCumulative();
78-
const stats::ViewDescriptor& ClientResponseCountCumulative();
79-
80-
const stats::ViewDescriptor& ServerErrorCountCumulative();
81-
const stats::ViewDescriptor& ServerRequestBytesCumulative();
82-
const stats::ViewDescriptor& ServerResponseBytesCumulative();
83-
const stats::ViewDescriptor& ServerServerElapsedTimeCumulative();
84-
const stats::ViewDescriptor& ServerStartedCountCumulative();
85-
const stats::ViewDescriptor& ServerFinishedCountCumulative();
86-
const stats::ViewDescriptor& ServerRequestCountCumulative();
87-
const stats::ViewDescriptor& ServerResponseCountCumulative();
71+
const stats::ViewDescriptor& ClientServerLatencyCumulative();
72+
const stats::ViewDescriptor& ClientCompletedRpcsCumulative();
8873

89-
const stats::ViewDescriptor& ClientErrorCountMinute();
90-
const stats::ViewDescriptor& ClientRequestBytesMinute();
91-
const stats::ViewDescriptor& ClientResponseBytesMinute();
74+
const stats::ViewDescriptor& ServerSentBytesPerRpcCumulative();
75+
const stats::ViewDescriptor& ServerReceivedBytesPerRpcCumulative();
76+
const stats::ViewDescriptor& ServerServerLatencyCumulative();
77+
const stats::ViewDescriptor& ServerStartedCountCumulative();
78+
const stats::ViewDescriptor& ServerCompletedRpcsCumulative();
79+
const stats::ViewDescriptor& ServerSentMessagesPerRpcCumulative();
80+
const stats::ViewDescriptor& ServerReceivedMessagesPerRpcCumulative();
81+
82+
const stats::ViewDescriptor& ClientSentMessagesPerRpcMinute();
83+
const stats::ViewDescriptor& ClientSentBytesPerRpcMinute();
84+
const stats::ViewDescriptor& ClientReceivedMessagesPerRpcMinute();
85+
const stats::ViewDescriptor& ClientReceivedBytesPerRpcMinute();
9286
const stats::ViewDescriptor& ClientRoundtripLatencyMinute();
93-
const stats::ViewDescriptor& ClientServerElapsedTimeMinute();
94-
const stats::ViewDescriptor& ClientStartedCountMinute();
95-
const stats::ViewDescriptor& ClientFinishedCountMinute();
96-
const stats::ViewDescriptor& ClientRequestCountMinute();
97-
const stats::ViewDescriptor& ClientResponseCountMinute();
98-
99-
const stats::ViewDescriptor& ServerErrorCountMinute();
100-
const stats::ViewDescriptor& ServerRequestBytesMinute();
101-
const stats::ViewDescriptor& ServerResponseBytesMinute();
102-
const stats::ViewDescriptor& ServerServerElapsedTimeMinute();
103-
const stats::ViewDescriptor& ServerStartedCountMinute();
104-
const stats::ViewDescriptor& ServerFinishedCountMinute();
105-
const stats::ViewDescriptor& ServerRequestCountMinute();
106-
const stats::ViewDescriptor& ServerResponseCountMinute();
107-
108-
const stats::ViewDescriptor& ClientErrorCountHour();
109-
const stats::ViewDescriptor& ClientRequestBytesHour();
110-
const stats::ViewDescriptor& ClientResponseBytesHour();
87+
const stats::ViewDescriptor& ClientServerLatencyMinute();
88+
const stats::ViewDescriptor& ClientCompletedRpcsMinute();
89+
90+
const stats::ViewDescriptor& ServerSentMessagesPerRpcMinute();
91+
const stats::ViewDescriptor& ServerSentBytesPerRpcMinute();
92+
const stats::ViewDescriptor& ServerReceivedMessagesPerRpcMinute();
93+
const stats::ViewDescriptor& ServerReceivedBytesPerRpcMinute();
94+
const stats::ViewDescriptor& ServerServerLatencyMinute();
95+
const stats::ViewDescriptor& ServerCompletedRpcsMinute();
96+
97+
const stats::ViewDescriptor& ClientSentMessagesPerRpcHour();
98+
const stats::ViewDescriptor& ClientSentBytesPerRpcHour();
99+
const stats::ViewDescriptor& ClientReceivedMessagesPerRpcHour();
100+
const stats::ViewDescriptor& ClientReceivedBytesPerRpcHour();
111101
const stats::ViewDescriptor& ClientRoundtripLatencyHour();
112-
const stats::ViewDescriptor& ClientServerElapsedTimeHour();
113-
const stats::ViewDescriptor& ClientStartedCountHour();
114-
const stats::ViewDescriptor& ClientFinishedCountHour();
115-
const stats::ViewDescriptor& ClientRequestCountHour();
116-
const stats::ViewDescriptor& ClientResponseCountHour();
117-
118-
const stats::ViewDescriptor& ServerErrorCountHour();
119-
const stats::ViewDescriptor& ServerRequestBytesHour();
120-
const stats::ViewDescriptor& ServerResponseBytesHour();
121-
const stats::ViewDescriptor& ServerServerElapsedTimeHour();
102+
const stats::ViewDescriptor& ClientServerLatencyHour();
103+
const stats::ViewDescriptor& ClientCompletedRpcsHour();
104+
105+
const stats::ViewDescriptor& ServerSentMessagesPerRpcHour();
106+
const stats::ViewDescriptor& ServerSentBytesPerRpcHour();
107+
const stats::ViewDescriptor& ServerReceivedMessagesPerRpcHour();
108+
const stats::ViewDescriptor& ServerReceivedBytesPerRpcHour();
109+
const stats::ViewDescriptor& ServerServerLatencyHour();
122110
const stats::ViewDescriptor& ServerStartedCountHour();
123-
const stats::ViewDescriptor& ServerFinishedCountHour();
124-
const stats::ViewDescriptor& ServerRequestCountHour();
125-
const stats::ViewDescriptor& ServerResponseCountHour();
111+
const stats::ViewDescriptor& ServerCompletedRpcsHour();
126112

127113
} // namespace opencensus
128114

opencensus/plugins/grpc/internal/client_filter.cc

Lines changed: 9 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -137,14 +137,13 @@ grpc_error *CensusClientCallData::Init(grpc_call_element *elem,
137137
const grpc_call_element_args *args) {
138138
path_ = grpc_slice_ref_internal(args->path);
139139
start_time_ = absl::Now();
140-
qualified_method_ = StrCat("Sent.", GetMethod(&path_));
140+
method_ = GetMethod(&path_);
141+
qualified_method_ = absl::StrCat("Sent.", method_);
141142
GRPC_CLOSURE_INIT(&on_done_recv_message_, OnDoneRecvMessageCb, elem,
142143
grpc_schedule_on_exec_ctx);
143144
GRPC_CLOSURE_INIT(&on_done_recv_trailing_metadata_,
144145
OnDoneRecvTrailingMetadataCb, elem,
145146
grpc_schedule_on_exec_ctx);
146-
stats::Record({{RpcClientStartedCount(), 1}},
147-
{{MethodTagKey(), qualified_method_}});
148147
return GRPC_ERROR_NONE;
149148
}
150149

@@ -155,18 +154,15 @@ void CensusClientCallData::Destroy(grpc_call_element *elem,
155154
const uint64_t response_size = GetIncomingDataSize(final_info);
156155
double latency_ms = absl::ToDoubleMilliseconds(absl::Now() - start_time_);
157156
stats::Record(
158-
{{RpcClientErrorCount(),
159-
final_info->final_status == GRPC_STATUS_OK ? 0 : 1},
160-
{RpcClientRequestBytes(), static_cast<double>(request_size)},
161-
{RpcClientResponseBytes(), static_cast<double>(response_size)},
157+
{{RpcClientSentBytesPerRpc(), static_cast<double>(request_size)},
158+
{RpcClientReceivedBytesPerRpc(), static_cast<double>(response_size)},
162159
{RpcClientRoundtripLatency(), latency_ms},
163-
{RpcClientServerElapsedTime(),
160+
{RpcClientServerLatency(),
164161
static_cast<double>(elapsed_time_) * kNumMillisPerNanosecond},
165-
{RpcClientFinishedCount(), 1},
166-
{RpcClientRequestCount(), sent_message_count_},
167-
{RpcClientResponseCount(), recv_message_count_}},
168-
{{MethodTagKey(), qualified_method_},
169-
{StatusTagKey(), StatusCodeToString(final_info->final_status)}});
162+
{RpcClientSentMessagesPerRpc(), sent_message_count_},
163+
{RpcClientReceivedMessagesPerRpc(), recv_message_count_}},
164+
{{ClientMethodTagKey(), method_},
165+
{ClientStatusTagKey(), StatusCodeToString(final_info->final_status)}});
170166
grpc_slice_unref_internal(path_);
171167
context_.EndSpan();
172168
}

opencensus/plugins/grpc/internal/client_filter.h

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -67,6 +67,7 @@ class CensusClientCallData : public grpc::CallData {
6767
grpc_linked_mdelem stats_bin_;
6868
grpc_linked_mdelem tracing_bin_;
6969
// Client method.
70+
absl::string_view method_;
7071
std::string qualified_method_;
7172
grpc_slice path_;
7273
// The recv trailing metadata callbacks.

opencensus/plugins/grpc/internal/filter.cc

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -39,15 +39,15 @@ void GenerateClientContext(absl::string_view method, CensusContext *ctxt,
3939
new (ctxt) CensusContext(method);
4040
}
4141

42-
size_t ServerStatsSerialize(uint64_t server_elapsed_time, char *buf,
42+
size_t ServerStatsSerialize(uint64_t server_latency, char *buf,
4343
size_t buf_size) {
44-
return RpcServerStatsEncoding::Encode(server_elapsed_time, buf, buf_size);
44+
return RpcServerStatsEncoding::Encode(server_latency, buf, buf_size);
4545
}
4646

4747
size_t ServerStatsDeserialize(const char *buf, size_t buf_size,
48-
uint64_t *server_elapsed_time) {
48+
uint64_t *server_latency) {
4949
return RpcServerStatsEncoding::Decode(absl::string_view(buf, buf_size),
50-
server_elapsed_time);
50+
server_latency);
5151
}
5252

5353
uint64_t GetIncomingDataSize(const grpc_call_final_info *final_info) {

opencensus/plugins/grpc/internal/filter.h

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -69,12 +69,12 @@ class CensusContext {
6969
};
7070

7171
// Serialize outgoing server stats. Returns the number of bytes serialized.
72-
size_t ServerStatsSerialize(uint64_t server_elapsed_time, char *buf,
72+
size_t ServerStatsSerialize(uint64_t server_latency, char *buf,
7373
size_t buf_size);
7474

7575
// Deserialize incoming server stats. Returns the number of bytes deserialized.
7676
size_t ServerStatsDeserialize(const char *buf, size_t buf_size,
77-
uint64_t *server_elapsed_time);
77+
uint64_t *server_latency);
7878

7979
// Deserialize the incoming SpanContext and generate a new server context based
8080
// on that. This new span will never be a root span. This should only be called

0 commit comments

Comments
 (0)