Skip to content
This repository was archived by the owner on Jun 16, 2025. It is now read-only.

Added RPC Measure and View Constants #889

Open
wants to merge 12 commits into
base: master
Choose a base branch
from
17 changes: 17 additions & 0 deletions opencensus/grpc/__init__.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,17 @@
# Copyright 2017, OpenCensus Authors
#
# Licensed under the Apache License, Version 2.0 (the "License");
# you may not use this file except in compliance with the License.
# You may obtain a copy of the License at
#
# http://www.apache.org/licenses/LICENSE-2.0
#
# Unless required by applicable law or agreed to in writing, software
# distributed under the License is distributed on an "AS IS" BASIS,
# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
# See the License for the specific language governing permissions and
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Shouldn't all of these be under opencensus/stats?

# limitations under the License.


# import rpc_measure_constants
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

What are these commented out imports for?

# import rpc_view_constants
135 changes: 135 additions & 0 deletions opencensus/grpc/rpc_m_c_test.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,135 @@
import unittest

from opencensus.grpc import rpc_measure_constants


class RPCMCTest(unittest.TestCase):
"""
RPCMTest tests the rpc_measure_constants module
"""

def setUp(self):
"""
sets up a test rpc_measure_constant instance
"""
self.rpc_measure = rpc_measure_constants.RPCMeasureConstants()

def test_client_measures(self):
"""
tests the client measures
"""
self.assertEqual(
self.rpc_measure.grpc_client_sent_messages_per_rpc.unit,
"1",
"grpc_client_sent_messages_per_rpc not set correctly on init")

self.assertEqual(
self.rpc_measure.grpc_client_sent_bytes_per_rpc.description,
"Total bytes sent across all request messages per RPC",
"grpc_client_sent_bytes_per_rpc not set correctly on init")

self.assertEqual(
self.rpc_measure.grpc_client_received_messages_per_rpc.name,
"grpc.io/client/received_messages_per_rpc",
"grpc_client_received_messages_per_rpc not set correctly on init")

self.assertEqual(
self.rpc_measure.grpc_client_received_bytes_per_rpc.unit,
"by",
"grpc_client_received_bytes_per_rpc not set correctly upon init")

self.assertEqual(
self.rpc_measure.grpc_client_roundtrip_latency.name,
"grpc.io/client/roundtrip_latency",
"grpc_client_roundtrip_latency not initialized correctly")

self.assertEqual(
self.rpc_measure.grpc_client_server_latency.name,
"grpc.io/client/server_latency",
"grpc_client_server_latency not set properly")

self.assertEqual(
self.rpc_measure.grpc_client_started_rpcs.description,
"Number of started client RPCs.",
"grpc_client_started_rpcs not set properly")

self.assertEqual(
self.rpc_measure.grpc_client_sent_messages_per_method.unit,
"1",
"grpc_client_sent_messages_per_method not set properly")

self.assertEqual(
self.rpc_measure.grpc_client_received_messages_per_method.name,
"grpc.io/client/received_messages_per_method",
"grpc_client_received_messages_per_method not set properly")

self.assertEqual(
self.rpc_measure.grpc_client_sent_bytes_per_method.description,
"Total bytes sent per method,"
" recorded real-time as bytes are sent.",
"grpc_client_sent_bytes_per_method not set properly")

self.assertEqual(
self.rpc_measure.grpc_client_received_bytes_per_method.unit,
"by",
"grpc_client_received_bytes_per_method not set properly")

def test_server_measures(self):
"""
tests the server measures
"""
self.assertEqual(
self.rpc_measure.grpc_server_received_messages_per_rpc.name,
"grpc.io/server/received_messages_per_rpc",
"grpc_server_received_messages_per_rpc not set properly")

self.assertEqual(
self.rpc_measure.grpc_server_received_bytes_per_rpc.description,
"Total bytes received across all messages per RPC",
"grpc_server_received_bytes_per_rpc not set properly")

self.assertEqual(
self.rpc_measure.grpc_server_sent_messages_per_rpc.unit,
"1",
"grpc_server_sent_messages_per_rpc not set properly")

