Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
1 change: 0 additions & 1 deletion api/lib/opentelemetry.rb
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,6 @@
require 'opentelemetry/distributed_context'
require 'opentelemetry/internal'
require 'opentelemetry/metrics'
require 'opentelemetry/resources'
require 'opentelemetry/trace'
require 'opentelemetry/version'

Expand Down
73 changes: 0 additions & 73 deletions api/lib/opentelemetry/resources/resource.rb

This file was deleted.

1 change: 1 addition & 0 deletions sdk/lib/opentelemetry/sdk.rb
Original file line number Diff line number Diff line change
Expand Up @@ -13,5 +13,6 @@ module SDK
end

require 'opentelemetry/sdk/internal'
require 'opentelemetry/sdk/resources'
require 'opentelemetry/sdk/trace'
require 'opentelemetry/sdk/version'
Original file line number Diff line number Diff line change
Expand Up @@ -4,4 +4,4 @@
#
# SPDX-License-Identifier: Apache-2.0

require 'opentelemetry/resources/resource'
require 'opentelemetry/sdk/resources/resource'
75 changes: 75 additions & 0 deletions sdk/lib/opentelemetry/sdk/resources/resource.rb
Original file line number Diff line number Diff line change
@@ -0,0 +1,75 @@
# frozen_string_literal: true

# Copyright 2019 OpenTelemetry Authors
#
# SPDX-License-Identifier: Apache-2.0

module OpenTelemetry
module SDK
module Resources
# Resource represents a resource, which captures identifying information about the entities
# for which telemetry (metrics or traces) is reported.
class Resource
class << self
private :new # rubocop:disable Style/AccessModifierDeclarations

# Returns a newly created {Resource} with the specified labels
#
# @param [Hash<String, String>] labels Hash of key-value pairs to be used
# as labels for this resource
# @raise [ArgumentError] If label keys and values are not strings
# @return [Resource]
def create(labels = {})
frozen_labels = labels.each_with_object({}) do |(k, v), memo|
raise ArgumentError, 'label keys and values must be strings' unless k.is_a?(String) && v.is_a?(String)

memo[-k] = -v
end.freeze

new(frozen_labels)
end
end

# @api private
# The constructor is private and only for use internally by the class.
# Users should use the {create} factory method to obtain a {Resource}
# instance.
#
# @param [Hash<String, String>] frozen_labels Frozen-hash of frozen-string
# key-value pairs to be used as labels for this resource
# @return [Resource]
def initialize(frozen_labels)
@labels = frozen_labels
end

# Returns an enumerator for labels of this {Resource}
#
# @return [Enumerator]
def label_enumerator
@label_enumerator ||= labels.to_enum
end

# Returns a new, merged {Resource} by merging the current {Resource} with
# the other {Resource}. In case of a collision, the current {Resource}
# takes precedence
#
# @param [Resource] other The other resource to merge
# @return [Resource] A new resource formed by merging the current resource
# with other
def merge(other)
raise ArgumentError unless other.is_a?(Resource)

merged_labels = labels.merge(other.labels) do |_, old_v, new_v|
old_v.empty? ? new_v : old_v
end

self.class.send(:new, merged_labels.freeze)
end

protected

attr_reader :labels
end
end
end
end
2 changes: 2 additions & 0 deletions sdk/lib/opentelemetry/sdk/trace/tracer.rb
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,7 @@ module Trace
# {Tracer} is the SDK implementation of {OpenTelemetry::Trace::Tracer}.
class Tracer < OpenTelemetry::Trace::Tracer
attr_accessor :active_trace_config
attr_accessor :resource # TODO: should this be read-only? If not, how should exporters know when it is updated?
Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

In the java SDK the resource is final: https://github.com/open-telemetry/opentelemetry-java/blob/master/sdk/src/main/java/io/opentelemetry/sdk/trace/TracerSdk.java#L50 and assigned as an EnvVarResource, which we don't yet have. We could consider doing the same and turn this into a reader.

Copy link
Copy Markdown
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Yeah, I wasn't sure how we wanted to configure the Resource. Go has WithService and WithResource factory methods for the Tracer, but they're not wired up to anything yet. I'd like to tackle configuration of the Resource in a subsequent PR.

Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

