Skip to content

Commit

Permalink
Merge branch 'main' into manual_metrics_recommendationservice
Browse files Browse the repository at this point in the history
  • Loading branch information
cartersocha authored Oct 8, 2022
2 parents 813df95 + b27fa7d commit acffd32
Show file tree
Hide file tree
Showing 2 changed files with 53 additions and 5 deletions.
44 changes: 43 additions & 1 deletion docs/services/checkoutservice.md
Original file line number Diff line number Diff line change
Expand Up @@ -109,7 +109,49 @@ Adding a span event with additional attributes:

## Metrics

TBD
### Initialize meter provider

The OpenTelemetry SDK is initialized from `main` using the `initMeterProvider`
function.

```go
func initMeterProvider() *sdkmetric.MeterProvider {
ctx := context.Background()

exporter, err := otlpmetricgrpc.New(ctx)
if err != nil {
log.Fatalf("new otlp metric grpc exporter failed: %v", err)
}

mp := sdkmetric.NewMeterProvider(sdkmetric.WithReader(sdkmetric.NewPeriodicReader(exporter)))
global.SetMeterProvider(mp)
return mp
}
```

You should call `MeterProvider.Shutdown()` when your service is shutdown to
ensure all records are exported. This service makes that call as part of a
deferred function in main

```go
mp := initMeterProvider()
defer func() {
if err := mp.Shutdown(context.Background()); err != nil {
log.Printf("Error shutting down meter provider: %v", err)
}
}()
```

### Adding golang runtime auto-instrumentation

Golang runtime are instrumented in the main function

```go
err := runtime.Start(runtime.WithMinimumReadMemStatsInterval(time.Second))
if err != nil {
log.Fatal(err)
}
```

## Logs

Expand Down
14 changes: 10 additions & 4 deletions src/recommendationservice/recommendation_server.py
Original file line number Diff line number Diff line change
Expand Up @@ -68,19 +68,25 @@ def Watch(self, request, context):
def get_product_list(request_product_ids):
with tracer.start_as_current_span("get_product_list") as span:
max_responses = 5
# fetch list of products from product catalog stub

# Formulate the list of characters to list of strings
request_product_ids_str = ''.join(request_product_ids)
request_product_ids = request_product_ids_str.split(',')

# Fetch list of products from product catalog stub
cat_response = product_catalog_stub.ListProducts(demo_pb2.Empty())
product_ids = [x.id for x in cat_response.products]
span.set_attribute("app.products.count", len(product_ids))

# Create a filtered list of products excluding the products received as input
filtered_products = list(set(product_ids) - set(request_product_ids))
num_products = len(filtered_products)
span.set_attribute("app.filtered_products.count", num_products)

num_return = min(max_responses, num_products)
# sample list of indicies to return

# Sample list of indicies to return
indices = random.sample(range(num_products), num_return)
# fetch product ids from indices
# Fetch product ids from indices
prod_list = [filtered_products[i] for i in indices]
return prod_list

Expand Down

0 comments on commit acffd32

Please sign in to comment.