-
Notifications
You must be signed in to change notification settings - Fork 362
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
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]>
- Loading branch information
1 parent
4f08222
commit ed2e135
Showing
42 changed files
with
1,751 additions
and
358 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,5 +1,5 @@ | ||
module CCInitializers | ||
def self.delayed_job(_) | ||
def self.delayed_job(_, _) | ||
::Delayed::Worker.backend = :sequel | ||
end | ||
end |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,94 @@ | ||
require 'opentelemetry/sdk' | ||
require 'opentelemetry/exporter/otlp' | ||
require 'opentelemetry/instrumentation/http_client' | ||
require 'opentelemetry/instrumentation/net/http' | ||
require 'opentelemetry/instrumentation/mysql2' | ||
require 'opentelemetry/instrumentation/redis' | ||
require 'opentelemetry/instrumentation/rake' | ||
require 'opentelemetry/instrumentation/pg' | ||
require 'opentelemetry/propagator/jaeger' | ||
require 'opentelemetry/propagator/xray' | ||
require 'delayed_job/opentelemetry/instrumentation' | ||
|
||
module CCInitializers | ||
def self.opentelemetry(cc_config, context) | ||
return unless cc_config.dig(:otel, :tracing, :enabled) | ||
|
||
OpenTelemetry.logger = Steno.logger(context == :api ? 'cc.api.opentelemetry' : 'cc.background.opentelemetry') | ||
|
||
trace_api_url = cc_config[:otel][:tracing][:api_url] | ||
trace_api_token = cc_config[:otel][:tracing][:api_token] | ||
sampler = OpenTelemetry::SDK::Trace::Samplers.trace_id_ratio_based(cc_config[:otel][:tracing][:sampling_ratio]) | ||
sampler = OpenTelemetry::SDK::Trace::Samplers.parent_based(root: sampler) if cc_config.dig(:otel, :tracing, :propagation, :accept_sampling_instruction) | ||
|
||
OpenTelemetry::SDK.configure do |c| | ||
c.add_span_processor( | ||
OpenTelemetry::SDK::Trace::Export::BatchSpanProcessor.new( | ||
OpenTelemetry::Exporter::OTLP::Exporter.new( | ||
endpoint: trace_api_url, | ||
headers: { | ||
Authorization: trace_api_token | ||
} | ||
) | ||
) | ||
) | ||
version = { | ||
V3: VCAP::CloudController::Constants::API_VERSION_V3.to_s, | ||
V2: VCAP::CloudController::Constants::API_VERSION.to_s, | ||
OSBAPI: VCAP::CloudController::Constants::OSBAPI_VERSION.to_s | ||
} | ||
# Set the service name to cloud_controller_ng_api if it is the webserver process and to cloud_controller_ng_worker if it is the worker process | ||
resource = OpenTelemetry::SDK::Resources::Resource | ||
conventions = OpenTelemetry::SemanticConventions::Resource | ||
c.resource = resource.create({ | ||
conventions::SERVICE_NAMESPACE => 'cloud_controller_ng', | ||
conventions::SERVICE_NAME => "cloud_controller_ng-#{context}", | ||
conventions::SERVICE_VERSION => version.to_json, | ||
conventions::SERVICE_INSTANCE_ID => Socket.gethostname + ':' + Process.pid.to_s, | ||
conventions::HOST_NAME => Socket.gethostname | ||
}) | ||
|
||
c.use 'OpenTelemetry::Instrumentation::HttpClient' | ||
c.use 'OpenTelemetry::Instrumentation::Net::HTTP' | ||
c.use 'OpenTelemetry::Instrumentation::Redis' | ||
c.use 'OpenTelemetry::Instrumentation::Rake' | ||
c.use 'OpenTelemetry::Instrumentation::CCDelayedJob' | ||
unless defined?(::PG).nil? | ||
c.use 'OpenTelemetry::Instrumentation::PG', { | ||
db_statement: (cc_config[:otel][:tracing][:redact][:db_statement] ? :obfuscate : :include) | ||
} | ||
end | ||
unless defined?(::Mysql2).nil? | ||
c.use 'OpenTelemetry::Instrumentation::Mysql2', { | ||
db_statement: (cc_config[:otel][:tracing][:redact][:db_statement] ? :obfuscate : :include) | ||
} | ||
end | ||
end | ||
|
||
# Configuration of sampling | ||
OpenTelemetry.tracer_provider.sampler = sampler if trace_api_url && !trace_api_url.empty? && !trace_api_token.empty? | ||
|
||
extractors = define_propagators(cc_config.dig(:otel, :tracing, :propagation, :extractors)) | ||
injectors = define_propagators(cc_config.dig(:otel, :tracing, :propagation, :injectors)) | ||
|
||
OpenTelemetry.propagation = OpenTelemetry::Context::Propagation::CompositeTextMapPropagator.compose(injectors:, extractors:) | ||
end | ||
|
||
def self.define_propagators(config_propagators) | ||
config_propagators = ['none'] if config_propagators.nil? | ||
config_propagators.uniq.collect do |propagator| | ||
case propagator | ||
when 'tracecontext' then OpenTelemetry::Trace::Propagation::TraceContext.text_map_propagator | ||
when 'baggage' then OpenTelemetry::Baggage::Propagation.text_map_propagator | ||
when 'b3' then OpenTelemetry::Propagator::B3::Single.text_map_propagator | ||
when 'b3multi' then OpenTelemetry::Propagator::B3::Multi.text_map_propagator | ||
when 'jaeger' then OpenTelemetry::Propagator::Jaeger.text_map_propagator | ||
when 'xray' then OpenTelemetry::Propagator::XRay.text_map_propagator | ||
when 'none' then OpenTelemetry::SDK::Configurator::NoopTextMapPropagator.new | ||
else | ||
OpenTelemetry.logger.warn "The #{propagator} propagator is unknown and cannot be configured" | ||
OpenTelemetry::SDK::Configurator::NoopTextMapPropagator.new | ||
end | ||
end | ||
end | ||
end |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.