Skip to content

Commit

Permalink
0.6.0: Allow to temporarily override default tags in runtime
Browse files Browse the repository at this point in the history
  • Loading branch information
Envek committed Jul 15, 2020
1 parent d745cf7 commit cbf4022
Show file tree
Hide file tree
Showing 6 changed files with 55 additions and 4 deletions.
6 changes: 5 additions & 1 deletion CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,11 @@ All notable changes to this project will be documented in this file.
The format is based on [Keep a Changelog](http://keepachangelog.com/en/1.0.0/)
and this project adheres to [Semantic Versioning](http://semver.org/spec/v2.0.0.html).

## not released
## 0.6.0 - 2020-07-15

### Added

- `Yabeda.with_tags` to redefine default tags for limited amount of time–for all metrics measured during a block execution. [@Envek]

### Fixed

Expand Down
5 changes: 5 additions & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -84,6 +84,11 @@ And then execute:
Yabeda.configure do
default_tag :rails_environment, 'production'
end
# You can redefine them for limited amount of time
Yabeda.with_tags(rails_environment: 'staging') do
Yabeda.your_app.bells_rang_count.increment({bell_size: bell.size}, by: 1)
end
```

6. See the docs for the adapter you're using
Expand Down
16 changes: 16 additions & 0 deletions lib/yabeda/dsl/class_methods.rb
Original file line number Diff line number Diff line change
Expand Up @@ -62,6 +62,22 @@ def default_tag(name, value)
::Yabeda.default_tags[name] = value
end

# Redefine default tags for a limited amount of time
# @param tags Hash{Symbol=>#to_s}
def with_tags(**tags)
Thread.current[:yabeda_temporary_tags] = tags
yield
ensure
Thread.current[:yabeda_temporary_tags] = {}
end

# Get tags set by +with_tags+
# @api private
# @return Hash
def temporary_tags
Thread.current[:yabeda_temporary_tags] || {}
end

private

def register_metric(metric)
Expand Down
2 changes: 1 addition & 1 deletion lib/yabeda/tags.rb
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,7 @@ module Yabeda
# Class to merge tags
class Tags
def self.build(tags)
::Yabeda.default_tags.merge(tags)
::Yabeda.default_tags.merge(Yabeda.temporary_tags, tags)
end
end
end
2 changes: 1 addition & 1 deletion lib/yabeda/version.rb
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
# frozen_string_literal: true

module Yabeda
VERSION = "0.5.0"
VERSION = "0.6.0"
end
28 changes: 27 additions & 1 deletion spec/yabeda/tags_spec.rb
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@

RSpec.describe Yabeda::Tags do
describe ".build" do
subject { described_class.build(tags) }
subject(:result) { described_class.build(tags) }

let(:tags) { { controller: "foo" } }

Expand Down Expand Up @@ -31,5 +31,31 @@

it { is_expected.to eq(controller: "foo") }
end

context "when default tags and temporary tags are set" do
before do
Yabeda.configure do
default_tag :controller, "default"
default_tag :action, "whatever"
default_tag :format, "html"
end
Yabeda.configure!
end

let(:tags) { { controller: "foo", id: "100500" } }

it "overwrites default tags but not metric tags", :aggregate_failures do
Yabeda.with_tags(controller: "bar", format: "json", q: "test") do
expect(result).to eq controller: "foo", action: "whatever", format: "json", id: "100500", q: "test"
end
end

it "don't use temporary tags outside with_tags block" do
Yabeda.with_tags(controller: "bar", format: "json", q: "test") do
# What happened in with_tags stays in with_tags
end
expect(result).to eq controller: "foo", action: "whatever", format: "html", id: "100500"
end
end
end
end

0 comments on commit cbf4022

Please sign in to comment.