-
Notifications
You must be signed in to change notification settings - Fork 2
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Add Passthrough transform and formatter
These are useful for custom targets that might not need/want any transforms for formatting, or to mix-and-match. For example, image a custom `LogTarget` that can be configured with a `:logger` option. It could be that the logger itself already knows how to format they telemetry data, (e.g., into key/value pairs), and so we don't want/need the Target to do any formatting. This allows this plugin's config to look like this: ```ruby Puma::Plugin::Telemetry.configure do |config| config.add_target(CustomLogTarget, logger: Logfmt.logger, transform: :cloud_watch, formatter: :noop) end ```
- Loading branch information
1 parent
2a4a191
commit b95b291
Showing
8 changed files
with
116 additions
and
15 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
16 changes: 16 additions & 0 deletions
16
lib/puma/plugin/telemetry/formatters/passthrough_formatter.rb
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,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 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 | ||
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 | ||
|
||
private | ||
|
||
attr_reader :formatter, :transform | ||
end | ||
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
16 changes: 16 additions & 0 deletions
16
lib/puma/plugin/telemetry/transforms/passthrough_transform.rb
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,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 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
20 changes: 20 additions & 0 deletions
20
spec/puma/plugin/telemetry/formatters/passthrough_formatter_spec.rb
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,20 @@ | ||
# frozen_string_literal: true | ||
|
||
module Puma | ||
class Plugin | ||
module Telemetry | ||
module Formatters | ||
RSpec.describe PassthroughFormatter do | ||
subject(:formatter) { described_class } | ||
|
||
it 'returns the telemetry, unalterted' do | ||
telmetry_data = { 'foo' => 'bar' } | ||
formatted_data = formatter.call(telmetry_data) | ||
|
||
expect(formatted_data).to eq(telmetry_data) | ||
end | ||
end | ||
end | ||
end | ||
end | ||
end |
20 changes: 20 additions & 0 deletions
20
spec/puma/plugin/telemetry/transforms/passthrough_transform_spec.rb
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,20 @@ | ||
# frozen_string_literal: true | ||
|
||
module Puma | ||
class Plugin | ||
module Telemetry | ||
module Transforms | ||
RSpec.describe PassthroughTransform do | ||
subject(:transform) { described_class } | ||
|
||
it 'returns the telemetry, unalterted' do | ||
telmetry_data = { 'foo' => 'bar' } | ||
transformed_data = transform.call(telmetry_data) | ||
|
||
expect(transformed_data).to eq(telmetry_data) | ||
end | ||
end | ||
end | ||
end | ||
end | ||
end |