Skip to content

Commit

Permalink
added meter instrumentation
Browse files Browse the repository at this point in the history
  • Loading branch information
Corvin Lasogga authored and CoLa5 committed Aug 14, 2022
1 parent de325c0 commit 114732d
Show file tree
Hide file tree
Showing 2 changed files with 239 additions and 83 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -67,8 +67,13 @@ def run():
import grpc
from opentelemetry import trace
from opentelemetry import metrics, trace
from opentelemetry.instrumentation.grpc import GrpcInstrumentorServer
from opentelemetry.sdk.metrics import MeterProvider
from opentelemetry.sdk.metrics.export import (
ConsoleMetricExporter,
PeriodicExportingMetricReader,
)
from opentelemetry.sdk.trace import TracerProvider
from opentelemetry.sdk.trace.export import (
ConsoleSpanExporter,
Expand All @@ -80,6 +85,12 @@ def run():
except ImportError:
from gen import helloworld_pb2, helloworld_pb2_grpc
exporter = ConsoleMetricExporter()
reader = PeriodicExportingMetricReader(exporter)
metrics.set_meter_provider(
MeterProvider(metric_readers=[reader])
)
trace.set_tracer_provider(TracerProvider())
trace.get_tracer_provider().add_span_processor(
SimpleSpanProcessor(ConsoleSpanExporter())
Expand Down Expand Up @@ -123,7 +134,7 @@ def serve():
import grpc # pylint:disable=import-self
from wrapt import wrap_function_wrapper as _wrap

from opentelemetry import trace
from opentelemetry import metrics, trace
from opentelemetry.instrumentation.grpc.grpcext import intercept_channel
from opentelemetry.instrumentation.grpc.package import _instruments
from opentelemetry.instrumentation.grpc.version import __version__
Expand Down Expand Up @@ -153,17 +164,24 @@ def instrumentation_dependencies(self) -> Collection[str]:

def _instrument(self, **kwargs):
self._original_func = grpc.server
meter_provider = kwargs.get("meter_provider")
tracer_provider = kwargs.get("tracer_provider")

def server(*args, **kwargs):
if "interceptors" in kwargs:
# add our interceptor as the first
kwargs["interceptors"].insert(
0, server_interceptor(tracer_provider=tracer_provider)
0, server_interceptor(
meter_provider=meter_provider,
tracer_provider=tracer_provider
)
)
else:
kwargs["interceptors"] = [
server_interceptor(tracer_provider=tracer_provider)
server_interceptor(
meter_provider=meter_provider,
tracer_provider=tracer_provider
)
]
return self._original_func(*args, **kwargs)

Expand Down Expand Up @@ -240,17 +258,19 @@ def client_interceptor(tracer_provider=None):
return _client.OpenTelemetryClientInterceptor(tracer)


def server_interceptor(tracer_provider=None):
def server_interceptor(meter_provider=None, tracer_provider=None):
"""Create a gRPC server interceptor.
Args:
tracer: The tracer to use to create server-side spans.
meter_provider: The meter provider which allows acess to the meter.
tracer_provider: The tracer provider which allows acess to the tracer.
Returns:
A service-side interceptor object.
"""
from . import _server

meter = metrics.get_meter(__name__, __version__, meter_provider)
tracer = trace.get_tracer(__name__, __version__, tracer_provider)

return _server.OpenTelemetryServerInterceptor(tracer)
return _server.OpenTelemetryServerInterceptor(meter, tracer)
Loading

0 comments on commit 114732d

Please sign in to comment.