splunk-otel 0.1.0

Sign up to get free protection for your applications and to get access to all the features.
Files changed (51) hide show
  1. checksums.yaml +7 -0
  2. data/.github/CODEOWNERS +14 -0
  3. data/.github/workflows/main.yml +48 -0
  4. data/.gitignore +13 -0
  5. data/.gitlab-ci.yml +23 -0
  6. data/.rubocop.yml +27 -0
  7. data/CHANGELOG.md +10 -0
  8. data/CODE_OF_CONDUCT.md +90 -0
  9. data/CONTRIBUTING.md +88 -0
  10. data/Gemfile +6 -0
  11. data/LICENSE +177 -0
  12. data/MIGRATING.md +61 -0
  13. data/README.md +190 -0
  14. data/Rakefile +15 -0
  15. data/SECURITY.md +7 -0
  16. data/conf/otel-collector-config.yaml +21 -0
  17. data/docker-compose.yml +11 -0
  18. data/docs/README.md +3 -0
  19. data/docs/advanced-config.md +52 -0
  20. data/docs/correlating-traces-and-logs.md +23 -0
  21. data/docs/instrumenting-rails.md +7 -0
  22. data/docs/troubleshooting.md +14 -0
  23. data/examples/basic/README.md +94 -0
  24. data/examples/basic/collector_trace.png +0 -0
  25. data/examples/basic/console.rb +63 -0
  26. data/examples/basic/direct_trace.png +0 -0
  27. data/examples/basic/docker-compose.yml +9 -0
  28. data/examples/rails-7-barebones/.bundle/config +2 -0
  29. data/examples/rails-7-barebones/.gitignore +1 -0
  30. data/examples/rails-7-barebones/Dockerfile +16 -0
  31. data/examples/rails-7-barebones/Gemfile +17 -0
  32. data/examples/rails-7-barebones/README.md +24 -0
  33. data/examples/rails-7-barebones/app.ru +34 -0
  34. data/examples/rails-7-barebones/config/initializers/splunk_otel.rb +4 -0
  35. data/examples/rails-7-barebones/docker-compose.yml +34 -0
  36. data/examples/rails-7-barebones/tests-e2e/rails_7_barebones_test.rb +27 -0
  37. data/examples/rails-7-barebones/wait-for.sh +191 -0
  38. data/lib/splunk/otel/common.rb +47 -0
  39. data/lib/splunk/otel/instrumentation/action_pack/instrumentation.rb +41 -0
  40. data/lib/splunk/otel/instrumentation/action_pack/railtie.rb +28 -0
  41. data/lib/splunk/otel/instrumentation/action_pack/version.rb +11 -0
  42. data/lib/splunk/otel/instrumentation/action_pack.rb +14 -0
  43. data/lib/splunk/otel/instrumentation/rack/middleware.rb +25 -0
  44. data/lib/splunk/otel/instrumentation/rack.rb +3 -0
  45. data/lib/splunk/otel/logging.rb +24 -0
  46. data/lib/splunk/otel/version.rb +7 -0
  47. data/lib/splunk/otel.rb +170 -0
  48. data/scripts/install-release-deps.sh +10 -0
  49. data/scripts/release.sh +38 -0
  50. data/splunk-otel.gemspec +51 -0
  51. metadata +316 -0
