Skip to content

Commit

Permalink
[demo] Add C++ metrics (#2432)
Browse files Browse the repository at this point in the history
Co-authored-by: Patrice Chalin <chalin@users.noreply.github.com>
  • Loading branch information
julianocosta89 and chalin authored Mar 2, 2023
1 parent 100e517 commit 4547ad2
Show file tree
Hide file tree
Showing 2 changed files with 66 additions and 3 deletions.
2 changes: 1 addition & 1 deletion content/en/docs/demo/metric-features.md
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,7 @@ aliases: [/docs/demo/metric_service_features]
| Ad | Java ||| 🚧 | 🚧 ||||
| Cart | .NET || 🚧 | 🚧 | 🚧 | 🚧 | 🚧 | 🚧 |
| Checkout | Go || 🚧 | 🚧 | 🚧 | 🚧 | 🚧 | 🚧 |
| Currency | C++ | 🔕 | 🚧 | 🚧 | 🚧 | 🚧 | 🚧 | 🚧 |
| Currency | C++ | 🔕 | | 🚧 | 🚧 | 🚧 | 🚧 | 🚧 |
| Email | Ruby | 🚧 | 🚧 | 🚧 | 🚧 | 🚧 | 🚧 | 🚧 |
| Feature Flag | Erlang / Elixir | 🚧 | 🚧 | 🚧 | 🚧 | 🚧 | 🚧 | 🚧 |
| Fraud Detection | Kotlin || 🚧 | 🚧 | 🚧 | 🚧 || 🚧 |
Expand Down
67 changes: 65 additions & 2 deletions content/en/docs/demo/services/currency.md
Original file line number Diff line number Diff line change
Expand Up @@ -14,7 +14,7 @@ currencies.
### Initializing Tracing

The OpenTelemetry SDK is initialized from `main` using the `initTracer` function
defined in `tacer_common.h`
defined in `tracer_common.h`

```cpp
void initTracer()
Expand Down Expand Up @@ -137,7 +137,70 @@ creating new spans.

## Metrics

TBD
### Initializing Metrics

The OpenTelemetry `MeterProvider` is initialized from `main()` using the
`initMeter()` function defined in `meter_common.h`.

```cpp
void initMeter()
{
// Build MetricExporter
otlp_exporter::OtlpGrpcMetricExporterOptions otlpOptions;

// Configuration via environment variable not supported yet
otlpOptions.endpoint = "otelcol:4317";
otlpOptions.aggregation_temporality = metric_sdk::AggregationTemporality::kDelta;
auto exporter = otlp_exporter::OtlpGrpcMetricExporterFactory::Create(otlpOptions);

// Build MeterProvider and Reader
metric_sdk::PeriodicExportingMetricReaderOptions options;
options.export_interval_millis = std::chrono::milliseconds(1000);
options.export_timeout_millis = std::chrono::milliseconds(500);
std::unique_ptr<metric_sdk::MetricReader> reader{
new metric_sdk::PeriodicExportingMetricReader(std::move(exporter), options) };
auto provider = std::shared_ptr<metrics_api::MeterProvider>(new metric_sdk::MeterProvider());
auto p = std::static_pointer_cast<metric_sdk::MeterProvider>(provider);
p->AddMetricReader(std::move(reader));
metrics_api::Provider::SetMeterProvider(provider);
}
```

### Starting IntCounter

A global `currency_counter` variable is created at `main()` calling the function
`initIntCounter()` defined in `meter_common.h`.

```cpp
nostd::unique_ptr<metrics_api::Counter<uint64_t>> initIntCounter()
{
std::string counter_name = name + "_counter";
auto provider = metrics_api::Provider::GetMeterProvider();
nostd::shared_ptr<metrics_api::Meter> meter = provider->GetMeter(name, version);
auto int_counter = meter->CreateUInt64Counter(counter_name);
return int_counter;
}
```

### Counting currency conversion requests

The method `CurrencyCounter()` is implemented as follows:

```cpp
void CurrencyCounter(const std::string& currency_code)
{
std::map<std::string, std::string> labels = { {"currency_code", currency_code} };
auto labelkv = common::KeyValueIterableView<decltype(labels)>{ labels };
currency_counter->Add(1, labelkv);
}
```
Every time the function `Convert()` is called, the currency code received as
`to_code` is used to count the conversions.
```cpp
CurrencyCounter(to_code);
```

## Logs

Expand Down

0 comments on commit 4547ad2

Please sign in to comment.