Skip to content

Commit ed2e135

Browse files
Add OpenTelemetry Tracing capabilities to Cloud Controller
In this change we implement Opentelemetry(OTel) tracing support for the ruby cloud_controller using the official(by OTel community) library opentelemetry-ruby. It enables the cloud_controller itself to measure traces and spans and send them to a OpenTelemetryProtocol(OTLP) capable backend. The recorded root span is stored in the with the service-name "cloud_controller_ng-api" or "cloud_controller_ng-worker". A trace of an api call that starts a delayed_job will cause a link between this api call and the trace of the worker. The worker inherits the sampling decision of the api call too, meaning a sampled api call will also lead to a sampled job run. The service name can be used to find the trace in the attached backend. Job samples and Api samples will have a bidirectional link. When disabled the code that load and initialise the framework as well as the middleware are not used to minimise impact on performance and bug surface. Instrumentations: The opentelemetry-ruby library offers a few out of the box instrumentations which extend key function calls in libraries automatically once loaded and measure span.
The used instrumentations and their respective roles include: - HttpClient: This instrumentation tracks outgoing HTTP requests executed with the httpclient gem. - HTTP: This instrumentations traces the HTTP client and server-side libraries. - Mysql2: This instrumentation detects queries, database calls, transactions, etc., made to MySQL using the Mysql2 gem. - Redis: This instrumentation traces all commands sent to Redis server using the redis-rb gem. - Rake: This instrumentation measures the execution time of your Rake tasks. - CCDelayedJob: This instrumentation traces the enqueue/job execute cycle within delayed jobs. This is a modified version of the DelayedJob instrumentation that propagates trace information so that bidirectional links can be set. It also records some cloud_controller specific job attributes when they are available. - PG (Postgres): Postgres instrumentation captures SQL queries executed on a Postgres database. The instrumentations automatically set spans into traces and spans, which are set manually in the code(OpenTelemetryMiddleware) to track specific activities. Manual spans include: - middleware-pre-app: This span measures the middleware before the application runs. - application: This span measures the application runtime(without middleware). Configuration To properly configure this functionality following properties will be introduced: - [otel][tracing][enabled]: Set this to true or false. If it's false, all functionality regarding OpenTelemetry Tracing is disabled. - [otel][tracing][api_url]: Set the URL for the API. - [otel][tracing][api_token]: Set the token for the API - [otel][tracing][redact][db_statemets]: Set this to true or false. If false the full sql statement will be provided as attribute in the spans of the mysql or pg instrumentation. If set to true, it will log the statements but will redact the query parameters as well as insert/update values by question marks. - [otel][tracing][sampling_ratio]: Set a float between 0.0 (=0%) and 1.0 (=100%) to define the probability with that traces are sampled. - [otel][tracing][propagation][accept_sampling_instruction] Set this to true or false. If true, the header passing the sampling value to the cloud_controller will be honoured and make the cloud_controller to also trace. If set to false the tracing information is parsed but it does not influence the sampling decision based on above sampling_ratio. - [otel][tracing][propagation][extractors]: Here you can set the list of propagators to be extracted. Extractors read the HTTP headers provided and deserialize the contained tracing information. - [otel][tracing][propagation][injectors]: Here you can set the list of propagators to be injected. Injectors serialize the tracing information into HTTP headers for propagation. Propagation Propagation refers to the process of moving tracing data using HTTP headers across service boundaries that can be linked together into a single trace. Propagators accepted are: - tracecontext(https://www.w3.org/TR/trace-context/) - baggage(https://www.w3.org/TR/baggage/) - b3(https://github.com/openzipkin/b3-propagation) - b3multi(https://github.com/openzipkin/b3-propagation) - jaeger(https://www.jaegertracing.io/docs/1.56/client-libraries/#propagation-format) - xray(https://docs.aws.amazon.com/xray/latest/devguide/aws-xray.html#xray-concepts-tracingheader) - none Propagators can be differentiated into extractors and injectors. Extractors are responsible for extracting trace context from incoming requests(incoming api calls), while injectors are responsible for injecting trace context into outgoing requests(e.g. to UAA or service brokers). If more than one injector is configured in the HTTP Request, than the last propagator that can successfully process the supplied headers defines the inherited trace context, in case multiple conflicting trace headers are recieved. If more than on extractor is configured, the CC will propagate the information in all configured formats. The propagators tracecontext and jaeger also support baggage. With baggage key-value pairs can be added to the headers. If more than one propagation, which supports baggage, is present, the first baggage key-value pairs are being used. The propagation functionality hereby replaces the zipkin middleware wich was removed, since it implemented the propagation(only, without the cloud_controller adding trace data) for the B3Multi Header exclusively. The current implementation with OTel can be configured to offer the exact same functionality/behaviour. Co-Authored-By: FlorianBraun <[email protected]>
1 parent 4f08222 commit ed2e135

