Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

[Quick start Example] Adding example with Sinatra #2319

Merged
merged 3 commits into from
Apr 10, 2023
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
1 change: 1 addition & 0 deletions examples/quick-start-ruby-sinatra/.gitignore
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
.DS_Store
1 change: 1 addition & 0 deletions examples/quick-start-ruby-sinatra/.ruby-version
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
3.1.2
12 changes: 12 additions & 0 deletions examples/quick-start-ruby-sinatra/Dockerfile
Original file line number Diff line number Diff line change
@@ -0,0 +1,12 @@
FROM ruby:3.1.2-alpine
WORKDIR /usr/src/app

COPY Gemfile Gemfile.lock .
RUN apk update && apk add make gcc musl-dev && bundle install

COPY . .
RUN chmod 666 ./Gemfile.lock

EXPOSE 8080

CMD [ "bundle", "exec", "ruby", "/usr/src/app/api.rb" ]
8 changes: 8 additions & 0 deletions examples/quick-start-ruby-sinatra/Gemfile
Original file line number Diff line number Diff line change
@@ -0,0 +1,8 @@
source "https://rubygems.org"

gem "puma", "~> 6.2.1"
gem "sinatra", "~> 3.0.5"

gem "opentelemetry-sdk", "~> 1.2"
gem "opentelemetry-exporter-otlp", "~> 0.24"
gem "opentelemetry-instrumentation-sinatra", "~> 0.21"
65 changes: 65 additions & 0 deletions examples/quick-start-ruby-sinatra/Gemfile.lock
Original file line number Diff line number Diff line change
@@ -0,0 +1,65 @@
GEM
remote: https://rubygems.org/
specs:
google-protobuf (3.22.2-x86_64-darwin)
googleapis-common-protos-types (1.5.0)
google-protobuf (~> 3.14)
mustermann (3.0.0)
ruby2_keywords (~> 0.0.1)
nio4r (2.5.9)
opentelemetry-api (1.1.0)
opentelemetry-common (0.19.6)
opentelemetry-api (~> 1.0)
opentelemetry-exporter-otlp (0.24.0)
google-protobuf (~> 3.19)
googleapis-common-protos-types (~> 1.3)
opentelemetry-api (~> 1.1)
opentelemetry-common (~> 0.19.6)
opentelemetry-sdk (~> 1.2)
opentelemetry-semantic_conventions
opentelemetry-instrumentation-base (0.21.1)
opentelemetry-api (~> 1.0)
opentelemetry-registry (~> 0.1)
opentelemetry-instrumentation-rack (0.22.1)
opentelemetry-api (~> 1.0)
opentelemetry-common (~> 0.19.3)
opentelemetry-instrumentation-base (~> 0.21.0)
opentelemetry-instrumentation-sinatra (0.21.5)
opentelemetry-api (~> 1.0)
opentelemetry-common (~> 0.19.3)
opentelemetry-instrumentation-base (~> 0.21.0)
opentelemetry-instrumentation-rack (~> 0.21)
opentelemetry-registry (0.2.0)
opentelemetry-api (~> 1.1)
opentelemetry-sdk (1.2.0)
opentelemetry-api (~> 1.1)
opentelemetry-common (~> 0.19.3)
opentelemetry-registry (~> 0.2)
opentelemetry-semantic_conventions
opentelemetry-semantic_conventions (1.8.0)
opentelemetry-api (~> 1.0)
puma (6.2.1)
nio4r (~> 2.0)
rack (2.2.6.4)
rack-protection (3.0.5)
rack
ruby2_keywords (0.0.5)
sinatra (3.0.5)
mustermann (~> 3.0)
rack (~> 2.2, >= 2.2.4)
rack-protection (= 3.0.5)
tilt (~> 2.0)
tilt (2.1.0)

PLATFORMS
x86_64-darwin-21

DEPENDENCIES
opentelemetry-exporter-otlp (~> 0.24)
opentelemetry-instrumentation-sinatra (~> 0.21)
opentelemetry-sdk (~> 1.2)
puma (~> 6.2.1)
sinatra (~> 3.0.5)

BUNDLED WITH
2.3.12
9 changes: 9 additions & 0 deletions examples/quick-start-ruby-sinatra/README.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,9 @@
# Quick Start - Ruby app with OpenTelemetry and Tracetest