self.assertEqual(
self.rpc_measure.grpc_server_sent_bytes_per_rpc.name,
"grpc.io/server/sent_bytes_per_rpc",
"grpc_server_sent_bytes_per_rpc not set properly")

self.assertEqual(
self.rpc_measure.grpc_server_server_latency.description,
"Time between first byte of request received to"
" last byte of response sent or terminal error.",
"grpc_server_server_latency not set properly")

self.assertEqual(
self.rpc_measure.grpc_server_started_rpcs.unit,
"1",
"grpc_server_started_rpcs not set correctly")

self.assertEqual(
self.rpc_measure.grpc_server_sent_messages_per_method.name,
"grpc.io/server/sent_messages_per_method",
"grpc_server_sent_messages_per_method not set correctly")

self.assertEqual(
self.rpc_measure.grpc_server_received_messages_per_method.
description,
"Total messages received per method.",
"grpc_server_received_messages_per_method not set correctly")

self.assertEqual(
self.rpc_measure.grpc_server_sent_bytes_per_method.unit,
"by",
"grpc_server_sent_bytes_per_method not set correctly")

self.assertEqual(
self.rpc_measure.grpc_server_received_bytes_per_method.name,
"grpc.io/server/received_bytes_per_method",
"grpc_server_received_bytes_per_method not set correctly")


if __name__ == '__main__':
unittest.main()
194 changes: 194 additions & 0 deletions opencensus/grpc/rpc_measure_constants.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,194 @@
from opencensus.stats.measure import MeasureFloat, MeasureInt
from opencensus.tags import tag_key


"""
Defines constants for collecting rpc stats
"""


class RPCMeasureConstants:
"""
Define constants used to define Measures below
see specs in documentation for opencensus-python
"""
byte = "by"
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Suggested change
byte = "by"
byte = "By"

count = "1"
millisecond = "ms"

def __init__(self):
"""
Define client and server tags
"""
# Client Tags
# gRPC server status code received,
# e.g. OK, CANCELLED, DEADLINE_EXCEEDED
self.grpc_client_status = tag_key.TagKey("grpc_client_status")

# Full gRPC method name, including package, service and method,
# e.g. google.bigtable.v2.Bigtable/CheckAndMutateRow
self.grpc_client_method = tag_key.TagKey("grpc_client_method")

# Server Tags
# gRPC server status code returned,
# e.g. OK, CANCELLED, DEADLINE_EXCEEDED
self.grpc_server_status = tag_key.TagKey("grpc_server_status")

# Full gRPC method name, including package, service and method,
# e.g. com.exampleapi.v4.BookshelfService/Checkout
self.grpc_server_method = tag_key.TagKey("grpc_server_method")

"""
Client Measures
"""
# Number of messages sent in the RPC
# (always 1 for non-streaming RPCs)
self.grpc_client_sent_messages_per_rpc = MeasureInt(
name="grpc.io/client/sent_messages_per_rpc",
description="Number of messages sent in the RPC",
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Suggested change
description="Number of messages sent in the RPC",
description="Number of messages sent in the RPC (always 1 for non-streaming RPCs)",

unit=self.count)

# Total bytes sent across all request messages per RPC
self.grpc_client_sent_bytes_per_rpc = MeasureFloat(
name="grpc.io/client/sent_bytes_per_rpc",
description="Total bytes sent across all"
" request messages per RPC",
unit=self.byte)

# Number of response messages received
# per RPC (always 1 for non-streaming RPCs)
self.grpc_client_received_messages_per_rpc = MeasureInt(
name="grpc.io/client/received_messages_per_rpc",
description="Number of response messages received per RPC",
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Suggested change
description="Number of response messages received per RPC",
description="Number of response messages received per RPC (always 1 for non-streaming RPCs)",

unit=self.count)

# Total bytes received across all
# response messages per RPC
self.grpc_client_received_bytes_per_rpc = MeasureFloat(
name="grpc.io/client/received_bytes_per_rpc",
description="Total bytes received across all"
" response messages per RPC",
unit=self.byte)

# Time between first byte of request sent to last
# byte of response received, or terminal error
self.grpc_client_roundtrip_latency = MeasureFloat(
name="grpc.io/client/roundtrip_latency",
description="Time between first byte of request sent to"
" last byte of response received or terminal error.",
unit=self.millisecond)

