-
Notifications
You must be signed in to change notification settings - Fork 7
Expose new transforms and formatters design #25
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
Merged
amrrbakry
merged 7 commits into
babbel:main
from
stevenharman:expose_transforms_and_formatters
Aug 5, 2026
Merged
Changes from 4 commits
Commits
Show all changes
7 commits
Select commit
Hold shift + click to select a range
fb36fd5
Move JSONFormatter up a level
stevenharman be89b1a
Extract "transform" concept
stevenharman f3208ff
Add Passthrough transform and formatter
stevenharman f815fd8
Document new formatter and transform options
stevenharman 4d54318
Merge remote-tracking branch 'origin/main' into expose_transforms_and…
amrrbakry 0176bc3
refactor: address review feedback
amrrbakry 4a258f1
test: cover passthrough formatter option
amrrbakry File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or 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 hidden or 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 hidden or 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,18 @@ | ||
| # frozen_string_literal: true | ||
|
|
||
| require 'json' | ||
|
|
||
| module Puma | ||
| class Plugin | ||
| module Telemetry | ||
| module Formatters | ||
| # JSON formatter, expects `call` method accepting telemetry hash | ||
| class JSONFormatter | ||
| def self.call(telemetry) | ||
| ::JSON.dump(telemetry) | ||
| end | ||
| end | ||
| end | ||
| end | ||
| end | ||
| end |
16 changes: 16 additions & 0 deletions
16
lib/puma/plugin/telemetry/formatters/passthrough_formatter.rb
This file contains hidden or 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,16 @@ | ||
| # frozen_string_literal: true | ||
|
|
||
| module Puma | ||
| class Plugin | ||
| module Telemetry | ||
| module Formatters | ||
| # A passthrough formatter - it returns the telemetry Hash it was given | ||
| class PassthroughFormatter | ||
| def self.call(telemetry) | ||
| telemetry | ||
| end | ||
| end | ||
| end | ||
| end | ||
| end | ||
| end |
38 changes: 38 additions & 0 deletions
38
lib/puma/plugin/telemetry/targets/base_formatting_target.rb
This file contains hidden or 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,38 @@ | ||
| # frozen_string_literal: true | ||
|
|
||
| require_relative '../formatters/json_formatter' | ||
| require_relative '../formatters/passthrough_formatter' | ||
| require_relative '../transforms/cloud_watch_transform' | ||
| require_relative '../transforms/passthrough_transform' | ||
|
|
||
| module Puma | ||
| class Plugin | ||
| module Telemetry | ||
| module Targets | ||
| # A base class for other Targets concerned with formatting telemetry | ||
| class BaseFormattingTarget | ||
| def initialize(formatter: :json, transform: :cloud_watch) | ||
| @transform = case transform | ||
| when :cloud_watch then Transforms::CloudWatchTranform | ||
| when :passthrough then Transforms::PassthroughTransform | ||
|
amrrbakry marked this conversation as resolved.
|
||
| else transform | ||
| end | ||
| @formatter = case formatter | ||
| when :json then Formatters::JSONFormatter | ||
| when :passthrough then Formatters::PassthroughFormatter | ||
| else formatter | ||
| end | ||
| end | ||
|
|
||
| def call(_telemetry) | ||
| raise "#{__method__} must be implemented by #{self.class.name}" | ||
| end | ||
|
amrrbakry marked this conversation as resolved.
|
||
|
|
||
| private | ||
|
|
||
| attr_reader :formatter, :transform | ||
| end | ||
| end | ||
| end | ||
| end | ||
| end | ||
This file contains hidden or 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
23 changes: 23 additions & 0 deletions
23
lib/puma/plugin/telemetry/transforms/cloud_watch_transform.rb
This file contains hidden or 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,23 @@ | ||
| # frozen_string_literal: true | ||
|
|
||
| require 'json' | ||
|
amrrbakry marked this conversation as resolved.
Outdated
|
||
|
|
||
| module Puma | ||
| class Plugin | ||
| module Telemetry | ||
| module Transforms | ||
| # Replace dots with dashes for better support of AWS CloudWatch Log | ||
| # Metric filters, as they don't support dots in key names. | ||
| # Expects `call` method accepting telemetry Hash | ||
| class CloudWatchTranform | ||
|
amrrbakry marked this conversation as resolved.
Outdated
|
||
| def self.call(telemetry) | ||
| telemetry.transform_keys { |k| String(k).tr('.', '-') }.tap do |data| | ||
| data['name'] = 'Puma::Plugin::Telemetry' | ||
| data['message'] = 'Publish telemetry' | ||
| end | ||
| end | ||
| end | ||
| end | ||
| end | ||
| end | ||
| end | ||
16 changes: 16 additions & 0 deletions
16
lib/puma/plugin/telemetry/transforms/passthrough_transform.rb
This file contains hidden or 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,16 @@ | ||
| # frozen_string_literal: true | ||
|
|
||
| module Puma | ||
| class Plugin | ||
| module Telemetry | ||
| module Transforms | ||
| # A passthrough transform - it returns the telemetry Hash it was given | ||
| class PassthroughTransform | ||
| def self.call(telemetry) | ||
| telemetry | ||
| end | ||
| end | ||
| end | ||
| end | ||
| end | ||
| end |
This file contains hidden or 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
27 changes: 27 additions & 0 deletions
27
spec/puma/plugin/telemetry/formatters/json_formatter_spec.rb
This file contains hidden or 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,27 @@ | ||
| # frozen_string_literal: true | ||
|
|
||
| module Puma | ||
| class Plugin | ||
| module Telemetry | ||
| module Formatters | ||
| RSpec.describe JSONFormatter do | ||
| subject(:formatter) { described_class } | ||
|
|
||
| it 'formats the telemetry as a JSON string' do | ||
| string = formatter.call('foo' => 'bar') | ||
|
|
||
| data = ::JSON.parse(string) | ||
| expect(data.fetch('foo')).to eq('bar') | ||
| end | ||
|
|
||
| it 'handles symbol keys' do | ||
| string = formatter.call(foo: 'bar') | ||
|
|
||
| data = ::JSON.parse(string) | ||
| expect(data.fetch('foo')).to eq('bar') | ||
| end | ||
| end | ||
| end | ||
| end | ||
| end | ||
| end |
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
Uh oh!
There was an error while loading. Please reload this page.