> [Read the detailed recipe for setting up OpenTelemetry Collector with Tractest in our documentation.](https://docs.tracetest.io/examples-tutorials/recipes/running-tracetest-without-a-trace-data-store)
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Do we need to update this before merging? Are you planning to add the recipe documentation afterward?

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

🤔 , I'm using the same description we used on Node.js, Python, and Golang READMEs.
We can review these descriptions and write a better explanation for them in other PR. Let's talk with @adnanrahic about this.


This is a simple quick start on how to configure a Ruby app (with [Sinatra](https://sinatrarb.com/) framework) to use OpenTelemetry instrumentation with traces, and Tracetest for enhancing your e2e and integration tests with trace-based testing.

To run it just execute `docker compose up` on this folder.

Feel free to check out the [docs](https://docs.tracetest.io/), and join our [Discord Community](https://discord.gg/8MtcMrQNbX) for more info!
19 changes: 19 additions & 0 deletions examples/quick-start-ruby-sinatra/api.rb
Original file line number Diff line number Diff line change
@@ -0,0 +1,19 @@
require "sinatra"

require "opentelemetry/sdk"
require "opentelemetry/exporter/otlp"
require "opentelemetry/instrumentation/sinatra"

set :port, 8080

OpenTelemetry::SDK.configure do |c|
c.use "OpenTelemetry::Instrumentation::Sinatra"
end

error do
OpenTelemetry::Trace.current_span.record_exception(env['sinatra.error'])
end

get '/hello' do
'Hello World'
end
59 changes: 59 additions & 0 deletions examples/quick-start-ruby-sinatra/docker-compose.yaml
Original file line number Diff line number Diff line change
@@ -0,0 +1,59 @@
version: '3'
services:
app:
image: quick-start-ruby-sinatra
platform: linux/amd64
build: .
extra_hosts:
- "host.docker.internal:host-gateway"
ports:
- "8080:8080"
environment:
- APP_ENV=production
- OTEL_EXPORTER_OTLP_TRACES_ENDPOINT=http://otel-collector:4318/v1/traces
- OTEL_SERVICE_NAME=app

tracetest:
image: kubeshop/tracetest:latest
platform: linux/amd64
volumes:
- type: bind
source: ./tracetest/tracetest-config.yaml
target: /app/tracetest.yaml
- type: bind
source: ./tracetest/tracetest-provision.yaml
target: /app/provisioning.yaml
ports:
- 11633:11633
command: --provisioning-file /app/provisioning.yaml
depends_on:
postgres:
condition: service_healthy
otel-collector:
condition: service_started
healthcheck:
test: ["CMD", "wget", "--spider", "localhost:11633"]
interval: 1s
timeout: 3s
retries: 60
environment:
TRACETEST_DEV: ${TRACETEST_DEV}

postgres:
image: postgres:14
environment:
POSTGRES_PASSWORD: postgres
POSTGRES_USER: postgres
healthcheck:
test: pg_isready -U "$$POSTGRES_USER" -d "$$POSTGRES_DB"
interval: 1s
timeout: 5s
retries: 60

otel-collector:
image: otel/opentelemetry-collector-contrib:0.59.0
command:
- "--config"
- "/otel-local-config.yaml"
volumes:
- ./tracetest/collector.config.yaml:/otel-local-config.yaml
16 changes: 16 additions & 0 deletions examples/quick-start-ruby-sinatra/test-api.yaml
Original file line number Diff line number Diff line change
@@ -0,0 +1,16 @@
type: Test
spec:
name: Call Ruby API
trigger:
type: http
httpRequest:
url: http://app:8080/hello
method: GET
headers:
- key: Content-Type
value: application/json
specs:
- selector: span[tracetest.span.type="http" name="GET /hello" http.method="GET"]
assertions:
- attr:http.status_code = 200
- attr:tracetest.span.duration < 500ms
26 changes: 26 additions & 0 deletions examples/quick-start-ruby-sinatra/tracetest/collector.config.yaml
Original file line number Diff line number Diff line change
@@ -0,0 +1,26 @@
receivers:
otlp:
protocols:
grpc:
http:

processors:
batch:
timeout: 100ms

exporters:
logging:
loglevel: debug
otlp/1:
endpoint: tracetest:21321
# Send traces to Tracetest.
# Read more in docs here: https://docs.tracetest.io/configuration/connecting-to-data-stores/opentelemetry-collector
tls:
insecure: true

service:
pipelines:
traces/1:
receivers: [otlp]
processors: [batch]
exporters: [otlp/1]
Original file line number Diff line number Diff line change
@@ -0,0 +1,7 @@
postgres:
host: postgres
user: postgres
password: postgres
port: 5432
dbname: postgres
params: sslmode=disable
Original file line number Diff line number Diff line change
@@ -0,0 +1,16 @@
---
type: PollingProfile
spec:
name: Default
strategy: periodic
default: true
periodic:
retryDelay: 5s
timeout: 10m

---
type: DataStore
spec:
name: OpenTelemetry Collector
type: otlp
isdefault: true