Some content is hidden

Large Commits have some content hidden by default. Use the searchbox below for content that may be hidden.

42 files changed

+1751
-358
lines changed

Gemfile

Lines changed: 14 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -68,6 +68,19 @@ gem 'fog-openstack'
6868
gem 'cf-uaa-lib', '~> 4.0.4'
6969
gem 'vcap-concurrency', git: 'https://github.com/cloudfoundry/vcap-concurrency.git', ref: '2a5b0179'
7070

71+
gem 'opentelemetry-exporter-otlp', '~> 0.26.1'
72+
gem 'opentelemetry-instrumentation-delayed_job', '~> 0.22.1'
73+
gem 'opentelemetry-instrumentation-http_client', '~> 0.22.3'
74+
gem 'opentelemetry-instrumentation-mysql2', '~> 0.27.0'
75+
gem 'opentelemetry-instrumentation-net_http', '~> 0.22.4'
76+
gem 'opentelemetry-instrumentation-pg', '~> 0.27.1'
77+
gem 'opentelemetry-instrumentation-rake', '~> 0.2.1'
78+
gem 'opentelemetry-instrumentation-redis', '~> 0.25.3'
79+
gem 'opentelemetry-propagator-b3'
80+
gem 'opentelemetry-propagator-jaeger', '~> 0.21.0'
81+
gem 'opentelemetry-propagator-xray', '~> 0.22.1'
82+
gem 'opentelemetry-sdk', '~> 1.3'
83+
7184
group :db do
7285
gem 'mysql2', '~> 0.5.6'
7386
gem 'pg'
@@ -84,6 +97,7 @@ group :test do
8497
gem 'rack-test'
8598
gem 'rspec', '~> 3.13.0'
8699
gem 'rspec_api_documentation', '>= 6.1.0'
100+
gem 'rspec-benchmark', '~> 0.6.0'
87101
gem 'rspec-collection_matchers'
88102
gem 'rspec-instafail'
89103
gem 'rspec-its'

Gemfile.lock

