open-telemetry / open-telemetry/opentelemetry-cpp

measurement residual in SDK

Open
#2,199 10 comments 0 reactions 1 assignee View on GitHub

@lalitb is already working on this.

Since Jun 19, 2023.

bug do-not-stale
Dominant language
C++
Stars
1.4k
Forks
632
Avg merge
1d 13h
Merged PRs (30d)
75

Description

Describe your environment
opentelemetry-cpp 1.9.0 SDK

Steps to reproduce
Using one instrument to observe memory of multiple processes, process can appear and disappear dynamically.
Measurements of disappeared process are still transmitted out to opentelemetry collector.
code snippet:

  auto exporter = otlp_exporter::OtlpGrpcMetricExporterFactory::Create(options);

  std::string version{"1.2.0"};
  std::string schema{"https://opentelemetry.io/schemas/1.2.0"};

  // Initialize and set the global MeterProvider
  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);
void foo_library::observable_process_example(const std::string &name)
{
  std::string observableProcess = name + "_process_metrics_v1";
  auto provider                               = metrics_api::Provider::GetMeterProvider();
  nostd::shared_ptr<metrics_api::Meter> meter = provider->GetMeter(name, "1.2.0");
  auto process_mem_counter = meter->CreateInt64ObservableGauge(observableProcess);
  process_mem_counter->AddCallback(MeasurementFetcher::process_callback, nullptr);
  while(1)
  {
      std::this_thread::sleep_for(std::chrono::seconds(1));
  }
}
  static void process_callback(opentelemetry::metrics::ObserverResult observer, void * /* state */)
  {
    auto observer_int = nostd::get<nostd::shared_ptr<opentelemetry::metrics::ObserverResultT<int64_t>>>(observer);
    for (int n: processes)
    {
        process_memory = getProcessMemory(n); //get processes memory info for pid n
        observer_int ->Observe(process_memory, {{"process_name", process.name},{"pid", n}});
    }
    
  }

What is the expected behavior?
If the process exit, it's metrics should not be seen in opentelemetry collector.

What is the actual behavior?
Measurements of disappeared process are still transmitted out to opentelemetry collector.

Additional context
see code snippet.

Contributor guide

Open the contributing guide

First steps

  1. Read the whole issue, then the project's contributing guide.
  2. Comment on the issue to say you are picking it up — it saves two people doing the same work.
  3. Fork the repository and make your change on a branch.
  4. Open a pull request that references the issue number.

Assessment

This issue has not been assessed yet.

Get new issues in your inbox

A short digest of beginner-friendly GitHub issues.