data/README.md ADDED
@@ -0,0 +1,190 @@
1
+ # Splunk Distribution of OpenTelemetry Ruby
2
+
3
+ <p align="left">
4
+ <a href="https://github.com/signalfx/gdi-specification/releases/tag/v1.2.0">
5
+ <img alt="Splunk GDI specification" src="https://img.shields.io/badge/GDI-1.2.0-blueviolet?style=for-the-badge">
6
+ </a>
7
+ <a href="https://github.com/signalfx/splunk-otel-ruby/actions?query=workflow%3A%22Ruby+CI%22">
8
+ <img alt="Build Status" src="https://img.shields.io/github/workflow/status/signalfx/splunk-otel-ruby/Ruby%20CI?style=for-the-badge">
9
+ </a>
10
+ </p>
11
+
12
+ The Splunk Distribution of [OpenTelemetry Instrumentation for
13
+ Ruby](https://github.com/open-telemetry/opentelemetry-ruby) provides a gem for
14
+ setup of OpenTelemetry SDK for reporting distributed traces to [Splunk
15
+ APM](https://docs.splunk.com/Observability/apm/intro-to-apm.html).
16
+
17
+ This distribution comes with the following defaults:
18
+
19
+ - [W3C tracecontext and baggage propagation](https://www.w3.org/TR/trace-context).
20
+ - [OTLP exporter](https://rubygems.org/gems/opentelemetry-exporter-otlp)
21
+ configured to send spans to a locally running [OpenTelemetry
22
+ Collector](https://github.com/open-telemetry/opentelemetry-collector) over
23
+ HTTP (default endpoint: `localhost:4318`).
24
+ - Unlimited default limits for [configuration options](#trace-configuration) to
25
+ support full-fidelity traces.
26
+
27
+ ## Requirements
28
+
29
+ - Ruby 2.6+
30
+
31
+ ## Get started
32
+
33
+ This Splunk distribution comes with the following defaults:
34
+
35
+ - [W3C tracecontext](https://www.w3.org/TR/trace-context/) and [W3C
36
+ baggage](https://www.w3.org/TR/baggage/) context propagation.
37
+ - OTLP over HTTP exporter configured to send spans to a locally running [Splunk
38
+ OpenTelemetry Connector](https://github.com/signalfx/splunk-otel-collector)
39
+ (http://localhost:4318).
40
+ - Unlimited default limits for configuration options to support full-fidelity
41
+ traces.
42
+
43
+ Install the gem by adding it to your project's `Gemfile`:
44
+
45
+ ``` ruby
46
+ gem "splunk-otel", "~> 0.1"
47
+ ```
48
+
49
+ Configure OpenTelemetry using the `Splunk::Otel` module from `splunk/otel`:
50
+
51
+ ``` ruby
52
+ require "splunk/otel"
53
+ ...
54
+ Splunk::Otel.configure
55
+ ```
56
+
57
+ ## Basic configuration
58
+
59
+ The `service.name` resource attribute is the only configuration option that
60
+ needs to be specified, using either the environment variable `OTEL_SERVICE_NAME`
61
+ or passing as an argument to `configure`:
62
+
63
+ ``` ruby
64
+ Splunk::Otel.configure(service_name: "my-service")
65
+ ```
66
+
67
+ Other resource attributes are not strictly required, but
68
+ `deployment.environment` and `service.version` are recommended to be set if they
69
+ are available. These can be set through the environment variable
70
+ `OTEL_RESOURCE_ATTRIBUTES`:
71
+
72
+ ```
73
+ OTEL_RESOURCE_ATTRIBUTES="service.version=1.2.3,deployment.environment=production"
74
+ ```
75
+
76
+ ## Advanced configuration
77
+
78
+ See [advanced-config.md](docs/advanced-config.md) for information on how to
79
+ configure the instrumentation.
80
+
81
+ ## Correlate traces and logs
82
+
83
+ You can add trace metadata to logs using the OpenTelemetry trace API. Trace
84
+ metadata lets you explore logs in Splunk Observability Cloud.
85
+
86
+ See [Correlating traces and logs](docs/correlating-traces-and-logs.md) for more information.
87
+
88
+ ## Library instrumentation
89
+
90
+ Supported libraries are listed
91
+ [here](https://github.com/open-telemetry/opentelemetry-ruby/tree/main/instrumentation).
92
+ The corresponding gems for the instrumentation libraries can be found under the
93
+ [opentelemetry-ruby](https://rubygems.org/profiles/opentelemetry-ruby) profile
94
+ on [rubygems.org](https://rubygems.org).
95
+
96
+ ### Automatic instrumentation
97
+
98
+ You can enable automatic instrumentation of all libraries used in your project
99
+ that have corresponding [OpenTelemetry Ruby
100
+ gems](https://rubygems.org/profiles/opentelemetry-ruby) libraries by installing
101
+ the
102
+ [opentelemetry-instrumentation-all](https://rubygems.org/gems/opentelemetry-instrumentation-all)
103
+ gem in your Gemfile:
104
+
105
+ ``` ruby
106
+ gem "opentelemetry-instrumentation-all", "~> 0.23"
107
+ ```
108
+
109
+ Enable the instrumentations from the gem by passing `auto_instrument:true` to
110
+ the `configure` method of `Splunk::Otel`. For example:
111
+
112
+ ``` ruby
113
+ require "splunk/otel"
114
+
115
+ Splunk::Otel.configure(auto_instrument: true)
116
+ ```
117
+
118
+ The gem fetches all instrumentation libraries but only enables those that
119
+ instrument a dependency in your project. For example, it will fetch
120
+ `opentelemetry-instrumentation-rack` but only if the `rack` gem is included and
121
+ used in your project will the instrumentation be enabled.
122
+
123
+ `auto_instrument: true` also works if individual instrumentation libraries are
124
+ installed, like the `opentelemetry-instrumentation-sinatra` gem.
125
+
126
+ To set configuration of one or more instrumentation libraries a config hash
127
+ can be passed to `use_all`:
128
+
129
+ ``` ruby
130
+ OpenTelemetry::SDK.configure do |c|
131
+ config = {'OpenTelemetry::Instrumentation::Redis' => { opt: "value" }}
132
+ c.use_all(config)
133
+ end
134
+ ```
135
+
136
+ The option `enabled` can be used to disable individual instrumentation libraries
137
+ when using `opentelemetry-instrumentation-all`:
138
+
139
+ ``` ruby
140
+ OpenTelemetry::SDK.configure do |c|
141
+ config = {'OpenTelemetry::Instrumentation::Redis' => { enabled: false }}
142
+ c.use_all(config)
143
+ end
144
+ ```
145
+
146
+ To enable instrumentation libraries manually, see [Manual instrumentation](#manually-instrument-code).
147
+
148
+ ### Manual instrumentation
149
+
150
+ Instrumentation gems can also be installed and enabled individually. This may be
151
+ preferred in order to control exactly which gems are fetched when building your project.
152
+
153
+ Install the instrumentation library by including it in
154
+ the project's `Gemfile`. For example, to install the
155
+ [Sinatra](https://rubygems.org/gems/opentelemetry-instrumentation-sinatra)
156
+ instrumentation:
157
+
158
+ ```
159
+ gem "opentelemetry-instrumentation-sinatra", "~> 0.19"
160
+ ```
161
+
162
+ In a block passed to `Splunk::Otel.configure` configure the SDK to use
163
+ each of the instrumentation libraries. In the case of the Sinatra instrumentation,
164
+ the block would look like the following example:
165
+
166
+ ``` ruby
167
+ require "splunk/otel"
168
+
169
+ Splunk::Otel.configure do |c|
170
+ c.use "OpenTelemetry::Instrumentation::Sinatra", { opt: "value" }
171
+ end
172
+ ```
173
+
174
+ ## Troubleshooting
175
+
176
+ For troubleshooting information, see the [Troubleshooting](docs/troubleshooting.md) documentation.
177
+
178
+ # License
179
+
180
+ The Splunk OpenTelemetry Ruby distribution is released under the terms of the
181
+ Apache Software License version 2.0. For more details, see [the license
182
+ file](./LICENSE).
183
+
184
+ > Copyright 2021 Splunk Inc.
185
+ >
186
+ > Licensed under the Apache License, Version 2.0 (the "License"); you may not use this file except in compliance with the License. You may obtain a copy of the License at
187
+ >
188
+ > http://www.apache.org/licenses/LICENSE-2.0
189
+ >
190
+ > Unless required by applicable law or agreed to in writing, software distributed under the License is distributed on an "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. See the License for the specific language governing permissions and limitations under the License.
data/Rakefile ADDED
@@ -0,0 +1,15 @@
1
+ # frozen_string_literal: true
2
+
3
+ require "bundler/gem_tasks"
4
+ require "rake/testtask"
5
+ require "rubocop/rake_task"
6
+
7
+ Rake::TestTask.new(:test) do |t|
8
+ t.libs << "test"
9
+ t.libs << "lib"
10
+ t.test_files = FileList["test/**/*_test.rb"]
11
+ end
12
+
13
+ RuboCop::RakeTask.new
14
+
15
+ task default: %i[test rubocop]
data/SECURITY.md ADDED
@@ -0,0 +1,7 @@
1
+ # Security
2
+
3
+ ## Reporting Security Issues
4
+
5
+ Please *DO NOT* report security vulnerabilities with public GitHub issue
6
+ reports. Please [report security issues here](
7
+ https://www.splunk.com/en_us/product-security/report.html).
@@ -0,0 +1,21 @@
1
+ receivers:
2
+ otlp:
3
+ protocols:
4
+ http:
5
+ endpoint: "0.0.0.0:4318"
6
+
7
+ processors:
8
+ batch:
9
+
10
+ exporters:
11
+ logging:
12
+ httpsink:
13
+ endpoint: "0.0.0.0:8378"
14
+
15
+ service:
16
+ pipelines:
17
+ traces:
18
+ receivers: [otlp]
19
+ processors: []
20
+ # processors: [batch]
21
+ exporters: [logging, httpsink]
@@ -0,0 +1,11 @@
1
+ version: "3"
2
+ services:
3
+ otel:
4
+ image: quay.io/signalfx/splunk-otel-collector:latest
5
+ command: ["--config=/conf/otel-collector-config.yaml"]
6
+ privileged: true
7
+ ports:
8
+ - 4318:4318
9
+ - 8378:8378
10
+ volumes:
11
+ - ./conf/otel-collector-config.yaml:/conf/otel-collector-config.yaml
data/docs/README.md ADDED
@@ -0,0 +1,3 @@
1
+ The official documentation for this distribution can be found here:
2
+
3
+ https://docs.splunk.com/Observability/gdi/get-data-in/application/ruby/get-started.html
@@ -0,0 +1,52 @@
1
+ # Advanced configuration
2
+
3
+ ## Trace exporters
4
+
5
+ | Environment variable | Default value | Support | Description |
6
+ | --------------------------------- | ------- | ----------- | --- |
7
+ | `OTEL_TRACES_EXPORTER` | `otlp` | Stable | Select the traces exporter to use. We recommend using the OTLP exporter (`otlp`).
8
+ | `OTEL_EXPORTER_OTLP_ENDPOINT` | `http://localhost:4318` | Stable | The OTLP endpoint to connect to.
9
+
10
+ The Splunk Distribution of OpenTelemetry Ruby uses the OTLP traces exporter as
11
+ the default setting. For debugging purposes, use the `console` exporter, which writes
12
+ spans directly to the console.
13
+
14
+ An example of setting an alternate trace exporter:
15
+
16
+ ```
17
+ export OTEL_TRACES_EXPORTER=console
18
+ ```
19
+
20
+ ## Exporting directly to Splunk Observability Cloud
21
+
22
+ To export traces directly to Splunk Observability Cloud, bypassing the Collector,
23
+ set the `SPLUNK_ACCESS_TOKEN` and `SPLUNK_REALM` environment variables.
24
+
25
+ | Environment variable | Default value | Support | Description |
26
+ | -------------------------------------- | ------------ | ----------- | --- |
27
+ | `SPLUNK_ACCESS_TOKEN` | unset | Stable | Splunk authentication token that lets exporters send data directly to Splunk Observability Cloud. Unset by default. Not required unless you need to send data to the Observability Cloud ingest endpoint. See [Create and manage authentication tokens using Splunk Observability Cloud](https://docs.splunk.com/Observability/admin/authentication-tokens/tokens.html#admin-tokens). |
28
+ | `SPLUNK_REALM` | us0 | Stable | The name of your organization’s realm, for example, us0. When you set the realm, traces are sent directly to the ingest endpoint of Splunk Observability Cloud, bypassing the Splunk OpenTelemetry Collector. |
29
+
30
+ ## Propagators configuration
31
+
32
+ | Environment variable | Default value | Support | Description |
33
+ | `OTEL_PROPAGATORS` | `tracecontext,baggage` | Stable | Comma-separated list of propagator names to be used. |
34
+
35
+ To keep backward compatibility with manual instrumentation for the SignalFx Ruby Tracing library, set the trace propagator to B3:
36
+
37
+ ```
38
+ export OTEL_PROPAGATORS=b3multi
39
+ ```
40
+
41
+ ## Trace configuration
42
+
43
+ | Environment variable | Config Option | Default value | Notes |
44
+ | ------------------------- | --------------------- | ------------------------- | ---------------------------------------------------------------------- |
45
+ | OTEL_SERVICE_NAME | service_name | `unnamed-ruby-service` | The service name of this Ruby application. |
46
+ | OTEL_RESOURCE_ATTRIBUTES | | unset | Comma-separated list of resource attributes added to every reported span. <details><summary>Example</summary>`service.name=my-ruby-service,service.version=3.1,deployment.environment=production`</details> |
47
+ | OTEL_SPAN_ATTRIBUTE_COUNT_LIMIT | | `""` (unlimited) | Maximum number of attributes per span. |
48
+ | OTEL_EVENT_ATTRIBUTE_COUNT_LIMIT | | `""` (unlimited) | Maximum number of attributes per event. |
49
+ | OTEL_LINK_ATTRIBUTE_COUNT_LIMIT | | `""` (unlimited) | Maximum number of attributes per link. |
50
+ | OTEL_SPAN_EVENT_COUNT_LIMIT | | `""` (unlimited) | Maximum number of events per span. |
51
+ | OTEL_SPAN_LINK_COUNT_LIMIT | | `1000` | Maximum number of links per span. |
52
+ | OTEL_ATTRIBUTE_VALUE_LENGTH_LIMIT | | `12000` | Maximum length of strings for span attribute values. Values larger than the limit are truncated. |
@@ -0,0 +1,23 @@
1
+ # Correlating trace and logs
2
+
3
+ ## Standard Ruby logger
4
+
5
+ To add trace metadata of the current trace to logs, use the
6
+ `Splunk::Otel::Logging.format_correlation` function of the
7
+ [Ruby standard logger](https://ruby-doc.org/stdlib-3.1.1/libdoc/logger/rdoc/Logger.html)
8
+ to set the formatter, as in the following example:
9
+
10
+ ``` ruby
11
+ require "splunk/otel"
12
+
13
+ logger.formatter = proc do |severity, datetime, progname, msg|
14
+ "#{Splunk::Otel::Logging.format_correlation} : #{msg}\n"
15
+ end
16
+ ```
17
+
18
+ This adds `service.name=<ServiceName> trace_id=<TraceId> span_id=<SpanId>` to each log line.
19
+
20
+ ```
21
+ service.name=basic-service trace_id=789b159aaee2b389a8771b2588278bcf
22
+ span_id=6d26eba14a81f3fa : show log correlation
23
+ ```
@@ -0,0 +1,7 @@
1
+ # Instrumenting Rails
2
+
3
+ TODO
4
+
5
+ ## Example
6
+
7
+ See the [readme for Rails 7 barebones example](../examples/rails-7-barebones/README.md).
@@ -0,0 +1,14 @@
1
+ # Troubleshooting
2
+
3
+ ## Enable debug logging
4
+
5
+ When things are not working, a good first step is to restart the program with
6
+ debug logging enabled. Do this by setting the `OTEL_LOG_LEVEL` environment
7
+ variable to `debug`.
8
+
9
+ ```sh
10
+ export OTEL_LOG_LEVEL="debug"
11
+ ```
12
+
13
+ Make sure to unset the environment variable after the issue is resolved, as its
14
+ output might overload systems if left on indefinitely.
@@ -0,0 +1,94 @@
1
+ # Basic Splunk Otel Distro example
2
+
3
+ The `console.rb` script will use the `splunk-otel` gem from the root of this repo
4
+ as the dependency.
5
+
6
+ Run the script and the function `BasicExample.some_spans` to create a couple spans and logs.
7
+
8
+ ``` shell
9
+ $ ./console.rb
10
+ irb(main):001:0> BasicExample.some_spans
11
+ service.name=unnamed-ruby-service trace_id=01300273775f7550bf4af9f46097afd9 span_id=26737fe11ff026a5 : show log correlation
12
+ => true
13
+ ```
14
+
15
+ ### Export to console
16
+
17
+ The environment variable `OTEL_TRACES_EXPORTER` can be set to `console` to see
18
+ the spans printed to the console when exported:
19
+
20
+ ``` shell
21
+ $ OTEL_TRACES_EXPORTER=console ./console.rb
22
+ service.name=unnamed-ruby-service trace_id=2c9a43b23a08163e4a25c8e3736c7bda span_id=43d59546e5d1b434 : show log correlation
23
+ #<struct OpenTelemetry::SDK::Trace::SpanData
24
+ name="span-2",
25
+ ...
26
+ parent_span_id="\x96\xDD\xC3&\xEE\xAE\xCB:",
27
+ ...
28
+ instrumentation_library=
29
+ #<struct OpenTelemetry::SDK::InstrumentationLibrary
30
+ name="mytracer",
31
+ version="">,
32
+ span_id="C\xD5\x95F\xE5\xD1\xB44",
33
+ trace_id=",\x9AC\xB2:\b\x16>J%\xC8\xE3sl{\xDA",
34
+ ...>
35
+ #<struct OpenTelemetry::SDK::Trace::SpanData
36
+ name="span-1",
37
+ ...
38
+ parent_span_id="\x00\x00\x00\x00\x00\x00\x00\x00",
39
+ ...
40
+ instrumentation_library=
41
+ #<struct OpenTelemetry::SDK::InstrumentationLibrary
42
+ name="mytracer",
43
+ version="">,
44
+ span_id="\x96\xDD\xC3&\xEE\xAE\xCB:",
45
+ trace_id=",\x9AC\xB2:\b\x16>J%\xC8\xE3sl{\xDA",
46
+ ...>
47
+ => true
48
+ ```
49
+
50
+ ### Export to Splunk OTEL Collector
51
+
52
+ ```
53
+ version: "3"
54
+ services:
55
+ splunk-otel-collector:
56
+ image: quay.io/signalfx/splunk-otel-collector:latest
57
+ environment:
58
+ - SPLUNK_ACCESS_TOKEN=${SPLUNK_ACCESS_TOKEN}
59
+ - SPLUNK_REALM=${SPLUNK_REALM}
60
+ ports:
61
+ - "4318:4318"
62
+ ```
63
+
64
+ From this (`examples/basic`) directory bring up the [Splunk OpenTelemetry Collector](https://github.com/signalfx/splunk-otel-collector):
65
+
66
+ ``` shell
67
+ $ SPLUNK_ACCESS_TOKEN=<ACCESS TOKEN> SPLUNK_REALM=<REALM> docker-compose up
68
+ ```
69
+
70
+ Run the example again:
71
+
72
+ ```
73
+ $ OTEL_SERVICE_NAME=basic-ruby-distro-test-collector ./console.rb
74
+ irb(main):001:0> BasicExample.some_spans
75
+ service.name=basic-ruby-distro-test-collector trace_id=0d26692f5982a470a1487c2be5207d20 span_id=64573df1bef69199 : show log correlation
76
+ ```
77
+
78
+ ![Splunk APM Ruby Trace](collector_trace.png)
79
+
80
+ ### Direct export to Splunk
81
+
82
+ The default exporter is `otlp` and can be used to export directly to Splunk by
83
+ setting `SPLUNK_ACCESS_TOKEN` to a token for the organization you wish to export
84
+ spans to:
85
+
86
+ ``` shell
87
+ $ OTEL_SERVICE_NAME=basic-ruby-distro-test-direct SPLUNK_ACCESS_TOKEN=<ACCESS TOKEN> ./console.rb
88
+ irb(main):001:0> BasicExample.some_spans
89
+ service.name=basic-ruby-distro-test-direct trace_id=754ecd93f0254ef5995e10a854dc47ad
90
+ span_id=8d6c02874000469b : show log correlation
91
+ => true
92
+ ```
93
+
94
+ ![Splunk APM Ruby Trace](direct_trace.png)
Binary file
@@ -0,0 +1,63 @@
1
+ #!/usr/bin/env ruby
2
+ # frozen_string_literal: true
3
+
4
+ require "bundler/inline"
5
+
6
+ gemfile do
7
+ source "https://rubygems.org"
8
+
9
+ gem "splunk-otel", path: "../../"
10
+ gem "opentelemetry-exporter-otlp", "~> 0.21.0"
11
+ end
12
+
13
+ require "splunk/otel"
14
+ require "splunk/otel/logging"
15
+ require "opentelemetry/exporter/otlp"
16
+ require "net/http"
17
+ require "json"
18
+
19
+ # a basic example module showing configuration through the Splunk distro
20
+ # and creating 2 spans and logs with trace correlation data
21
+ module BasicExample
22
+ def some_spans
23
+ logger = Logger.new($stdout)
24
+ logger.level = Logger::INFO
25
+
26
+ logger.formatter = proc do |_severity, _datetime, _progname, msg|
27
+ "#{Splunk::Otel::Logging.format_correlation} : #{msg}\n"
28
+ end
29
+
30
+ Splunk::Otel.configure
31
+ tracer = OpenTelemetry.tracer_provider.tracer("mytracer")
32
+ tracer.in_span("basic-example-span-1") do |_span|
33
+ tracer.in_span("basic-example-span-2") do |_span|
34
+ logger.info("show log correlation")
35
+ end
36
+ end
37
+ end
38
+
39
+ def test
40
+ collector_host = ENV.fetch("COLLECTOR_HOST", "localhost")
41
+ collector_port = ENV.fetch("COLLECTOR_PORT", "8378").to_i
42
+
43
+ BasicExample.some_spans
44
+
45
+ response = Net::HTTP.get(collector_host, "/?timeout=20&count=1", collector_port)
46
+ spans = JSON.parse(response)
47
+
48
+ if spans.any? { |span| span["operationName"].to_s == "basic-example-span-1" }
49
+ exit 0
50
+ else
51
+ exit 1
52
+ end
53
+ end
54
+
55
+ module_function :some_spans, :test
56
+ end
57
+
58
+ if ENV.key?("TEST")
59
+ BasicExample.test
60
+ else
61
+ require "irb"
62
+ IRB.start(__FILE__)
63
+ end
Binary file
@@ -0,0 +1,9 @@
1
+ version: "3"
2
+ services:
3
+ splunk-otel-collector:
4
+ image: quay.io/signalfx/splunk-otel-collector:latest
5
+ environment:
6
+ - SPLUNK_ACCESS_TOKEN=${SPLUNK_ACCESS_TOKEN}
7
+ - SPLUNK_REALM=${SPLUNK_REALM}
8
+ ports:
9
+ - "4318:4318"
@@ -0,0 +1,2 @@
1
+ ---
2
+ BUNDLE_BIN: "bin"
@@ -0,0 +1 @@
1
+ .bundle
@@ -0,0 +1,16 @@
1
+ FROM ruby:3-buster
2
+ RUN apt-get update -qq && apt-get install -y nodejs netcat
3
+
4
+ # ruby comes with older versions of bundler sometimes
5
+ RUN gem install bundler -v "2.3.14"
6
+
7
+ # WARNING
8
+ # note: context is changed, see ../../docker-compose.rails-7-barebones.yml
9
+
10
+ ADD ./ /splunk-otel-ruby
11
+ ENV SPLUNK_OTEL_LOCATION="/splunk-otel-ruby"
12
+
13
+ WORKDIR /splunk-otel-ruby/examples/rails-7-barebones
14
+ RUN bundle install
15
+
16
+ CMD ["bundle", "exec", "rackup", "app.ru", "--host", "0.0.0.0", "-p", "3000"]
@@ -0,0 +1,17 @@
1
+ # frozen_string_literal: true
2
+
3
+ source "https://rubygems.org"
4
+ git_source(:github) { |repo| "https://github.com/#{repo}.git" }
5
+
6
+ ruby "3.1.2"
7
+
8
+ # rails app
9
+ gem "puma"
10
+ gem "rails", "~> 7.0.3"
11
+
12
+ # instrumentation
13
+ gem "opentelemetry-instrumentation-rails", "~> 0.22.0"
14
+ gem "splunk-otel", path: ENV.fetch("SPLUNK_OTEL_LOCATION", "../../")
15
+
16
+ # tests
17
+ gem "test-unit", "~> 3.0"
@@ -0,0 +1,24 @@
1
+ # Rails 7 example
2
+
3
+ ## Starting
4
+
5
+ ```shell
6
+ docker-compose up -d
7
+ ```
8
+
9
+ Which starts:
10
+ - Open Telemetry Collector preconfigured for end-to-end testing
11
+ - Rails 7 application instance
12
+ - Test runner
13
+
14
+ You can check the output of E2E tests by running either:
15
+ - `docker-compose logs tester`
16
+ - `ruby tests-e2e/rails_7_barebones_test.rb` (reruns the tests from your machine)
17
+
18
+ To create spans send requests to an endpoint:
19
+
20
+ ```
21
+ curl http://localhost:3000/
22
+ ```
23
+
24
+ The spans will be printed in the console (stdout), and they will also be sent to the Collector instance started by docker-compose.
@@ -0,0 +1,34 @@
1
+ # frozen_string_literal: true
2
+
3
+ # Copyright The OpenTelemetry Authors
4
+ #
5
+ # SPDX-License-Identifier: Apache-2.0
6
+
7
+ require "rubygems"
8
+ require "bundler/setup"
9
+ Bundler.require :default
10
+
11
+ require "action_controller/railtie"
12
+
13
+ # TraceRequestApp is a minimal Rails application inspired by the Rails
14
+ # bug report template for action controller.
15
+ # https://github.com/rails/rails/blob/v6.0.0/guides/bug_report_templates/action_controller_gem.rb
16
+ # The configuration is compatible with Rails 6.0
17
+ class TraceRequestApp < Rails::Application
18
+ config.root = __dir__
19
+ config.hosts << "example.org"
20
+ secrets.secret_key_base = "secret_key_base"
21
+ config.eager_load = false
22
+
23
+ config.logger = Logger.new($stdout)
24
+ Rails.logger = config.logger
25
+ end
26
+
27
+ Rails.application.initialize!
28
+
29
+ run Rails.application
30
+
31
+ # To run this example run the `rackup` command with this file
32
+ # Example: rackup trace_request_demonstration.ru
33
+ # Navigate to http://localhost:9292/
34
+ # Spans for the requests will appear in the console
@@ -0,0 +1,4 @@
1
+ # frozen_string_literal: true
2
+
3
+ ENV["OTEL_TRACES_EXPORTER"] = "console,otlp"
4
+ Splunk::Otel.configure(service_name: "rails-7-barebones", auto_instrument: true)
@@ -0,0 +1,34 @@
1
+ services:
2
+ collector:
3
+ image: quay.io/signalfx/splunk-otel-collector:latest
4
+ command: ["--config=/conf/otel-collector-config.yaml"]
5
+ ports:
6
+ - 4318:4318
7
+ - 8378:8378
8
+ volumes:
9
+ - ../../conf/otel-collector-config.yaml:/conf/otel-collector-config.yaml
10
+
11
+ rails_7_barebones:
12
+ build:
13
+ context: ../../
14
+ dockerfile: ./examples/rails-7-barebones/Dockerfile
15
+ ports:
16
+ - 3000:3000
17
+ environment:
18
+ - OTEL_EXPORTER_OTLP_TRACES_ENDPOINT=http://collector:4318/v1/traces
19
+ links:
20
+ - collector
21
+
22
+ tester:
23
+ build:
24
+ context: ../../
25
+ dockerfile: ./examples/rails-7-barebones/Dockerfile
26
+ links:
27
+ - rails_7_barebones
28
+ - collector
29
+ environment:
30
+ - APP_HOST=rails_7_barebones
31
+ - APP_PORT=3000
32
+ - COLLECTOR_HOST=collector
33
+ - COLLECTOR_PORT=8378
34
+ entrypoint: ["./wait-for.sh", "rails_7_barebones:3000", "--", "ruby", "tests-e2e/rails_7_barebones_test.rb"]