Lines changed: 81 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -110,6 +110,9 @@ GEM
110110
base64 (0.2.0)
111111
beefcake (1.0.0)
112112
benchmark (0.3.0)
113+
benchmark-malloc (0.2.0)
114+
benchmark-perf (0.6.0)
115+
benchmark-trend (0.4.0)
113116
bigdecimal (3.1.7)
114117
bit-struct (0.17)
115118
builder (3.2.4)
@@ -351,6 +354,66 @@ GEM
351354
oj (3.16.3)
352355
bigdecimal (>= 3.0)
353356
openssl (3.2.0)
357+
opentelemetry-api (1.2.5)
358+
opentelemetry-common (0.20.1)
359+
opentelemetry-api (~> 1.0)
360+
opentelemetry-exporter-otlp (0.26.3)
361+
google-protobuf (~> 3.14)
362+
googleapis-common-protos-types (~> 1.3)
363+
opentelemetry-api (~> 1.1)
364+
opentelemetry-common (~> 0.20)
365+
opentelemetry-sdk (~> 1.2)
366+
opentelemetry-semantic_conventions
367+
opentelemetry-helpers-mysql (0.1.0)
368+
opentelemetry-api (~> 1.0)
369+
opentelemetry-common (~> 0.20)
370+
opentelemetry-helpers-sql-obfuscation (0.1.0)
371+
opentelemetry-common (~> 0.20)
372+
opentelemetry-instrumentation-base (0.22.3)
373+
opentelemetry-api (~> 1.0)
374+
opentelemetry-registry (~> 0.1)
375+
opentelemetry-instrumentation-delayed_job (0.22.1)
376+
opentelemetry-api (~> 1.0)
377+
opentelemetry-instrumentation-base (~> 0.22.1)
378+
opentelemetry-instrumentation-http_client (0.22.3)
379+
opentelemetry-api (~> 1.0)
380+
opentelemetry-common (~> 0.20.0)
381+
opentelemetry-instrumentation-base (~> 0.22.1)
382+
opentelemetry-instrumentation-mysql2 (0.27.0)
383+
opentelemetry-api (~> 1.0)
384+
opentelemetry-helpers-mysql
385+
opentelemetry-helpers-sql-obfuscation
386+
opentelemetry-instrumentation-base (~> 0.22.1)
387+
opentelemetry-instrumentation-net_http (0.22.4)
388+
opentelemetry-api (~> 1.0)
389+
opentelemetry-common (~> 0.20.0)
390+
opentelemetry-instrumentation-base (~> 0.22.1)
391+
opentelemetry-instrumentation-pg (0.27.1)
392+
opentelemetry-api (~> 1.0)
393+
opentelemetry-helpers-sql-obfuscation
394+
opentelemetry-instrumentation-base (~> 0.22.1)
395+
opentelemetry-instrumentation-rake (0.2.1)
396+
opentelemetry-api (~> 1.0)
397+
opentelemetry-instrumentation-base (~> 0.22.1)
398+
opentelemetry-instrumentation-redis (0.25.3)
399+
opentelemetry-api (~> 1.0)
400+
opentelemetry-common (~> 0.20.0)
401+
opentelemetry-instrumentation-base (~> 0.22.1)
402+
opentelemetry-propagator-b3 (0.21.0)
403+
opentelemetry-api (~> 1.1)
404+
opentelemetry-propagator-jaeger (0.21.1)
405+
opentelemetry-api (~> 1.1)
406+
opentelemetry-propagator-xray (0.22.1)
407+
opentelemetry-api (~> 1.0)
408+
opentelemetry-registry (0.3.0)
409+
opentelemetry-api (~> 1.1)
410+
opentelemetry-sdk (1.4.0)
411+
opentelemetry-api (~> 1.1)
412+
opentelemetry-common (~> 0.20)
413+
opentelemetry-registry (~> 0.2)
414+
opentelemetry-semantic_conventions
415+
opentelemetry-semantic_conventions (1.10.0)
416+
opentelemetry-api (~> 1.0)
354417
os (1.1.4)
355418
palm_civet (1.1.0)
356419
parallel (1.24.0)
@@ -435,6 +498,11 @@ GEM
435498
rspec-core (~> 3.13.0)
436499
rspec-expectations (~> 3.13.0)
437500
rspec-mocks (~> 3.13.0)
501+
rspec-benchmark (0.6.0)
502+
benchmark-malloc (~> 0.2)
503+
benchmark-perf (~> 0.6)
504+
benchmark-trend (~> 0.4)
505+
rspec (>= 3.0)
438506
rspec-collection_matchers (1.2.1)
439507
rspec-expectations (>= 2.99.0.beta1)
440508
rspec-core (3.13.0)
@@ -635,6 +703,18 @@ DEPENDENCIES
635703
nokogiri (>= 1.10.5)
636704
oj
637705
openssl (>= 3.2)
706+
opentelemetry-exporter-otlp (~> 0.26.1)
707+
opentelemetry-instrumentation-delayed_job (~> 0.22.1)
708+
opentelemetry-instrumentation-http_client (~> 0.22.3)
709+
opentelemetry-instrumentation-mysql2 (~> 0.27.0)
710+
opentelemetry-instrumentation-net_http (~> 0.22.4)
711+
opentelemetry-instrumentation-pg (~> 0.27.1)
712+
opentelemetry-instrumentation-rake (~> 0.2.1)
713+
opentelemetry-instrumentation-redis (~> 0.25.3)
714+
opentelemetry-propagator-b3
715+
opentelemetry-propagator-jaeger (~> 0.21.0)
716+
opentelemetry-propagator-xray (~> 0.22.1)
717+
opentelemetry-sdk (~> 1.3)
638718
palm_civet
639719
parallel_tests
640720
pg
@@ -650,6 +730,7 @@ DEPENDENCIES
650730
rfc822
651731
roodi
652732
rspec (~> 3.13.0)
733+
rspec-benchmark (~> 0.6.0)
653734
rspec-collection_matchers
654735
rspec-instafail
655736
rspec-its

