diff --git a/ext/opentelemetry-ext-grpc/src/opentelemetry/ext/grpc/__init__.py b/ext/opentelemetry-ext-grpc/src/opentelemetry/ext/grpc/__init__.py index 66b09860641..fa20d00db62 100644 --- a/ext/opentelemetry-ext-grpc/src/opentelemetry/ext/grpc/__init__.py +++ b/ext/opentelemetry-ext-grpc/src/opentelemetry/ext/grpc/__init__.py @@ -21,7 +21,7 @@ from opentelemetry.ext.grpc.version import __version__ -def client_interceptor(tracer_provider=None, meter=None): +def client_interceptor(tracer_provider=None, exporter=None, interval=30): """Create a gRPC client channel interceptor. Args: @@ -34,7 +34,7 @@ def client_interceptor(tracer_provider=None, meter=None): tracer = trace.get_tracer(__name__, __version__, tracer_provider) - return _client.OpenTelemetryClientInterceptor(tracer, meter) + return _client.OpenTelemetryClientInterceptor(tracer, exporter, interval) def server_interceptor(tracer_provider=None): diff --git a/ext/opentelemetry-ext-grpc/src/opentelemetry/ext/grpc/_client.py b/ext/opentelemetry-ext-grpc/src/opentelemetry/ext/grpc/_client.py index 3e460810630..9e6069b0f95 100644 --- a/ext/opentelemetry-ext-grpc/src/opentelemetry/ext/grpc/_client.py +++ b/ext/opentelemetry-ext-grpc/src/opentelemetry/ext/grpc/_client.py @@ -24,7 +24,8 @@ import grpc -from opentelemetry import propagators, trace +from opentelemetry import metrics, propagators, trace +from opentelemetry.sdk.metrics.export.controller import PushController from opentelemetry.trace.status import Status, StatusCanonicalCode from . import grpcext @@ -83,9 +84,12 @@ def callback(response_future): class OpenTelemetryClientInterceptor( grpcext.UnaryClientInterceptor, grpcext.StreamClientInterceptor ): - def __init__(self, tracer, meter): + def __init__(self, tracer, exporter, interval): self._tracer = tracer - self._meter = meter + self._meter = metrics.get_meter(__name__) + self.controller = PushController( + meter=self._meter, exporter=exporter, interval=interval + ) self._metrics_recorder = TimedMetricRecorder(self._meter, "client") def _start_span(self, method): diff --git a/ext/opentelemetry-ext-grpc/tests/test_client_interceptor.py b/ext/opentelemetry-ext-grpc/tests/test_client_interceptor.py index 39f12385c58..a5b24f3873b 100644 --- a/ext/opentelemetry-ext-grpc/tests/test_client_interceptor.py +++ b/ext/opentelemetry-ext-grpc/tests/test_client_interceptor.py @@ -15,14 +15,13 @@ import grpc import opentelemetry.ext.grpc -from opentelemetry import metrics, trace +from opentelemetry import trace from opentelemetry.ext.grpc import client_interceptor from opentelemetry.ext.grpc.grpcext import intercept_channel from opentelemetry.sdk.metrics.export.aggregate import ( MinMaxSumCountAggregator, SumAggregator, ) -from opentelemetry.sdk.metrics.export.controller import PushController from opentelemetry.test.test_base import TestBase from tests.protobuf import test_server_pb2_grpc @@ -40,24 +39,21 @@ def setUp(self): super().setUp() self.server = create_test_server(25565) self.server.start() - meter = metrics.get_meter(__name__) - interceptor = client_interceptor(meter=meter) + self.interceptor = client_interceptor( + exporter=self.memory_metrics_exporter + ) self.channel = intercept_channel( - grpc.insecure_channel("localhost:25565"), interceptor + grpc.insecure_channel("localhost:25565"), self.interceptor ) self._stub = test_server_pb2_grpc.GRPCTestServerStub(self.channel) - self._controller = PushController( - meter, self.memory_metrics_exporter, 30 - ) - def tearDown(self): super().tearDown() self.memory_metrics_exporter.clear() self.server.stop(None) def _verify_success_records(self, num_bytes_out, num_bytes_in, method): - self._controller.tick() + self.interceptor.controller.tick() records = self.memory_metrics_exporter.get_exported_metrics() self.assertEqual(len(records), 3) @@ -167,7 +163,7 @@ def test_stream_stream(self): ) def _verify_error_records(self, method): - self._controller.tick() + self.interceptor.controller.tick() records = self.memory_metrics_exporter.get_exported_metrics() self.assertEqual(len(records), 3)