# frozen_string_literal: true

require 'bundler/inline'

gemfile(true) do
  source 'https://rubygems.org'
  gem "opentelemetry-api"
  gem "opentelemetry-common"
  gem "opentelemetry-sdk"

  gem 'opentelemetry-metrics-api', path: '../../metrics_api'
  gem 'opentelemetry-metrics-sdk', path: '../../metrics_sdk'
end

require 'opentelemetry/sdk'
require 'opentelemetry-metrics-sdk'

# this example manually configures the exporter, turn off automatic configuration
ENV['OTEL_METRICS_EXPORTER'] = 'none'

OpenTelemetry::SDK.configure

console_metric_exporter = OpenTelemetry::SDK::Metrics::Export::ConsoleMetricPullExporter.new

OpenTelemetry.meter_provider.add_metric_reader(console_metric_exporter)

meter = OpenTelemetry.meter_provider.meter("SAMPLE_METER_NAME")

recoverable_callback = proc {
  cpu_usage = `ps -p #{Process.pid} -o %cpu=`.strip.to_f
  cpu_usage
}

ob_counter = meter.create_observable_counter('observable_counter', callback: recoverable_callback, unit: 'ms')

ob_counter.observe

OpenTelemetry.meter_provider.metric_readers.each(&:pull)
OpenTelemetry.meter_provider.shutdown
