Skip to content

Commit 4e89bf1

Browse files
committed
Add Rakefile, README, API docs, change namespace of Otel tracer/meter
1 parent 570bfe3 commit 4e89bf1

File tree

16 files changed

+298
-71
lines changed

16 files changed

+298
-71
lines changed

.gitignore

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -8,14 +8,14 @@
88
.byebug_history
99
/.rakeTasks
1010
/.ruby-version
11-
/.yardoc
11+
/**/.yardoc
1212
/_yardoc/
1313
/cmake-build-*/
1414
/build*
1515
/coverage/
16-
/doc/
16+
/**/doc/
1717
/ext/cache/
18-
/pkg/
18+
/**/pkg/
1919
/test/reports/
2020
/tmp/
2121
/logs/

README.md

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -18,7 +18,7 @@ Please attach version information to the ticket/post. To obtain this information
1818

1919
## Installation
2020

21-
The library has been tested with MRI 3.1, 3.2, 3.3 and 3.4. Supported platforms are Linux and MacOS.
21+
The library has been tested with MRI 3.2, 3.3, 3.4 and 4.0. Supported platforms are Linux and MacOS.
2222

2323
Add this line to your application's Gemfile:
2424

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1 @@
1+
inherit_from: ../.rubocop.yml

couchbase-opentelemetry/.yardopts

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,3 @@
1+
--no-progress --output-dir doc/couchbase-ruby-client-master lib - README.md
2+
--tag couchbase.stability:"Stability"
3+
--transitive-tag couchbase.stability

couchbase-opentelemetry/README.md