The WithResource/WithService should be moved on the TracerSDK. Only Component will be available in the API based on the OTEP-007 (better than 0007)

Copy link
Copy Markdown
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

OTEP-007 (better than 0007)

🤣

Copy link
Copy Markdown
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

@bogdandrutu this PR moves the Resource to the SDK, but I didn't see anything in the OTEP-007 indicating how the Resource should be configured. Is the plan to only support configuration from environment variables, as in the Java SDK, or should we also allow explicit manual configuration like the Go SDK?


# Returns a new {Tracer} instance.
#
Expand All @@ -20,6 +21,7 @@ def initialize
@registered_span_processors = []
@mutex = Mutex.new
@stopped = false
@resource = Resources::Resource.create # TODO: should this initialize from env vars instead?
end

# Attempts to stop all the activity for this {Tracer}. Calls
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -6,42 +6,40 @@

require 'test_helper'

describe OpenTelemetry::Resources::Resource do
describe OpenTelemetry::SDK::Resources::Resource do
Resource = OpenTelemetry::SDK::Resources::Resource

describe '.new' do
it 'is private' do
-> { OpenTelemetry::Resources::Resource.new('k1' => 'v1') }\
.must_raise(NoMethodError)
proc { Resource.new('k1' => 'v1') }.must_raise(NoMethodError)
end
end

describe '.create' do
it 'can be initialized with labels' do
expected_labels = { 'k1' => 'v1', 'k2' => 'v2' }
resource = OpenTelemetry::Resources::Resource.create(expected_labels)
resource = Resource.create(expected_labels)
resource.label_enumerator.to_h.must_equal(expected_labels)
end

it 'can be empty' do
resource = OpenTelemetry::Resources::Resource.create
resource = Resource.create
resource.label_enumerator.to_h.must_be_empty
end

it 'enforces keys are strings' do
-> { OpenTelemetry::Resources::Resource.create(k1: 'v1') }\
.must_raise(ArgumentError)
proc { Resource.create(k1: 'v1') }.must_raise(ArgumentError)
end

it 'enforces values are strings' do
-> { OpenTelemetry::Resources::Resource.create('k1' => :v1) }\
.must_raise(ArgumentError)
proc { Resource.create('k1' => :v1) }.must_raise(ArgumentError)
end
end

describe '#merge' do
it 'merges two resources into a third' do
res1 = OpenTelemetry::Resources::Resource.create('k1' => 'v1',
'k2' => 'v2')
res2 = OpenTelemetry::Resources::Resource.create('k3' => 'v3',
'k4' => 'v4')
res1 = Resource.create('k1' => 'v1', 'k2' => 'v2')
res2 = Resource.create('k3' => 'v3', 'k4' => 'v4')
res3 = res1.merge(res2)

res3.label_enumerator.to_h.must_equal('k1' => 'v1', 'k2' => 'v2',
Expand All @@ -51,10 +49,8 @@
end

it 'does not overwrite receiver\'s keys when value is non-empty' do
res1 = OpenTelemetry::Resources::Resource.create('k1' => 'v1',
'k2' => 'v2')
res2 = OpenTelemetry::Resources::Resource.create('k2' => '2v2',
'k3' => '2v3')
res1 = Resource.create('k1' => 'v1', 'k2' => 'v2')
res2 = Resource.create('k2' => '2v2', 'k3' => '2v3')
res3 = res1.merge(res2)

res3.label_enumerator.to_h.must_equal('k1' => 'v1',
Expand All @@ -63,10 +59,8 @@
end

it 'overwrites receiver\'s key when value is empty' do
res1 = OpenTelemetry::Resources::Resource.create('k1' => 'v1',
'k2' => '')
res2 = OpenTelemetry::Resources::Resource.create('k2' => '2v2',
'k3' => '2v3')
res1 = Resource.create('k1' => 'v1', 'k2' => '')
res2 = Resource.create('k2' => '2v2', 'k3' => '2v3')
res3 = res1.merge(res2)

res3.label_enumerator.to_h.must_equal('k1' => 'v1',
Expand Down