Skip to content
Snippets Groups Projects
Commit 21f92692 authored by Mike Gane's avatar Mike Gane
Browse files

chore: add rack middleware for tracking response status codes

parent 275a22ce
No related branches found
No related tags found
1 merge request!7chore: add rack middleware for tracking response status codes
Pipeline #88435 passed
......@@ -10,6 +10,7 @@ As well as providing a semantic logger, this gem handles datadog telemetry assoc
* traces
* metrics
* statsd is automatically attached to datadog runtime metrics and may also be used for custom metrics.
* `ResponseCodeStatsMiddleware` is included to capture response code metrics from rack applications
### Customise log level per logger
......@@ -60,6 +61,13 @@ For example, to increment a count:
NexusSemanticLogger.metrics.increment('nexus.users.registration.complete')
```
### Rack response code metrics
This can be configured with a middleware in application.rb
```
config.middleware.use ResponseCodeStatsMiddleware
```
# Local gem development
Steps to run this gem from local sources in one the nexus 'staged build' rails components:
......
......@@ -164,6 +164,10 @@ Puma::Plugin.create do
NexusSemanticLogger.metrics.gauge('puma.pool_capacity', stats.pool_capacity, tags: tags)
NexusSemanticLogger.metrics.gauge('puma.max_threads', stats.max_threads, tags: tags)
NexusSemanticLogger.metrics.gauge('puma.requests_count', stats.requests_count, tags: tags)
ResponseCodeStatsMiddleware.read_and_reset_metrics.each do |code, count|
NexusSemanticLogger.metrics.gauge("puma.response_code.#{code}", count, tags: tags)
end
rescue StandardError => e
@log_writer.unknown_error(e, nil, '! statsd: notify stats failed')
ensure
......
# frozen_string_literal: true
# noinspection RubyClassVariableUsageInspection
class ResponseCodeStatsMiddleware
def initialize(app)
@app = app
@@code_metrics = {}
end
def call(env)
status, headers, response = @app.call(env)
@@code_metrics[status] ||= 0
@@code_metrics[status] += 1
[status, headers, response]
end
def self.read_and_reset_metrics
metrics = @@code_metrics.dup
@@code_metrics.clear
metrics
end
end
0% Loading or .
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment