Skip to content

Commit 3977a4d

Browse files
committed
Add default_labels config option
1 parent 68d084e commit 3977a4d

File tree

5 files changed

+23
-0
lines changed

5 files changed

+23
-0
lines changed

google-cloud-trace/lib/google-cloud-trace.rb

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -152,4 +152,5 @@ def self.trace project_id = nil,
152152
config.add_field! :notifications, nil, match: Array
153153
config.add_field! :max_data_length, nil, match: Integer
154154
config.add_field! :on_error, nil, match: Proc
155+
config.add_field! :default_labels, {}, match: Hash
155156
end

google-cloud-trace/lib/google/cloud/trace.rb

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -144,6 +144,8 @@ def self.new project_id: nil,
144144
# * `on_error` - (Proc) A Proc to be run when an error is encountered
145145
# during the reporting of traces by the middleware. The Proc must take
146146
# the error object as the single argument.
147+
# * `default_labels` - (Hash) A Hash that contains the default labels
148+
# which will be added to every root span.
147149
#
148150
# See the {file:INSTRUMENTATION.md Configuration Guide} for full
149151
# configuration parameters.

google-cloud-trace/lib/google/cloud/trace/middleware.rb

Lines changed: 13 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -301,12 +301,25 @@ def get_url env
301301
#
302302
def configure_span span, env
303303
span.name = get_path env
304+
set_default_labels span.labels
304305
set_basic_labels span.labels, env
305306
set_extended_labels span.labels,
306307
span.trace.trace_context.capture_stack?
307308
span
308309
end
309310

311+
##
312+
# Configures default labels.
313+
# @private
314+
#
315+
# rubocop:disable Naming/AccessorMethodName
316+
def set_default_labels labels
317+
configuration.default_labels.each do |key, value|
318+
set_label labels, key, value
319+
end
320+
end
321+
# rubocop:enable Naming/AccessorMethodName
322+
310323
##
311324
# Configures standard labels.
312325
# @private

google-cloud-trace/lib/google/cloud/trace/rails.rb

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -85,6 +85,7 @@ class Railtie < ::Rails::Railtie
8585
config.google_cloud.trace.notifications = DEFAULT_NOTIFICATIONS.dup
8686
config.google_cloud.trace.max_data_length =
8787
Google::Cloud::Trace::Notifications::DEFAULT_MAX_DATA_LENGTH
88+
config.google_cloud.trace.default_labels = {}
8889

8990
initializer "Google.Cloud.Trace" do |app|
9091
self.class.consolidate_rails_config app.config
@@ -168,6 +169,9 @@ def self.merge_rails_config rails_config
168169
end
169170
config.sampler ||= trace_config.sampler
170171
config.span_id_generator ||= trace_config.span_id_generator
172+
if trace_config.default_labels.present?
173+
config.default_labels = trace_config.default_labels
174+
end
171175
end
172176
end
173177

google-cloud-trace/test/google/cloud/trace/rails_test.rb

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -58,6 +58,7 @@
5858
_(config.max_data_length).must_equal 123
5959
_(config.sampler).must_equal "test-sampler"
6060
_(config.span_id_generator).must_equal a_test_generator
61+
_(config.default_labels).must_equal({})
6162
end
6263
end
6364
end
@@ -71,6 +72,7 @@
7172
config.max_data_length = 345
7273
config.sampler = "another-test-sampler"
7374
config.span_id_generator = another_generator
75+
config.default_labels = { '/component' => 'test-service' }
7476
end
7577

7678
STDOUT.stub :puts, nil do
@@ -84,6 +86,7 @@
8486
_(config.max_data_length).must_equal 345
8587
_(config.sampler).must_equal "another-test-sampler"
8688
_(config.span_id_generator).must_equal another_generator
89+
_(config.default_labels).must_equal({ '/component' => 'test-service' })
8790
end
8891
end
8992
end

0 commit comments

Comments
 (0)