-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Browse files
Browse the repository at this point in the history
* Updated API tests to verify they are instrumented. * Added correlation and request id to requests * Updated dependencies.
- Loading branch information
1 parent
06b4c4f
commit aae4fde
Showing
20 changed files
with
567 additions
and
42 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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,7 +1,38 @@ | ||
# frozen_string_literal: true | ||
|
||
require 'opentelemetry/sdk' | ||
require 'opentelemetry-exporter-otlp' | ||
require 'opentelemetry/instrumentation/faraday' | ||
require 'opentelemetry/instrumentation/grape' | ||
require 'opentelemetry/instrumentation/rack' | ||
require 'semantic_logger' | ||
|
||
require_relative 'lib/document_transfer' | ||
require_relative 'lib/api/api' | ||
require_relative 'lib/api/middleware/correlation_id' | ||
require_relative 'lib/api/middleware/instrument' | ||
require_relative 'lib/api/middleware/request_id' | ||
require_relative 'lib/api/middleware/request_logging' | ||
|
||
# Configure the logger. | ||
SemanticLogger.default_level = ENV.fetch('LOG_LEVEL', | ||
DocumentTransfer::DEFAULT_LOG_LEVEL) | ||
SemanticLogger.application = DocumentTransfer::NAME | ||
SemanticLogger.add_appender(io: $stdout, formatter: :json) | ||
|
||
# Configure telemetry reporting. | ||
OpenTelemetry::SDK.configure do |c| | ||
c.service_name = DocumentTransfer::NAME | ||
c.service_version = DocumentTransfer::VERSION | ||
c.use_all | ||
end | ||
|
||
# Include Rack middleware. | ||
use Rack::RewindableInput::Middleware | ||
use DocumentTransfer::API::Middleware::RequestId | ||
use DocumentTransfer::API::Middleware::CorrelationId | ||
use(*OpenTelemetry::Instrumentation::Rack::Instrumentation.instance.middleware_args) | ||
use DocumentTransfer::API::Middleware::RequestLogging | ||
use DocumentTransfer::API::Middleware::Instrument | ||
|
||
run DocumentTransfer::API::API |
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,50 @@ | ||
# frozen_string_literal: true | ||
|
||
module DocumentTransfer | ||
module API | ||
module Middleware | ||
# Rack middleware that adds a correlation id to requests that don't | ||
# include one. | ||
# | ||
# This middleware must be loaded before any middleware that may make use | ||
# of the correlation id (such as logging). | ||
class CorrelationId | ||
HEADER = 'x-correlation-id' | ||
KEY = 'HTTP_X_CORRELATION_ID' | ||
|
||
# Initialize the middleware | ||
# | ||
# @param [Rack::Events] app The Rack application. | ||
def initialize(app) | ||
@app = app | ||
end | ||
|
||
# Ensure that the request has a correlation id. | ||
# | ||
# @param [Hash] env The environment hash. | ||
# @return [Array<Integer, Rack::Headers, Rack::BodyProxy] The response | ||
# for the request. | ||
def call(env) | ||
# Prefer the existing correlation id, if it exists. | ||
correlation_id = env.fetch(KEY, generate) | ||
env[KEY] ||= correlation_id | ||
|
||
# Add the correlation id to to the response headers. | ||
status, headers, body = @app.call(env) | ||
headers[HEADER] = correlation_id | ||
|
||
[status, headers, body] | ||
end | ||
|
||
private | ||
|
||
# Generates a random correlation id. | ||
# | ||
# @return [String] | ||
def generate | ||
SecureRandom.uuid | ||
end | ||
end | ||
end | ||
end | ||
end |
Oops, something went wrong.