app/jobs/cc_job.rb

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,8 @@
11
module VCAP::CloudController
22
module Jobs
33
class CCJob
4+
attr_accessor :otel_tracing_carrier
5+
46
def reschedule_at(time, attempts)
57
time + (attempts**4) + 5
68
end

app/jobs/enqueuer.rb

Lines changed: 11 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -5,6 +5,7 @@
55
require 'jobs/logging_context_job'
66
require 'jobs/timeout_job'
77
require 'securerandom'
8+
require 'opentelemetry/sdk'
89

910
module VCAP::CloudController
1011
module Jobs
@@ -26,8 +27,12 @@ def enqueue_pollable(existing_guid: nil)
2627

2728
wrapped_job = yield wrapped_job if block_given?
2829

29-
delayed_job = enqueue_job(wrapped_job)
30-
PollableJobModel.find_by_delayed_job(delayed_job)
30+
tracer = OpenTelemetry.tracer_provider.tracer(self.class.name, '1.0.0')
31+
job_handler = wrapped_job.handler.class.name.respond_to?(:split) ? wrapped_job.handler.class.name.split('::').last : wrapped_job.handler.class.name
32+
tracer.in_span("enqueue-job: #{job_handler}", kind: :producer) do
33+
delayed_job = enqueue_job(wrapped_job)
34+
PollableJobModel.find_by_delayed_job(delayed_job)
35+
end
3136
end
3237

3338
def run_inline
@@ -42,7 +47,10 @@ def enqueue_job(job)
4247
@opts['guid'] = SecureRandom.uuid
4348
request_id = ::VCAP::Request.current_id
4449
timeout_job = TimeoutJob.new(job, job_timeout)
45-
logging_context_job = LoggingContextJob.new(timeout_job, request_id)
50+
logging_context_job = LoggingContextJob.new(
51+
timeout_job,
52+
request_id
53+
)
4654
@opts[:priority] = job_priority unless @opts[:priority] || job_priority.nil?
4755
Delayed::Job.enqueue(logging_context_job, @opts)
4856
end

app/jobs/logging_context_job.rb

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,6 @@
11
require 'jobs/wrapping_job'
22
require 'presenters/error_presenter'
3+
require 'opentelemetry/sdk'
34

45
module VCAP::CloudController
56
module Jobs

app/jobs/wrapping_job.rb

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,9 +1,11 @@
11
require 'jobs/cc_job'
2+
require 'opentelemetry-sdk'
23

34
module VCAP::CloudController
45
module Jobs
56
class WrappingJob < VCAP::CloudController::Jobs::CCJob
67
attr_reader :handler
8+
attr_accessor :otel_api_trace_carrier, :otel_job_trace_carrier
79

810
def initialize(handler)
911
@handler = handler

config/cloud_controller.yml

Lines changed: 13 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -401,3 +401,16 @@ custom_metric_tag_prefix_list: ["metric.tag.cloudfoundry.org"]
401401

402402
max_manifest_service_binding_poll_duration_in_seconds: 60
403403
update_metric_tags_on_rename: true
404+
405+
otel:
406+
tracing:
407+
enabled: false
408+
api_url: ''
409+
api_token: ''
410+
sampling_ratio: 1.0
411+
redact:
412+
db_statements: true
413+
propagation:
414+
accept_sampling_instruction: true
415+
extractors: ['tracecontext', 'xray', 'jaeger', 'baggage', 'b3', 'b3multi']
416+
injectors: ['tracecontext', 'xray', 'jaeger', 'baggage', 'b3', 'b3multi']

config/initializers/cloudfront_signer.rb

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,7 @@
11
require 'cloudfront-signer'
22

33
module CCInitializers
4-
def self.cloudfront_signer(cc_config)
4+
def self.cloudfront_signer(cc_config, _)
55
return if cc_config[:droplets].blank?
66

77
cdn_config = cc_config[:droplets][:cdn]

config/initializers/delayed_job.rb

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
module CCInitializers
2-
def self.delayed_job(_)
2+
def self.delayed_job(_, _)
33
::Delayed::Worker.backend = :sequel
44
end
55
end

config/initializers/honeycomb.rb

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,7 @@
11
require 'honeycomb-beeline'
22

33
module CCInitializers
4-
def self.honeycomb(cc_config)
4+
def self.honeycomb(cc_config, _)
55
return unless cc_config[:honeycomb]
66

77
Honeycomb.configure do |hc|

0 commit comments

Comments
 (0)