1 /*
2  * Copyright 2023 The gRPC Authors
3  *
4  * Licensed under the Apache License, Version 2.0 (the "License");
5  * you may not use this file except in compliance with the License.
6  * You may obtain a copy of the License at
7  *
8  *     http://www.apache.org/licenses/LICENSE-2.0
9  *
10  * Unless required by applicable law or agreed to in writing, software
11  * distributed under the License is distributed on an "AS IS" BASIS,
12  * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13  * See the License for the specific language governing permissions and
14  * limitations under the License.
15  */
16 
17 package io.grpc.census.internal;
18 
19 import static io.opencensus.contrib.grpc.metrics.RpcMeasureConstants.GRPC_CLIENT_METHOD;
20 import static io.opencensus.contrib.grpc.metrics.RpcMeasureConstants.GRPC_CLIENT_RECEIVED_BYTES_PER_RPC;
21 import static io.opencensus.contrib.grpc.metrics.RpcMeasureConstants.GRPC_CLIENT_SENT_BYTES_PER_RPC;
22 import static io.opencensus.contrib.grpc.metrics.RpcMeasureConstants.GRPC_CLIENT_STATUS;
23 import static io.opencensus.contrib.grpc.metrics.RpcMeasureConstants.GRPC_SERVER_METHOD;
24 import static io.opencensus.contrib.grpc.metrics.RpcMeasureConstants.GRPC_SERVER_RECEIVED_BYTES_PER_RPC;
25 import static io.opencensus.contrib.grpc.metrics.RpcMeasureConstants.GRPC_SERVER_SENT_BYTES_PER_RPC;
26 import static io.opencensus.contrib.grpc.metrics.RpcMeasureConstants.GRPC_SERVER_STATUS;
27 
28 import com.google.common.annotations.VisibleForTesting;
29 import io.grpc.CallOptions;
30 import io.opencensus.contrib.grpc.metrics.RpcViewConstants;
31 import io.opencensus.stats.Aggregation;
32 import io.opencensus.stats.Measure;
33 import io.opencensus.stats.Measure.MeasureDouble;
34 import io.opencensus.stats.View;
35 import io.opencensus.trace.SpanContext;
36 import java.util.Arrays;
37 
38 // TODO(dnvindhya): Remove metric and view definitions from this class once it is moved to
39 // OpenCensus library.
40 /**
41  * Temporary holder class for the observability specific OpenCensus constants. The class will be
42  * removed once the new views are added in OpenCensus library.
43  */
44 @VisibleForTesting
45 public final class ObservabilityCensusConstants {
46 
47   public static CallOptions.Key<SpanContext> CLIENT_TRACE_SPAN_CONTEXT_KEY
48       = CallOptions.Key.createWithDefault("Client span context for tracing", SpanContext.INVALID);
49 
50   static final Aggregation AGGREGATION_WITH_BYTES_HISTOGRAM =
51       RpcViewConstants.GRPC_CLIENT_SENT_BYTES_PER_RPC_VIEW.getAggregation();
52 
53   static final Aggregation AGGREGATION_WITH_MILLIS_HISTOGRAM =
54       RpcViewConstants.GRPC_CLIENT_ROUNDTRIP_LATENCY_VIEW.getAggregation();
55 
56   public static final MeasureDouble API_LATENCY_PER_CALL =
57       Measure.MeasureDouble.create(
58           "grpc.io/client/api_latency",
59           "Time taken by gRPC to complete an RPC from application's perspective",
60           "ms");
61 
62   public static final View GRPC_CLIENT_API_LATENCY_VIEW =
63       View.create(
64           View.Name.create("grpc.io/client/api_latency"),
65           "Time taken by gRPC to complete an RPC from application's perspective",
66           API_LATENCY_PER_CALL,
67           AGGREGATION_WITH_MILLIS_HISTOGRAM,
68           Arrays.asList(GRPC_CLIENT_METHOD, GRPC_CLIENT_STATUS));
69 
70   public static final View GRPC_CLIENT_SENT_COMPRESSED_MESSAGE_BYTES_PER_RPC_VIEW =
71       View.create(
72           View.Name.create("grpc.io/client/sent_compressed_message_bytes_per_rpc"),
73           "Compressed message bytes sent per client RPC attempt",
74           GRPC_CLIENT_SENT_BYTES_PER_RPC,
75           AGGREGATION_WITH_BYTES_HISTOGRAM,
76           Arrays.asList(GRPC_CLIENT_METHOD, GRPC_CLIENT_STATUS));
77 
78   public static final View GRPC_CLIENT_RECEIVED_COMPRESSED_MESSAGE_BYTES_PER_RPC_VIEW =
79       View.create(
80           View.Name.create("grpc.io/client/received_compressed_message_bytes_per_rpc"),
81           "Compressed message bytes received per client RPC attempt",
82           GRPC_CLIENT_RECEIVED_BYTES_PER_RPC,
83           AGGREGATION_WITH_BYTES_HISTOGRAM,
84           Arrays.asList(GRPC_CLIENT_METHOD, GRPC_CLIENT_STATUS));
85 
86   public static final View GRPC_SERVER_SENT_COMPRESSED_MESSAGE_BYTES_PER_RPC_VIEW =
87       View.create(
88           View.Name.create("grpc.io/server/sent_compressed_message_bytes_per_rpc"),
89           "Compressed message bytes sent per server RPC",
90           GRPC_SERVER_SENT_BYTES_PER_RPC,
91           AGGREGATION_WITH_BYTES_HISTOGRAM,
92           Arrays.asList(GRPC_SERVER_METHOD, GRPC_SERVER_STATUS));
93 
94   public static final View GRPC_SERVER_RECEIVED_COMPRESSED_MESSAGE_BYTES_PER_RPC_VIEW =
95       View.create(
96           View.Name.create("grpc.io/server/received_compressed_message_bytes_per_rpc"),
97           "Compressed message bytes received per server RPC",
98           GRPC_SERVER_RECEIVED_BYTES_PER_RPC,
99           AGGREGATION_WITH_BYTES_HISTOGRAM,
100           Arrays.asList(GRPC_SERVER_METHOD, GRPC_SERVER_STATUS));
101 
ObservabilityCensusConstants()102   private ObservabilityCensusConstants() {}
103 }
104