diff --git a/lib/instana/exporter/otlp/base_converter.rb b/lib/instana/exporter/otlp/base_converter.rb index f03de390..9dfe521e 100644 --- a/lib/instana/exporter/otlp/base_converter.rb +++ b/lib/instana/exporter/otlp/base_converter.rb @@ -422,7 +422,7 @@ def resource_adapter # # @return [InstrumentationScope] Scope identifying the Instana Ruby sensor def instrumentation_scope - @instrumentation_scope ||= InstrumentationScope.new('instana-ruby', ::Instana::VERSION) + @instrumentation_scope ||= InstrumentationScope.new('instana.ruby.tracer', ::Instana::VERSION) end end end diff --git a/lib/instana/exporter/otlp/resource.rb b/lib/instana/exporter/otlp/resource.rb index 2db69da2..480e1bc0 100644 --- a/lib/instana/exporter/otlp/resource.rb +++ b/lib/instana/exporter/otlp/resource.rb @@ -2,6 +2,7 @@ # (c) Copyright IBM Corp. 2026 +require 'open3' require 'socket' require 'opentelemetry/semantic_conventions' require_relative '../../util' @@ -14,10 +15,16 @@ module Otlp # This follows OpenTelemetry semantic conventions for resource attributes class Resource PROC_SELF_CGROUP = '/proc/self/cgroup' + PROC_SELF_MOUNTINFO = '/proc/self/mountinfo' + # cgroup v1 and systemd-driver paths: /docker/, docker-.scope, cri-containerd-.scope, + # crio-.scope, libpod-.scope + CGROUP_CONTAINER_ID_REGEX = %r{(?:/docker/|/(?:docker|cri-containerd|crio|libpod)-)([a-f0-9]{64})} + # cgroup v2 mount paths: /docker/containers//..., .../overlay-containers//... + MOUNTINFO_CONTAINER_ID_REGEX = %r{/(?:docker/containers|overlay-containers)/([a-f0-9]{64})/} DOCKER_ENV_FILE = '/.dockerenv' PODMAN_CONTAINERENV = '/run/.containerenv' - # Linux-only stable machine-id paths (systemd and D-Bus fallback). - # These files do not exist on macOS or Windows; host_id returns nil there. + # Linux stable machine-id paths (systemd and D-Bus fallback). + # macOS uses a command-based lookup instead (see host_id). MACHINE_ID_PATHS = %w[/etc/machine-id /var/lib/dbus/machine-id].freeze # cloud.resource_id is not yet in the installed semconv gem version CLOUD_RESOURCE_ID = 'cloud.resource_id' @@ -173,6 +180,7 @@ def faas_attributes add_aws_lambda_attributes(attrs) add_cloud_run_attributes(attrs) + add_azure_functions_attributes(attrs) create(attrs) end @@ -248,6 +256,15 @@ def add_cloud_run_attributes(attrs) attrs[OpenTelemetry::SemanticConventions::Resource::FAAS_VERSION] = revision if revision end + # Detects Azure Functions. FUNCTIONS_WORKER_RUNTIME is set only for Functions, + # and WEBSITE_SITE_NAME is injected only when running on Azure (not local `func start`). + def add_azure_functions_attributes(attrs) + return unless ENV.fetch('FUNCTIONS_WORKER_RUNTIME', nil) && ENV.fetch('WEBSITE_SITE_NAME', nil) + + attrs[OpenTelemetry::SemanticConventions::Resource::CLOUD_PROVIDER] = 'azure' + attrs[OpenTelemetry::SemanticConventions::Resource::CLOUD_PLATFORM] = 'azure_functions' + end + # Returns true when the current OS is Linux. # # @return [Boolean] @@ -270,18 +287,38 @@ def detect_os_type end # Returns a stable machine-level identifier. - # Reads /etc/machine-id (Linux systemd standard) or - # /var/lib/dbus/machine-id as fallback. Returns nil on macOS/Windows. + # Linux: /etc/machine-id, falling back to /var/lib/dbus/machine-id + # macOS: IOPlatformUUID from `ioreg` # # @return [String, nil] def host_id + id = case detect_os_type + when 'linux' then linux_host_id + when 'darwin' then darwin_host_id + else '' # unsupported OS: normalized to nil below + end + id = id&.strip + id unless id.nil? || id.empty? + rescue StandardError + nil + end + + def linux_host_id path = MACHINE_ID_PATHS.find { |machine_id_path| File.exist?(machine_id_path) } - return nil unless path + File.read(path) if path + end - id = File.read(path).strip - return id unless id.empty? + def darwin_host_id + output = run_command('ioreg', '-rd1', '-c', 'IOPlatformExpertDevice') + output&.[](/"IOPlatformUUID"\s*=\s*"([^"]+)"/, 1) + end - nil + # Runs an external command without a shell and returns stdout, or nil on failure. + # + # @return [String, nil] + def run_command(*cmd) + output, status = Open3.capture2(*cmd, err: File::NULL) + output if status.success? rescue StandardError nil end @@ -319,20 +356,26 @@ def host_architecture RbConfig::CONFIG['host_cpu'] end - # Extract container ID from cgroup file + # Extract the 64-hex container ID. + # + # cgroup v1 exposes it in /proc/self/cgroup (e.g. /docker/, docker-.scope, + # cri-containerd-.scope). On cgroup v2 that file is just "0::/", so the ID is + # taken from the container paths in /proc/self/mountinfo instead. # # @return [String, nil] Container ID def extract_container_id - return nil unless File.exist?(PROC_SELF_CGROUP) - - line = File.readlines(PROC_SELF_CGROUP).find do |l| - l.match?(%r{/docker/([a-f0-9]{64})}) - end + container_id_from_file(PROC_SELF_CGROUP, CGROUP_CONTAINER_ID_REGEX) || + container_id_from_file(PROC_SELF_MOUNTINFO, MOUNTINFO_CONTAINER_ID_REGEX) + end - return nil unless line + def container_id_from_file(path, regex) + return nil unless File.exist?(path) - match = line.match(%r{/docker/([a-f0-9]{64})}) - match[1] + File.foreach(path) do |line| + match = line.match(regex) + return match[1] if match + end + nil rescue StandardError nil end diff --git a/lib/instana/instrumentation/aws_sdk_s3.rb b/lib/instana/instrumentation/aws_sdk_s3.rb index d6b3329e..7bfa630d 100644 --- a/lib/instana/instrumentation/aws_sdk_s3.rb +++ b/lib/instana/instrumentation/aws_sdk_s3.rb @@ -9,7 +9,8 @@ def call(context) s3_tags = { op: format_operation(context.operation_name), bucket: bucket_name_from(context), - key: key_from_context(context) + key: key_from_context(context), + region: context.config.region }.reject { |_, v| v.nil? } ::Instana.tracer.in_span(:s3, attributes: {s3: s3_tags}) { @handler.call(context) } diff --git a/test/exporter/otlp/base_converter_test.rb b/test/exporter/otlp/base_converter_test.rb index 7a6fdadd..f9de017c 100644 --- a/test/exporter/otlp/base_converter_test.rb +++ b/test/exporter/otlp/base_converter_test.rb @@ -25,6 +25,14 @@ def test_convert_returns_span_data assert_instance_of String, span_data.span_id end + def test_convert_uses_spec_instrumentation_scope_name + span = create_test_span(name: :rack) + span_data = Instana::Exporter::Otlp::BaseConverter.new(span).convert + + assert_equal 'instana.ruby.tracer', span_data.instrumentation_scope.name + assert_equal ::Instana::VERSION, span_data.instrumentation_scope.version + end + def test_convert_span_kind # Test explicit internal kind span = create_test_span(name: :rack, kind: 3) # Instana intermediate/internal diff --git a/test/exporter/otlp/resource_test.rb b/test/exporter/otlp/resource_test.rb index 75a12f55..a367ac53 100644 --- a/test/exporter/otlp/resource_test.rb +++ b/test/exporter/otlp/resource_test.rb @@ -5,7 +5,7 @@ require 'test_helper' require 'instana/exporter/otlp/resource' -class ResourceTest < Minitest::Test +class ResourceTest < Minitest::Test # rubocop:disable Metrics/ClassLength R = Instana::Exporter::Otlp::Resource SC = OpenTelemetry::SemanticConventions::Resource @@ -21,7 +21,7 @@ def teardown AWS_LAMBDA_FUNCTION_NAME AWS_LAMBDA_FUNCTION_VERSION AWS_LAMBDA_FUNCTION_ARN KUBERNETES_SERVICE_HOST MY_POD_UID KUBERNETES_NAMESPACE ECS_CONTAINER_METADATA_URI ECS_CONTAINER_METADATA_URI_V4 - K_SERVICE K_REVISION + K_SERVICE K_REVISION FUNCTIONS_WORKER_RUNTIME WEBSITE_SITE_NAME ].each { |k| ENV.delete(k) } end @@ -110,7 +110,7 @@ def test_host_id_reads_machine_id_file FileUtils.mkdir_p('/etc') File.write('/etc/machine-id', "abc123\n") R.reset! - assert_equal 'abc123', R.send(:host_id) + stub_rbconfig('linux-gnu') { assert_equal 'abc123', R.send(:host_id) } end end @@ -119,14 +119,14 @@ def test_host_id_falls_back_to_dbus_machine_id FileUtils.mkdir_p('/var/lib/dbus') File.write('/var/lib/dbus/machine-id', "fallback-id\n") R.reset! - assert_equal 'fallback-id', R.send(:host_id) + stub_rbconfig('linux-gnu') { assert_equal 'fallback-id', R.send(:host_id) } end end def test_host_id_returns_nil_when_no_file FakeFS.with_fresh do R.reset! - assert_nil R.send(:host_id) + stub_rbconfig('linux-gnu') { assert_nil R.send(:host_id) } end end @@ -134,11 +134,43 @@ def test_host_id_present_in_default_resource_on_linux FakeFS.with_fresh do FileUtils.mkdir_p('/etc') File.write('/etc/machine-id', "machine-xyz\n") - R.reset! - assert_equal 'machine-xyz', R.instance[SC::HOST_ID] + stub_rbconfig('linux-gnu') do + R.reset! + assert_equal 'machine-xyz', R.instance[SC::HOST_ID] + end + end + end + + def test_host_id_on_macos_reads_ioplatformuuid + output = <<~IOREG + +-o Root + +-o MacBookPro + { + "IOPlatformUUID" = "11111111-2222-3333-4444-555555555555" + "manufacturer" = <"Apple Inc."> + } + IOREG + stub_rbconfig('arm-apple-darwin23') do + with_run_command(output) { assert_equal '11111111-2222-3333-4444-555555555555', R.send(:host_id) } + end + end + + def test_host_id_on_macos_returns_nil_when_command_fails + stub_rbconfig('arm-apple-darwin23') do + with_run_command(nil) { assert_nil R.send(:host_id) } end end + def test_host_id_on_macos_returns_nil_when_output_unparseable + stub_rbconfig('arm-apple-darwin23') do + with_run_command('garbage') { assert_nil R.send(:host_id) } + end + end + + def test_host_id_returns_nil_on_unknown_os + stub_rbconfig('aix7.1') { assert_nil R.send(:host_id) } + end + # ─── service.instance.id priority ───────────────────────────────────────── def test_service_instance_id_uses_pod_uid_over_hostname_pid @@ -155,16 +187,20 @@ def test_service_instance_id_uses_host_id_over_hostname_pid FakeFS.with_fresh do FileUtils.mkdir_p('/etc') File.write('/etc/machine-id', "stable-host-id\n") - R.reset! - assert_equal 'stable-host-id', R.instance[SC::SERVICE_INSTANCE_ID] + stub_rbconfig('linux-gnu') do + R.reset! + assert_equal 'stable-host-id', R.instance[SC::SERVICE_INSTANCE_ID] + end end end def test_service_instance_id_falls_back_to_hostname_pid FakeFS.with_fresh do - R.reset! - instance_id = R.instance[SC::SERVICE_INSTANCE_ID] - assert_match(/.+:\d+/, instance_id, 'fallback should be hostname:pid') + stub_rbconfig('linux-gnu') do + R.reset! + instance_id = R.instance[SC::SERVICE_INSTANCE_ID] + assert_match(/.+:\d+/, instance_id, 'fallback should be hostname:pid') + end end end @@ -276,6 +312,88 @@ def test_no_container_runtime_detected_on_non_linux end end + # ─── Azure Functions ─────────────────────────────────────────────────────── + + def test_azure_functions_attributes_set_when_env_present + ENV['FUNCTIONS_WORKER_RUNTIME'] = 'ruby' + ENV['WEBSITE_SITE_NAME'] = 'my-func-app' + R.reset! + attrs = R.instance + assert_equal 'azure', attrs[SC::CLOUD_PROVIDER] + assert_equal 'azure_functions', attrs[SC::CLOUD_PLATFORM] + end + + def test_azure_functions_not_detected_without_website_site_name + ENV['FUNCTIONS_WORKER_RUNTIME'] = 'ruby' + R.reset! + refute R.instance.key?(SC::CLOUD_PROVIDER) + refute R.instance.key?(SC::CLOUD_PLATFORM) + end + + def test_azure_functions_not_detected_without_functions_worker_runtime + ENV['WEBSITE_SITE_NAME'] = 'my-web-app' + R.reset! + refute R.instance.key?(SC::CLOUD_PROVIDER) + end + + def test_azure_attributes_in_faas_attrs_not_container_attrs + ENV['FUNCTIONS_WORKER_RUNTIME'] = 'ruby' + ENV['WEBSITE_SITE_NAME'] = 'my-func-app' + R.reset! + assert_equal 'azure_functions', R.send(:faas_attributes).attributes[SC::CLOUD_PLATFORM] + refute R.send(:container_attributes).attributes.key?(SC::CLOUD_PLATFORM) + end + + # ─── container.id ────────────────────────────────────────────────────────── + + CONTAINER_ID = ('a1b2c3d4' * 8).freeze + + def test_container_id_from_cgroup_v1_docker_path + with_proc_files('/proc/self/cgroup' => "12:cpu:/docker/#{CONTAINER_ID}\n") do + assert_equal CONTAINER_ID, R.send(:extract_container_id) + end + end + + def test_container_id_from_cgroup_v1_systemd_scope + with_proc_files('/proc/self/cgroup' => "1:name=systemd:/system.slice/docker-#{CONTAINER_ID}.scope\n") do + assert_equal CONTAINER_ID, R.send(:extract_container_id) + end + end + + def test_container_id_from_cgroup_v1_kubernetes_containerd + path = "/kubepods.slice/kubepods-pod1.slice/cri-containerd-#{CONTAINER_ID}.scope" + with_proc_files('/proc/self/cgroup' => "0::#{path}\n") do + assert_equal CONTAINER_ID, R.send(:extract_container_id) + end + end + + def test_container_id_from_cgroup_v2_docker_mountinfo + mountinfo = "612 598 254:1 /var/lib/docker/containers/#{CONTAINER_ID}/hostname /etc/hostname rw - ext4 /dev/vda1 rw\n" + with_proc_files('/proc/self/cgroup' => "0::/\n", '/proc/self/mountinfo' => mountinfo) do + assert_equal CONTAINER_ID, R.send(:extract_container_id) + end + end + + def test_container_id_from_cgroup_v2_podman_mountinfo + mountinfo = "700 650 0:50 /containers/storage/overlay-containers/#{CONTAINER_ID}/userdata/hostname /etc/hostname rw - tmpfs tmpfs rw\n" + with_proc_files('/proc/self/cgroup' => "0::/\n", '/proc/self/mountinfo' => mountinfo) do + assert_equal CONTAINER_ID, R.send(:extract_container_id) + end + end + + def test_container_id_nil_when_not_in_container + with_proc_files('/proc/self/cgroup' => "0::/user.slice/session-1.scope\n", + '/proc/self/mountinfo' => "22 28 0:21 / /proc rw - proc proc rw\n") do + assert_nil R.send(:extract_container_id) + end + end + + def test_container_id_nil_when_proc_files_missing + FakeFS.with_fresh do + assert_nil R.send(:extract_container_id) + end + end + # ─── Lambda is NOT part of container_attributes ─────────────────────────── def test_lambda_attributes_not_in_container_attrs @@ -299,6 +417,19 @@ def test_lambda_attributes_in_faas_attrs private + def with_proc_files(files) + FakeFS.with_fresh do + FileUtils.mkdir_p('/proc/self') + files.each { |path, content| File.write(path, content) } + yield + end + end + + # Replaces Resource.run_command with a stub returning +output+ for the duration of the block. + def with_run_command(output, &block) + R.stub(:run_command, output, &block) + end + def stub_rbconfig(host_os_value) original = RbConfig::CONFIG['host_os'] RbConfig::CONFIG['host_os'] = host_os_value diff --git a/test/instrumentation/aws_test.rb b/test/instrumentation/aws_test.rb index 9052c723..73986fdd 100644 --- a/test/instrumentation/aws_test.rb +++ b/test/instrumentation/aws_test.rb @@ -132,6 +132,7 @@ def test_s3 assert_equal 'get', s3_span[:data][:s3][:op] assert_equal 'sample-bucket', s3_span[:data][:s3][:bucket] assert_equal 'sample_key', s3_span[:data][:s3][:key] + assert_equal 'local', s3_span[:data][:s3][:region] end def test_s3_operations