diff --git a/CHANGELOG.md b/CHANGELOG.md index 54dfcf0..b0b94e3 100644 --- a/CHANGELOG.md +++ b/CHANGELOG.md @@ -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 diff --git a/README.md b/README.md index 163b3d8..588a7b4 100644 --- a/README.md +++ b/README.md @@ -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 diff --git a/lib/yabeda/dsl/class_methods.rb b/lib/yabeda/dsl/class_methods.rb index 4c4c00e..eb9e765 100644 --- a/lib/yabeda/dsl/class_methods.rb +++ b/lib/yabeda/dsl/class_methods.rb @@ -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) diff --git a/lib/yabeda/tags.rb b/lib/yabeda/tags.rb index 4717dfc..d86e223 100644 --- a/lib/yabeda/tags.rb +++ b/lib/yabeda/tags.rb @@ -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 diff --git a/lib/yabeda/version.rb b/lib/yabeda/version.rb index c2c2abb..9448500 100644 --- a/lib/yabeda/version.rb +++ b/lib/yabeda/version.rb @@ -1,5 +1,5 @@ # frozen_string_literal: true module Yabeda - VERSION = "0.5.0" + VERSION = "0.6.0" end diff --git a/spec/yabeda/tags_spec.rb b/spec/yabeda/tags_spec.rb index 581b548..50f1046 100644 --- a/spec/yabeda/tags_spec.rb +++ b/spec/yabeda/tags_spec.rb @@ -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" } } @@ -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