Skip to content

Commit

Permalink
Adding basic metrics support for recommendation service (Python) (#416)
Browse files Browse the repository at this point in the history
  • Loading branch information
mviitane authored Oct 10, 2022
1 parent 1f1dbd6 commit 88c0440
Show file tree
Hide file tree
Showing 5 changed files with 40 additions and 11 deletions.
2 changes: 2 additions & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -111,3 +111,5 @@ significant modifications will be credited to OpenTelemetry Authors.
([#409](https://github.com/open-telemetry/opentelemetry-demo/pull/409))
* Added hero scenario metric to Checkout Service on cache leak
([#339](https://github.com/open-telemetry/opentelemetry-demo/pull/339))
* Added basic metrics support for recommendation service (Python)
([#416](https://github.com/open-telemetry/opentelemetry-demo/pull/416))
3 changes: 3 additions & 0 deletions docker-compose.yml
Original file line number Diff line number Diff line change
Expand Up @@ -329,6 +329,9 @@ services:
- PRODUCT_CATALOG_SERVICE_ADDR
- OTEL_PYTHON_LOG_CORRELATION=true
- OTEL_EXPORTER_OTLP_TRACES_ENDPOINT
# OTEL_EXPORTER_OTLP_METRICS_ENDPOINT # Not working for Python OTLP exporter
- OTEL_EXPORTER_OTLP_ENDPOINT
- OTEL_EXPORTER_OTLP_METRICS_TEMPORALITY_PREFERENCE
- OTEL_SERVICE_NAME=recommendationservice
- PROTOCOL_BUFFERS_PYTHON_IMPLEMENTATION=python
logging: *logging
Expand Down
2 changes: 1 addition & 1 deletion docs/metric_service_features.md
Original file line number Diff line number Diff line change
Expand Up @@ -17,5 +17,5 @@ Emoji Legend
| Frontend | JavaScript | :construction: | :construction: | :construction: | :construction: | :construction: | :construction: |
| Payment | JavaScript | :construction: | :construction: | :construction: | :construction: | :construction: | :construction: |
| Product Catalog | Go | :construction: | :construction: | :construction: | :construction: | :construction: | :construction: |
| Recommendation | Python | :construction: | :construction: | :construction: | :construction: | :construction: | :construction: |
| Recommendation | Python | :construction: | :100: | :construction: | :construction: | :construction: | :construction: |
| Shipping | Rust | :construction: | :construction: | :construction: | :construction: | :construction: | :construction: |
32 changes: 28 additions & 4 deletions src/recommendationservice/recommendation_server.py
Original file line number Diff line number Diff line change
Expand Up @@ -22,11 +22,19 @@

# Pip
import grpc

# Traces
from opentelemetry import trace
from opentelemetry.sdk.trace import TracerProvider
from opentelemetry.sdk.trace.export import (BatchSpanProcessor)
from opentelemetry.exporter.otlp.proto.grpc.trace_exporter import OTLPSpanExporter

# Metrics
from opentelemetry import metrics
from opentelemetry.sdk.metrics import MeterProvider
from opentelemetry.sdk.metrics.export import (PeriodicExportingMetricReader)
from opentelemetry.exporter.otlp.proto.grpc.metric_exporter import OTLPMetricExporter

# Local
import demo_pb2
import demo_pb2_grpc
Expand All @@ -41,6 +49,8 @@ def ListRecommendations(self, request, context):
span = trace.get_current_span()
span.set_attribute("app.products_recommended.count", len(prod_list))
logger.info("[Recv ListRecommendations] product_ids={}".format(prod_list))
app_recommendations_counter.add(len(prod_list), {'recommendation.type': 'catalog'})

# build and return response
response = demo_pb2.ListRecommendationsResponse()
response.product_ids.extend(prod_list)
Expand Down Expand Up @@ -88,28 +98,42 @@ def must_map_env(key: str):
return value

if __name__ == "__main__":
logger = getJSONLogger('recommendationservice-server')
# Initialize tracer provider
tracer_provider = TracerProvider()
trace.set_tracer_provider(tracer_provider)
tracer_provider.add_span_processor(BatchSpanProcessor(OTLPSpanExporter()))
tracer = trace.get_tracer("recommendationservice")

# Initialize meter provider
metric_reader = PeriodicExportingMetricReader(OTLPMetricExporter())
provider = MeterProvider(metric_readers=[metric_reader])
metrics.set_meter_provider(provider)
meter = metrics.get_meter(__name__)

# Create counters
app_recommendations_counter = meter.create_counter(
'app.recommendations.counter', unit='recommendations', description="Counts the total number of given recommendations"
)

port = must_map_env('RECOMMENDATION_SERVICE_PORT')
catalog_addr = must_map_env('PRODUCT_CATALOG_SERVICE_ADDR')

channel = grpc.insecure_channel(catalog_addr)
product_catalog_stub = demo_pb2_grpc.ProductCatalogServiceStub(channel)

# create gRPC server
# Create gRPC server
server = grpc.server(futures.ThreadPoolExecutor(max_workers=10))

# add class to gRPC server
# Add class to gRPC server
service = RecommendationService()
demo_pb2_grpc.add_RecommendationServiceServicer_to_server(service, server)
health_pb2_grpc.add_HealthServicer_to_server(service, server)

# start server
# Start logger
logger = getJSONLogger('recommendationservice-server')
logger.info("RecommendationService listening on port: " + port)

# Start server
server.add_insecure_port('[::]:' + port)
server.start()
server.wait_for_termination()
12 changes: 6 additions & 6 deletions src/recommendationservice/requirements.txt
Original file line number Diff line number Diff line change
@@ -1,12 +1,12 @@
google-api-core==2.4.0
grpcio-health-checking==1.43.0
grpcio==1.43.0
opentelemetry-api==1.9.1
opentelemetry-exporter-otlp-proto-grpc==1.9.1
opentelemetry-instrumentation==0.28b1
opentelemetry-instrumentation-grpc==0.28b1
opentelemetry-instrumentation-urllib3==0.28b1
opentelemetry-sdk==1.9.1
opentelemetry-api==1.13.0
opentelemetry-exporter-otlp-proto-grpc==1.13.0
opentelemetry-instrumentation==0.34b0
opentelemetry-instrumentation-grpc==0.34b0
opentelemetry-instrumentation-urllib3==0.34b0
opentelemetry-sdk==1.13.0
python-dotenv==0.20.0
python-json-logger==2.0.2
requests==2.27.1
Expand Down

0 comments on commit 88c0440

Please sign in to comment.