Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

[demo] Add C++ metrics #2432

Merged
merged 4 commits into from
Mar 2, 2023
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
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