Lines changed: 83 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,83 @@
1+
# Couchbase Ruby Client OpenTelemetry Integration
2+
3+
## Installation
4+
5+
The library has been tested with MRI 3.2, 3.3, 3.4 and 4.0. Supported platforms are Linux and MacOS.
6+
7+
Add this line to your application's Gemfile:
8+
9+
```ruby
10+
gem "couchbase-opentelemetry", "0.1.0"
11+
```
12+
13+
And then execute:
14+
15+
$ bundle install
16+
17+
Or install it yourself as:
18+
19+
$ gem install couchbase-opentelemetry
20+
21+
## Usage
22+
23+
Here is an example on how to set up Tracing and Metrics with OpenTelemetr:
24+
25+
```ruby
26+
require "couchbase"
27+
require "couchbase/opentelemetry"
28+
29+
require "opentelemetry-sdk"
30+
require "opentelemetry-metrics-sdk"
31+
32+
# Initialize a tracer provider
33+
tracer_provider = OpenTelemetry::SDK::Trace::TracerProvider.new
34+
tracer_provider.add_span_processor(
35+
OpenTelemetry::SDK::Trace::Export::BatchSpanProcessor.new(
36+
OpenTelemetry::Exporter::OTLP::Exporter.new(endpoint: "https://<hostname>:<port>/v1/traces")
37+
)
38+
)
39+
40+
# Initialize the Couchbase OpenTelemetry Request Tracer
41+
tracer = Couchbase::OpenTelemetry::RequestTracer.new(tracer_provider)
42+
43+
# Initialize a meter provider
44+
meter_provider = OpenTelemetry::SDK::Metrics::MeterProvider.new
45+
meter_provider.add_metric_reader(
46+
OpenTelemetry::SDk::Metrics::Export::PeriodicMetricReader.new(
47+
exporter: OpenTelemetry::Exporter::OTLP::Metrics::MetricsExporter.new(
48+
endpoint: "https://<hostname>:<port>/v1/metrics"
49+
)
50+
)
51+
)
52+
53+
# Initialize the Couchbase OpenTelemetry Meter
54+
meter = Couchbase::OpenTelemetry::Meter.new(meter_provider)
55+
56+
# Configure tracer and meter in cluster options
57+
options = Couchbase::Options::Cluster.new(
58+
authenticator: Couchbase::PasswordAuthenticator.new("Administrator", "password")
59+
tracer: tracer,
60+
meter: meter
61+
)
62+
63+
# Initialize cluster instance
64+
cluster = Cluster.connect("couchbase://127.0.0.1", options)
65+
```
66+
67+
## License
68+
69+
The gem is available as open source under the terms of the [Apache 2.0 License](https://opensource.org/licenses/Apache-2.0).
70+
71+
Copyright 2025-Present Couchbase, Inc.
72+
73+
Licensed under the Apache License, Version 2.0 (the "License");
74+
you may not use this file except in compliance with the License.
75+
You may obtain a copy of the License at
76+
77+
http://www.apache.org/licenses/LICENSE-2.0
78+
79+
Unless required by applicable law or agreed to in writing, software
80+
distributed under the License is distributed on an "AS IS" BASIS,
81+
WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
82+
See the License for the specific language governing permissions and
83+
limitations under the License.

couchbase-opentelemetry/Rakefile

Lines changed: 38 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,38 @@
1+
# frozen_string_literal: true
2+
3+
# Copyright 2026-Present Couchbase, Inc.
4+
#
5+
# Licensed under the Apache License, Version 2.0 (the "License");
6+
# you may not use this file except in compliance with the License.
7+
# You may obtain a copy of the License at
8+
#
9+
# http://www.apache.org/licenses/LICENSE-2.0
10+
#
11+
# Unless required by applicable law or agreed to in writing, software
12+
# distributed under the License is distributed on an "AS IS" BASIS,
13+
# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
14+
# See the License for the specific language governing permissions and
15+
# limitations under the License.
16+
17+
require "bundler/gem_tasks"
18+
require "rubocop/rake_task"
19+
20+
desc "Generate YARD documentation"
21+
task :doc do
22+
require "couchbase/opentelemetry/version"
23+
input_dir = File.join(__dir__, "lib")
24+
output_dir = File.join(__dir__, "doc", "couchbase-ruby-client-#{Couchbase::OpenTelemetry::VERSION}")
25+
rm_rf output_dir
26+
sh "bundle exec yard doc --no-progress --hide-api private --output-dir #{output_dir} #{input_dir} --main README.md"
27+
puts "#{File.realpath(output_dir)}/index.html"
28+
end
29+
30+
desc "An alias for documentation generation task"
31+
task :docs => :doc
32+
33+
desc "Display stats on undocumented things"
34+
task :undocumented => :doc do
35+
sh "yard stats --list-undoc --compact"
36+
end
37+
38+
RuboCop::RakeTask.new

couchbase-opentelemetry/couchbase-opentelemetry.gemspec

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -26,7 +26,7 @@ Gem::Specification.new do |spec|
2626
spec.description = "OpenTelemetry integration for the Couchbase Ruby Client"
2727
spec.homepage = "https://www.couchbase.com"
2828
spec.license = "Apache-2.0"
29-
spec.required_ruby_version = "> 3.1"
29+
spec.required_ruby_version = "> 3.2"
3030

3131
spec.metadata = {
3232
"homepage_uri" => "https://docs.couchbase.com/ruby-sdk/current/hello-world/start-using-sdk.html",

couchbase-opentelemetry/lib/couchbase/metrics/open_telemetry_meter.rb

Lines changed: 0 additions & 49 deletions
This file was deleted.
Lines changed: 18 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,18 @@
1+
# frozen_string_literal: true
2+
3+
# Copyright 2026-Present Couchbase, Inc.
4+
#
5+
# Licensed under the Apache License, Version 2.0 (the "License");
6+
# you may not use this file except in compliance with the License.
7+
# You may obtain a copy of the License at
8+
#
9+
# http://www.apache.org/licenses/LICENSE-2.0
10+
#
11+
# Unless required by applicable law or agreed to in writing, software
12+
# distributed under the License is distributed on an "AS IS" BASIS,
13+
# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
14+
# See the License for the specific language governing permissions and
15+
# limitations under the License.
16+
17+
require_relative "opentelemetry/request_tracer"
18+
require_relative "opentelemetry/meter"
Lines changed: 82 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,82 @@
1+
# frozen_string_literal: true
2+
3+
# Copyright 2026-Present Couchbase, Inc.
4+
#
5+
# Licensed under the Apache License, Version 2.0 (the "License");
6+
# you may not use this file except in compliance with the License.
7+
# You may obtain a copy of the License at
8+
#
9+
# http://www.apache.org/licenses/LICENSE-2.0
10+
#
11+
# Unless required by applicable law or agreed to in writing, software
12+
# distributed under the License is distributed on an "AS IS" BASIS,
13+
# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
14+
# See the License for the specific language governing permissions and
15+
# limitations under the License.
16+
17+
require "couchbase/metrics/meter"
18+
require "couchbase/errors"
19+
require_relative "value_recorder"
20+
21+
require "opentelemetry-metrics-api"
22+
23+
module Couchbase
24+
module OpenTelemetry
25+
# @couchbase.stability
26+
# **Uncommitted:** This API may change in the future, as Metrics in OpenTelemetry Ruby are currently in development.
27+
#
28+
# See the {https://opentelemetry.io/docs/languages/ruby/#status-and-releases OpenTelemetry Ruby documentation} for more information.
29+
class Meter < ::Couchbase::Metrics::Meter
30+
# Initializes a Couchbase OpenTelemetry Meter
31+
#
32+
# @param [::OpenTelemetry::Metrics::MeterProvider] meter_provider The OpenTelemetry meter provider
33+
#
34+
# @raise [Couchbase::Error::MeterError] if the meter cannot be created for any reason
35+
#
36+
# @example Initializing a Couchbase OpenTelemetry Meter with an OTLP Exporter
37+
# require "opentelemetry-metrics-sdk"
38+
#
39+
# # Initialize a meter provider
40+
# meter_provider = ::OpenTelemetry::SDK::Metrics::MeterProvider.new
41+
# meter_provider.add_metric_reader(
42+
# ::OpenTelemetry::SDK::Metrics::Export::PeriodicMetricReader.new(
43+
# exporter: ::OpenTelemetry::Exporter::OTLP::Metrics::MetricsExporter.new(
44+
# endpoint: "https://<hostname>:<port>/v1/metrics"
45+
# )
46+
# )
47+
# )
48+
# # Initialize the Couchbase OpenTelemetry Meter
49+
# meter = Couchbase::OpenTelemetry::Meter.new(meter_provider)
50+
#
51+
# # Set the meter in the cluster options
52+
# options = Couchbase::Options::Cluster.new(
53+
# authenticator: Couchbase::PasswordAuthenticator.new("Administrator", "password")
54+
# meter: meter
55+
# )
56+
#
57+
# @see https://www.rubydoc.info/gems/opentelemetry-metrics-sdk/OpenTelemetry/SDK/Metrics/MeterProvider
58+
# <tt>opentelemetry-metrics-sdk</tt> API Reference
59+
def initialize(meter_provider)
60+
super()
61+
@histogram_cache = Concurrent::Map.new
62+
begin
63+
@wrapped = meter_provider.meter("com.couchbase.client/ruby")
64+
rescue StandardError => e
65+
raise Error::MeterError.new("Failed to create OpenTelemetry Meter: #{e.message}", nil, e)
66+
end
67+
end
68+
69+
def value_recorder(name, tags)
70+
unit = tags.delete("__unit")
71+
72+
otel_histogram = @histogram_cache.compute_if_absent(name) do
73+
@wrapped.create_histogram(name, unit: unit)
74+
end
75+
76+
ValueRecorder.new(otel_histogram, tags, unit: unit)
77+
rescue StandardError => e
78+
raise Error::MeterError.new("Failed to create OpenTelemetry Histogram: #{e.message}", nil, e)
79+
end
80+
end
81+
end
82+
end

0 commit comments

Comments
 (0)