# Propagated from the server and should
# have the same value as "grpc.io/server/latency"
self.grpc_client_server_latency = MeasureFloat(
name="grpc.io/client/server_latency",
description="Server latency in msecs",
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Suggested change
description="Server latency in msecs",
description="Propagated from the server and should have the same value as "grpc.io/server/latency",

unit=self.millisecond)

# The total number of client RPCs ever opened,
# including those that have not completed
self.grpc_client_started_rpcs = MeasureInt(
name="grpc.io/client/started_rpcs",
description="Number of started client RPCs.",
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Please use the exact descriptions that are listed in the specs.

unit=self.count)

# Total messages sent per method
self.grpc_client_sent_messages_per_method = MeasureInt(
name="grpc.io/client/sent_messages_per_method",
description="Total messages sent per method.",
unit=self.count)

# Total messages received per method
self.grpc_client_received_messages_per_method = MeasureInt(
name="grpc.io/client/received_messages_per_method",
description="Total messages received per method.",
unit=self.count)

# Total bytes sent per method,
# recorded real-time as bytes are sent
self.grpc_client_sent_bytes_per_method = MeasureFloat(
name="grpc.io/client/sent_bytes_per_method",
description="Total bytes sent per method, recorded"
" real-time as bytes are sent.",
unit=self.byte)

# Total bytes received per method, recorded real-time
# as bytes are received
self.grpc_client_received_bytes_per_method = MeasureFloat(
name="grpc.io/client/received_bytes_per_method",
description="Total bytes received per method,"
" recorded real-time as bytes are received.",
unit=self.byte)

"""
Server Measures
"""
# Number of messages received in each RPC.
# Has value 1 for non-streaming RPCs
self.grpc_server_received_messages_per_rpc = MeasureInt(
name="grpc.io/server/received_messages_per_rpc",
description="Number of messages received in each RPC",
unit=self.count)

# Total bytes received across all messages per RPC
self.grpc_server_received_bytes_per_rpc = MeasureFloat(
name="grpc.io/server/received_bytes_per_rpc",
description="Total bytes received across all"
" messages per RPC",
unit=self.byte)

# Number of messages sent in each RPC.
# Has value 1 for non-streaming RPCs
self.grpc_server_sent_messages_per_rpc = MeasureInt(
name="grpc.io/server/sent_messages_per_rpc",
description="Number of messages sent in each RPC",
unit=self.count)

# Total bytes sent in across all response messages per RPC
self.grpc_server_sent_bytes_per_rpc = MeasureFloat(
name="grpc.io/server/sent_bytes_per_rpc",
description="Total bytes sent across all response"
" messages per RPC",
unit=self.byte)

# Time between first byte of request received to last byte of
# response sent, or terminal error
self.grpc_server_server_latency = MeasureFloat(
name="grpc.io/server/server_latency",
description="Time between first byte of request received"
" to last byte of response sent or terminal error.",
unit=self.millisecond)

# The total number of server RPCs ever opened,
# including those that have not completed
self.grpc_server_started_rpcs = MeasureInt(
name="grpc.io/server/started_rpcs",
description="Number of started server RPCs.",
unit=self.count)

# Total messages sent per method
self.grpc_server_sent_messages_per_method = MeasureInt(
name="grpc.io/server/sent_messages_per_method",
description="Total messages sent per method.",
unit=self.count)

# Total messages received per method
self.grpc_server_received_messages_per_method = MeasureInt(
name="grpc.io/server/received_messages_per_method",
description="Total messages received per method.",
unit=self.count)

# Total bytes sent per method, recorded real-time as bytes are sent
self.grpc_server_sent_bytes_per_method = MeasureFloat(
name="grpc.io/server/sent_bytes_per_method",
description="Total bytes sent per method, recorded"
" real-time as bytes are sent.",
unit=self.byte)

# Total bytes received per method, recorded real-time as
# bytes are received
self.grpc_server_received_bytes_per_method = MeasureFloat(
name="grpc.io/server/received_bytes_per_method",
description="Total bytes received per method, recorded"
" real-time as bytes are received.",
unit=self.byte)
Loading