Skip to content

Commit

Permalink
update one more script to run otlp metrics exporter
Browse files Browse the repository at this point in the history
  • Loading branch information
xuan-cao-swi committed May 30, 2024
1 parent cf76522 commit 8dfcaaa
Show file tree
Hide file tree
Showing 2 changed files with 94 additions and 16 deletions.
76 changes: 60 additions & 16 deletions examples/metrics_sdk/README.md
Original file line number Diff line number Diff line change
@@ -1,29 +1,73 @@
# OpenTelemetry Ruby Example
# OpenTelemetry Ruby Metrics SDK Example

## HTTP
### metrics_collect.rb

This is a simple example that demonstrates tracing an HTTP request from client to server. The example shows several aspects of tracing, such as:
Running the script to see the metric data from console

* Using the `TracerProvider`
* Span Attributes
* Using the console exporter
```sh
ruby metrics_collect.rb
```

### metrics_collect_otlp.rb

**WARN: this example doesn't work on alpine aarch64 container due to grpc installation issue.**

This example test both metrics sdk and metrics otlp http exporter.

### Running the example
You can view the metrics in your favored backend (e.g. jaeger).

Install gems
#### 1. Setup the local opentelemetry-collector.

e.g.
```sh
bundle install
docker pull otel/opentelemetry-collector
docker run --rm -v $(pwd)/config.yaml:/etc/otel/config.yaml -p 4317:4317 -p 4318:4318 otel/opentelemetry-collector --config /etc/otel/config.yaml
```
Sample config.yaml
```yaml
receivers:
otlp:
protocols:
grpc:
http:
# Default endpoints: 0.0.0.0:4317 for gRPC and 0.0.0.0:4318 for HTTP

Start the server
```sh
ruby server.rb
exporters:
logging:
loglevel: debug

processors:
batch:

service:
pipelines:
traces:
receivers: [otlp]
processors: [batch]
exporters: [logging]
metrics:
receivers: [otlp]
processors: [batch]
exporters: [logging]
```

In a separate terminal window, run the client to make a single request:
```sh
ruby client.rb
More information on how to setup the otel collector in [quick start](https://opentelemetry.io/docs/collector/quick-start/).

#### 2. Assign endpoint value to destinated address

e.g.
```
# Using environment variable
ENV['OTEL_EXPORTER_OTLP_METRICS_ENDPOINT'] = 'http://host.docker.internal:4318/v1/metrics'
# Or using export command
export OTEL_EXPORTER_OTLP_METRICS_ENDPOINT=http://host.docker.internal:4318/v1/metrics
```

You should see console exporter output for both the client and server sessions.
#### 3. Running the script to send metric data to otlp collector

```sh
ruby metrics_collect_otlp.rb
```

You should see the metric data appearing in the collector.
34 changes: 34 additions & 0 deletions examples/metrics_sdk/metrics_collect_otlp.rb
Original file line number Diff line number Diff line change
@@ -0,0 +1,34 @@
# 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'
gem 'opentelemetry-exporter-otlp-metrics', path: '../../exporter/otlp-metrics'

end

require 'opentelemetry/sdk'
require 'opentelemetry-metrics-sdk'
require 'opentelemetry-exporter-otlp-metrics'

OpenTelemetry::SDK.configure

console_metric_exporter = OpenTelemetry::Exporter::OTLP::MetricsExporter.new

OpenTelemetry.meter_provider.add_metric_reader(console_metric_exporter)

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

histogram = meter.create_histogram('histogram', unit: 'smidgen', description: 'desscription')

histogram.record(123, attributes: {'foo' => 'bar'})

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

0 comments on commit 8dfcaaa

Please sign in to comment.