From 0bcf169279a9dd02d4ba561ea76408442a92ed87 Mon Sep 17 00:00:00 2001 From: Arjun Rajappa Date: Wed, 25 Nov 2026 14:00:37 +0530 Subject: [PATCH] fix: resolve Blocker and High SonarCloud findings - Pass workflow inputs to the Slack release script via env vars instead of interpolating them in the shell (script injection) - Replace Thread.current with Fiber[] for the Lambda function ARN and with local variables in the gRPC and Sidekiq test boot files - Use ENV.fetch instead of ENV[] for reads without a default - Add explicit else branches to case statements and set Style/EmptyElse to EnforcedStyle: empty in .rubocop.yml - Comment intentionally empty methods - Extract duplicated string literals into constants - Assign APP once in the Sinatra and Rails tests - Move requires to the top of the file in trace.rb, util.rb and grpc_server.rb No behavior change intended. The remaining S7816 findings are lazy or conditional requires and are to be marked as false positives. Signed-off-by: Arjun Rajappa --- .../release-notification-on-slack.yml | 6 ++++-- .rubocop.yml | 6 +++++- lib/instana/backend/agent.rb | 2 +- lib/instana/backend/host_agent.rb | 4 +++- lib/instana/backend/process_info.rb | 2 +- lib/instana/backend/serverless_agent.rb | 8 +++++--- lib/instana/config.rb | 7 ++++--- .../instrumentation/action_controller.rb | 2 ++ lib/instana/instrumentation/aws_sdk_sqs.rb | 2 ++ lib/instana/instrumentation/graphql.rb | 8 +++++--- lib/instana/instrumentation/grpc.rb | 13 ++++++++----- lib/instana/serverless.rb | 4 ++-- lib/instana/snapshot/docker_container.rb | 2 +- lib/instana/snapshot/fargate_container.rb | 2 +- lib/instana/snapshot/fargate_process.rb | 2 +- lib/instana/snapshot/fargate_task.rb | 4 ++-- .../snapshot/google_cloud_run_instance.rb | 10 +++++----- .../snapshot/google_cloud_run_process.rb | 2 +- lib/instana/snapshot/lambda_function.rb | 2 +- lib/instana/span_filtering/configuration.rb | 8 +++++--- lib/instana/trace.rb | 10 +++++----- lib/instana/trace/span.rb | 4 +++- lib/instana/util.rb | 5 +++-- test/frameworks/sinatra_test.rb | 6 ++---- .../rails_action_controller_test.rb | 7 +++---- test/instrumentation/rails_action_view_test.rb | 6 ++---- test/instrumentation/rails_active_job_test.rb | 4 +++- test/snapshot/lambda_function_test.rb | 18 +++++++++--------- test/support/apps/grpc/boot.rb | 14 ++++++-------- test/support/apps/grpc/grpc_server.rb | 2 +- .../support/apps/sidekiq/jobs/sidekiq_job_1.rb | 1 + test/support/apps/sidekiq/worker.rb | 6 +++--- test/support/helpers.rb | 2 ++ 33 files changed, 102 insertions(+), 79 deletions(-) diff --git a/.github/workflows/release-notification-on-slack.yml b/.github/workflows/release-notification-on-slack.yml index d040e2a9b..c28caa476 100644 --- a/.github/workflows/release-notification-on-slack.yml +++ b/.github/workflows/release-notification-on-slack.yml @@ -18,8 +18,8 @@ jobs: - name: 'Checkout' uses: actions/checkout@v3 - run: | - if [[ ${{ github.event_name == 'workflow_dispatch' }} == true ]]; then - export GITHUB_RELEASE_TAG=${{ inputs.github_ref }} + if [[ "${EVENT_NAME}" == "workflow_dispatch" ]]; then + export GITHUB_RELEASE_TAG="${INPUT_GITHUB_REF}" else # release event export GITHUB_RELEASE_TAG=$(basename ${GITHUB_REF}) fi @@ -29,6 +29,8 @@ jobs: ls -lah python ./.github/scripts/announce_release_on_slack.py env: + EVENT_NAME: ${{ github.event_name }} + INPUT_GITHUB_REF: ${{ inputs.github_ref }} GITHUB_TOKEN: ${{ secrets.GITHUB_TOKEN }} SLACK_TOKEN: ${{ secrets.RUPY_TRACER_RELEASES_TOKEN }} SLACK_SERVICE: ${{ secrets.RUPY_TRACER_RELEASES_SERVICE_ID }} diff --git a/.rubocop.yml b/.rubocop.yml index 7189a3f25..1c0d5ef74 100644 --- a/.rubocop.yml +++ b/.rubocop.yml @@ -31,4 +31,8 @@ Style/SymbolProc: Enabled: false Style/MultilineBlockChain: - Enabled: false \ No newline at end of file + Enabled: false + +# Allow an explicit `else nil` in case statements (SonarCloud S131 requires a default clause) +Style/EmptyElse: + EnforcedStyle: empty \ No newline at end of file diff --git a/lib/instana/backend/agent.rb b/lib/instana/backend/agent.rb index aa7079e31..f2a798b7a 100644 --- a/lib/instana/backend/agent.rb +++ b/lib/instana/backend/agent.rb @@ -8,7 +8,7 @@ module Backend class Agent attr_reader :delegate - def initialize(fargate_metadata_uri: ENV['ECS_CONTAINER_METADATA_URI'], logger: ::Instana.logger) + def initialize(fargate_metadata_uri: ENV.fetch('ECS_CONTAINER_METADATA_URI', nil), logger: ::Instana.logger) @delegate = nil @logger = logger @fargate_metadata_uri = fargate_metadata_uri diff --git a/lib/instana/backend/host_agent.rb b/lib/instana/backend/host_agent.rb index 9e3e04eb1..c5929b69a 100644 --- a/lib/instana/backend/host_agent.rb +++ b/lib/instana/backend/host_agent.rb @@ -16,7 +16,9 @@ def initialize(discovery: Concurrent::Atom.new(nil), logger: ::Instana.logger) @agent_connection_task = Concurrent::TimerTask.new(execution_interval: 75) { announce } end - def setup; end + def setup + # No setup needed: the host agent is activated via the announce timer task + end def spawn_background_thread return if ENV.key?('INSTANA_TEST') diff --git a/lib/instana/backend/process_info.rb b/lib/instana/backend/process_info.rb index d24be9d8f..ce177386b 100644 --- a/lib/instana/backend/process_info.rb +++ b/lib/instana/backend/process_info.rb @@ -63,7 +63,7 @@ def clean_arguments(arguments) arguments.reject do |a| if a.include?('=') k, = a.split('=', 2) - ENV[k] + ENV.fetch(k, nil) end end end diff --git a/lib/instana/backend/serverless_agent.rb b/lib/instana/backend/serverless_agent.rb index 525ea1d51..655722373 100644 --- a/lib/instana/backend/serverless_agent.rb +++ b/lib/instana/backend/serverless_agent.rb @@ -13,7 +13,7 @@ def initialize(snapshots, timer_class: Concurrent::TimerTask, processor: ::Instana.processor, logger: ::Instana.logger, - backend_uri: ENV['INSTANA_ENDPOINT_URL'], + backend_uri: ENV.fetch('INSTANA_ENDPOINT_URL', nil), secrets: ENV.fetch('INSTANA_SECRETS', DEFAULT_SECRETS), headers: ENV.fetch('INSTANA_EXTRA_HTTP_HEADERS', '')) @snapshots = snapshots @processor = processor @@ -26,7 +26,9 @@ def initialize(snapshots, end # rubocop:enable Metrics/ParameterLists - def setup; end + def setup + # No setup needed: the serverless agent is ready on construction + end def spawn_background_thread @timer.execute @@ -85,7 +87,7 @@ def send_bundle def request_headers { 'X-Instana-Host' => host_name, - 'X-Instana-Key' => ENV['INSTANA_AGENT_KEY'], + 'X-Instana-Key' => ENV.fetch('INSTANA_AGENT_KEY', nil), 'X-Instana-Time' => (Time.now.to_i * 1000).to_s } end diff --git a/lib/instana/config.rb b/lib/instana/config.rb index 9b497ab70..3caf6d6ce 100644 --- a/lib/instana/config.rb +++ b/lib/instana/config.rb @@ -8,6 +8,7 @@ class Config # rubocop:disable Metrics/ClassLength LEGACY_TRACING_KEY = 'com.instana.tracing'.freeze TRACING_KEY = 'tracing'.freeze + DISABLE_SERVER_TIMING_KEY = 'disable-server-timing'.freeze def initialize(logger: ::Instana.logger, agent_host: ENV.fetch('INSTANA_AGENT_HOST', nil), agent_port: ENV.fetch('INSTANA_AGENT_PORT', nil)) # rubocop:disable Metrics/MethodLength @config = {} @@ -302,7 +303,7 @@ def parse_disable_server_timing_from_yaml yaml_content = YAML.safe_load(File.read(config_path)) tracing_config = yaml_content[TRACING_KEY] || yaml_content[LEGACY_TRACING_KEY] global_config = tracing_config && tracing_config['global'] - global_config.is_a?(Hash) ? global_config['disable-server-timing'] : nil + global_config.is_a?(Hash) ? global_config[DISABLE_SERVER_TIMING_KEY] : nil rescue => e ::Instana.logger.warn("Failed to load disable-server-timing configuration from YAML: #{e.message}") nil @@ -311,9 +312,9 @@ def parse_disable_server_timing_from_yaml # @param tracing_config [Hash] the tracing sub-hash from the discovery payload def read_disable_server_timing_from_agent(tracing_config) global_config = tracing_config['global'] - return unless global_config.is_a?(Hash) && !global_config['disable-server-timing'].nil? + return unless global_config.is_a?(Hash) && !global_config[DISABLE_SERVER_TIMING_KEY].nil? - @config[:disable_server_timing] = truthy?(global_config['disable-server-timing']) + @config[:disable_server_timing] = truthy?(global_config[DISABLE_SERVER_TIMING_KEY]) @config[:disable_server_timing_source] = 'agent' end diff --git a/lib/instana/instrumentation/action_controller.rb b/lib/instana/instrumentation/action_controller.rb index 082433b88..69793033a 100644 --- a/lib/instana/instrumentation/action_controller.rb +++ b/lib/instana/instrumentation/action_controller.rb @@ -75,6 +75,8 @@ def describe_direct(options) options[:template] when ->(o) { o.key?(:file) } options[:file] + else + nil # unrecognized render option end end end diff --git a/lib/instana/instrumentation/aws_sdk_sqs.rb b/lib/instana/instrumentation/aws_sdk_sqs.rb index 02af70906..d2f6c6504 100644 --- a/lib/instana/instrumentation/aws_sdk_sqs.rb +++ b/lib/instana/instrumentation/aws_sdk_sqs.rb @@ -21,6 +21,8 @@ def call(context) inject_instana_headers(span, context.params) when :send_message_batch context.params[:entries].each { |e| inject_instana_headers(span, e) } + else + nil # other operations carry no message attributes to inject end response = @handler.call(context) diff --git a/lib/instana/instrumentation/graphql.rb b/lib/instana/instrumentation/graphql.rb index f875eacea..d06c8e26e 100644 --- a/lib/instana/instrumentation/graphql.rb +++ b/lib/instana/instrumentation/graphql.rb @@ -4,15 +4,17 @@ module Instana module Instrumentation class GraphqlTracing < GraphQL::Tracing::PlatformTracing + EXECUTE_GRAPHQL = 'execute.graphql'.freeze + self.platform_keys = { 'lex' => 'lex.graphql', 'parse' => 'parse.graphql', 'validate' => 'validate.graphql', 'analyze_query' => 'analyze.graphql', 'analyze_multiplex' => 'analyze.graphql', - 'execute_multiplex' => 'execute.graphql', - 'execute_query' => 'execute.graphql', - 'execute_query_lazy' => 'execute.graphql', + 'execute_multiplex' => EXECUTE_GRAPHQL, + 'execute_query' => EXECUTE_GRAPHQL, + 'execute_query_lazy' => EXECUTE_GRAPHQL, } def platform_trace(platform_key, key, data) diff --git a/lib/instana/instrumentation/grpc.rb b/lib/instana/instrumentation/grpc.rb index ce73834df..3e66bd52d 100644 --- a/lib/instana/instrumentation/grpc.rb +++ b/lib/instana/instrumentation/grpc.rb @@ -5,6 +5,9 @@ module Instana module Instrumentation + INSTANA_TRACE_ID_METADATA_KEY = 'x-instana-t'.freeze + INSTANA_SPAN_ID_METADATA_KEY = 'x-instana-s'.freeze + module GRPCCientInstrumentation CALL_TYPES = [:request_response, :client_streamer, :server_streamer, :bidi_streamer] @@ -27,8 +30,8 @@ module GRPCCientInstrumentation context = ::Instana.tracer.context if context extra = { - 'x-instana-t' => context.trace_id_header, - 'x-instana-s' => context.span_id_header, + INSTANA_TRACE_ID_METADATA_KEY => context.trace_id_header, + INSTANA_SPAN_ID_METADATA_KEY => context.span_id_header, 'x-instana-l' => context.level.to_s, 'traceparent' => context.trace_parent_header, 'tracestate' => context.trace_state_header @@ -91,7 +94,7 @@ module GRPCServerInstrumentation private def extract_grpc_context(metadata) - if metadata.key?('x-instana-t') + if metadata.key?(INSTANA_TRACE_ID_METADATA_KEY) extract_instana_grpc_context(metadata) elsif metadata.key?('traceparent') extract_w3c_grpc_context(metadata) @@ -100,8 +103,8 @@ def extract_grpc_context(metadata) def extract_instana_grpc_context(metadata) SpanContext.new( - trace_id: ::Instana::Util.header_to_id(metadata['x-instana-t']), - span_id: metadata.key?('x-instana-s') ? ::Instana::Util.header_to_id(metadata['x-instana-s']) : nil, + trace_id: ::Instana::Util.header_to_id(metadata[INSTANA_TRACE_ID_METADATA_KEY]), + span_id: metadata.key?(INSTANA_SPAN_ID_METADATA_KEY) ? ::Instana::Util.header_to_id(metadata[INSTANA_SPAN_ID_METADATA_KEY]) : nil, level: metadata['x-instana-l'] || 1 ) end diff --git a/lib/instana/serverless.rb b/lib/instana/serverless.rb index db26b31cf..9d5739b5c 100644 --- a/lib/instana/serverless.rb +++ b/lib/instana/serverless.rb @@ -24,7 +24,7 @@ def initialize(agent: ::Instana.agent, tracer: ::Instana.tracer, logger: ::Insta end def wrap_aws(event, context, &block) - Thread.current[:instana_function_arn] = [context.invoked_function_arn, context.function_version].join(':') + Fiber[:instana_function_arn] = [context.invoked_function_arn, context.function_version].join(':') trigger, event_tags, span_context = trigger_from_event(event, context) tags = { @@ -52,7 +52,7 @@ def wrap_aws(event, context, &block) rescue StandardError => e @logger.error(e.message) end - Thread.current[:instana_function_arn] = nil + Fiber[:instana_function_arn] = nil end private diff --git a/lib/instana/snapshot/docker_container.rb b/lib/instana/snapshot/docker_container.rb index c22e14772..e95170d63 100644 --- a/lib/instana/snapshot/docker_container.rb +++ b/lib/instana/snapshot/docker_container.rb @@ -9,7 +9,7 @@ class DockerContainer include Deltable ID = 'com.instana.plugin.docker'.freeze - def initialize(container, metadata_uri: ENV['ECS_CONTAINER_METADATA_URI']) + def initialize(container, metadata_uri: ENV.fetch('ECS_CONTAINER_METADATA_URI', nil)) @container = container @metadata_uri = URI(metadata_uri) @client = Backend::RequestClient.new(@metadata_uri.host, @metadata_uri.port, use_ssl: @metadata_uri.scheme == "https") diff --git a/lib/instana/snapshot/fargate_container.rb b/lib/instana/snapshot/fargate_container.rb index 1a560f1aa..208ae6b5a 100644 --- a/lib/instana/snapshot/fargate_container.rb +++ b/lib/instana/snapshot/fargate_container.rb @@ -8,7 +8,7 @@ module Snapshot class FargateContainer ID = 'com.instana.plugin.aws.ecs.container'.freeze - def initialize(container, metadata_uri: ENV['ECS_CONTAINER_METADATA_URI']) + def initialize(container, metadata_uri: ENV.fetch('ECS_CONTAINER_METADATA_URI', nil)) @container = container @metadata_uri = URI(metadata_uri) @client = Backend::RequestClient.new(@metadata_uri.host, @metadata_uri.port, use_ssl: @metadata_uri.scheme == "https") diff --git a/lib/instana/snapshot/fargate_process.rb b/lib/instana/snapshot/fargate_process.rb index b3d421bb2..f6906bf08 100644 --- a/lib/instana/snapshot/fargate_process.rb +++ b/lib/instana/snapshot/fargate_process.rb @@ -8,7 +8,7 @@ module Snapshot class FargateProcess ID = 'com.instana.plugin.process'.freeze - def initialize(metadata_uri: ENV['ECS_CONTAINER_METADATA_URI']) + def initialize(metadata_uri: ENV.fetch('ECS_CONTAINER_METADATA_URI', nil)) @metadata_uri = URI(metadata_uri) @client = Backend::RequestClient.new(@metadata_uri.host, @metadata_uri.port, use_ssl: @metadata_uri.scheme == "https") @start_time = Time.now diff --git a/lib/instana/snapshot/fargate_task.rb b/lib/instana/snapshot/fargate_task.rb index cc1eb7901..0a40330ea 100644 --- a/lib/instana/snapshot/fargate_task.rb +++ b/lib/instana/snapshot/fargate_task.rb @@ -8,7 +8,7 @@ module Snapshot class FargateTask ID = 'com.instana.plugin.aws.ecs.task'.freeze - def initialize(metadata_uri: ENV['ECS_CONTAINER_METADATA_URI']) + def initialize(metadata_uri: ENV.fetch('ECS_CONTAINER_METADATA_URI', nil)) @metadata_uri = URI(metadata_uri) @client = Backend::RequestClient.new(@metadata_uri.host, @metadata_uri.port, use_ssl: @metadata_uri.scheme == "https") end @@ -49,7 +49,7 @@ def task_metadata end def instana_zone - ENV['INSTANA_ZONE'] + ENV.fetch('INSTANA_ZONE', nil) end def instana_tags diff --git a/lib/instana/snapshot/google_cloud_run_instance.rb b/lib/instana/snapshot/google_cloud_run_instance.rb index 2e8f40bfd..93ff7ce9e 100644 --- a/lib/instana/snapshot/google_cloud_run_instance.rb +++ b/lib/instana/snapshot/google_cloud_run_instance.rb @@ -20,11 +20,11 @@ def data { runtime: 'ruby', region: gcp_region, - service: ENV['K_SERVICE'], - configuration: ENV['K_CONFIGURATION'], - revision: ENV['K_REVISION'], + service: ENV.fetch('K_SERVICE', nil), + configuration: ENV.fetch('K_CONFIGURATION', nil), + revision: ENV.fetch('K_REVISION', nil), instanceId: entity_id, - port: ENV['PORT'], + port: ENV.fetch('PORT', nil), numericProjectId: lookup('/computeMetadata/v1/project/numeric-project-id'), projectId: lookup('/computeMetadata/v1/project/project-id') }.reject { |_, v| v.nil? } @@ -47,7 +47,7 @@ def source end def host_name - "gcp:cloud-run:revision:#{ENV['K_REVISION']}" + "gcp:cloud-run:revision:#{ENV.fetch('K_REVISION', nil)}" end private diff --git a/lib/instana/snapshot/google_cloud_run_process.rb b/lib/instana/snapshot/google_cloud_run_process.rb index dc60d2601..b1fbfa753 100644 --- a/lib/instana/snapshot/google_cloud_run_process.rb +++ b/lib/instana/snapshot/google_cloud_run_process.rb @@ -31,7 +31,7 @@ def data start: @start_time.to_i * 1000, containerType: 'gcpCloudRunInstance', container: lookup('/computeMetadata/v1/instance/id'), - "com.instana.plugin.host.name": "gcp:cloud-run:revision:#{ENV['K_REVISION']}" + "com.instana.plugin.host.name": "gcp:cloud-run:revision:#{ENV.fetch('K_REVISION', nil)}" } end diff --git a/lib/instana/snapshot/lambda_function.rb b/lib/instana/snapshot/lambda_function.rb index 588c67cf2..0fb7071ad 100644 --- a/lib/instana/snapshot/lambda_function.rb +++ b/lib/instana/snapshot/lambda_function.rb @@ -8,7 +8,7 @@ class LambdaFunction ID = "com.instana.plugin.aws.lambda".freeze def entity_id - Thread.current[:instana_function_arn] + Fiber[:instana_function_arn] end def data diff --git a/lib/instana/span_filtering/configuration.rb b/lib/instana/span_filtering/configuration.rb index a1b300ed9..768bf38dc 100644 --- a/lib/instana/span_filtering/configuration.rb +++ b/lib/instana/span_filtering/configuration.rb @@ -80,7 +80,7 @@ def rules_loaded? # Load configuration from YAML file specified by INSTANA_CONFIG_PATH def load_from_yaml - config_path = ENV['INSTANA_CONFIG_PATH'] + config_path = ENV.fetch('INSTANA_CONFIG_PATH', nil) return unless config_path && File.exist?(config_path) begin @@ -130,9 +130,9 @@ def load_from_env_vars end end - return unless !ENV["INSTANA_TRACING_DISABLE"].nil? && !%w[True true 1].include?(ENV["INSTANA_TRACING_DISABLE"]) + return unless !ENV.fetch("INSTANA_TRACING_DISABLE", nil).nil? && !%w[True true 1].include?(ENV.fetch("INSTANA_TRACING_DISABLE", nil)) - process_disable_config(ENV["INSTANA_TRACING_DISABLE"].split(',')) + process_disable_config(ENV.fetch("INSTANA_TRACING_DISABLE").split(',')) end # Process rules from YAML configuration @@ -219,6 +219,8 @@ def update_instana_config_for_disabled_technology(technology) when :databases # If databases category is disabled, also disable redis ::Instana.config[:redis][:enabled] = false + else + nil # no Instana::Config toggle exists for other technologies end end end diff --git a/lib/instana/trace.rb b/lib/instana/trace.rb index 0e3bfa320..e14c5fd76 100644 --- a/lib/instana/trace.rb +++ b/lib/instana/trace.rb @@ -1,5 +1,10 @@ # (c) Copyright IBM Corp. 2025 require 'opentelemetry/context' +require 'instana/trace/span_context' +require 'instana/trace/span_kind' +require 'instana/trace/span' +require 'instana/trace/tracer' + module Instana # The Trace API allows recording a set of events, triggered as a result of a # single logical operation, consolidated across various components of an @@ -67,8 +72,3 @@ def non_recording_span(span_context) end end end - -require 'instana/trace/span_context' -require 'instana/trace/span_kind' -require 'instana/trace/span' -require 'instana/trace/tracer' diff --git a/lib/instana/trace/span.rb b/lib/instana/trace/span.rb index 4d6d45e1d..7e31b0592 100644 --- a/lib/instana/trace/span.rb +++ b/lib/instana/trace/span.rb @@ -535,7 +535,9 @@ def add_event(_name, attributes: nil, timestamp: nil) # rubocop:disable Lint/Unu # status, which is OK. # # @return [void] - def status=(status); end + def status=(status) + # Intentionally a no-op: Instana spans do not track an OTel status + end def should_collect_stack_trace? return false unless exit_span? diff --git a/lib/instana/util.rb b/lib/instana/util.rb index 114f757ea..b4117d504 100644 --- a/lib/instana/util.rb +++ b/lib/instana/util.rb @@ -1,6 +1,8 @@ # (c) Copyright IBM Corp. 2021 # (c) Copyright Instana Inc. 2016 +require 'net/http' + module Instana module Util # rubocop:disable Metrics/ModuleLength class << self @@ -71,7 +73,7 @@ def take_snapshot # :nocov: def get_app_name if ENV.key?('INSTANA_SERVICE_NAME') - return ENV['INSTANA_SERVICE_NAME'] + return ENV.fetch('INSTANA_SERVICE_NAME') end if defined?(::Resque) @@ -261,7 +263,6 @@ def should_mark_http_exit_as_error?(status_code) # Uses Ruby stdlib Net::HTTPResponse::CODE_TO_OBJ to derive the phrase from # the response class name (e.g. Net::HTTPUnauthorized → "Unauthorized"). def http_reason_phrase(status_code) - require 'net/http' klass = Net::HTTPResponse::CODE_TO_OBJ[status_code.to_s] return nil unless klass diff --git a/test/frameworks/sinatra_test.rb b/test/frameworks/sinatra_test.rb index 43e56462b..8c0230925 100644 --- a/test/frameworks/sinatra_test.rb +++ b/test/frameworks/sinatra_test.rb @@ -6,12 +6,10 @@ class SinatraTest < Minitest::Test include Rack::Test::Methods - APP = Rack::Builder.parse_file('test/support/apps/sinatra/config.ru') + parsed_app = Rack::Builder.parse_file('test/support/apps/sinatra/config.ru') sinatra_version = Gem::Specification.find_by_name('sinatra').version - if sinatra_version < Gem::Version.new('4.0.0') - APP = APP.first - end + APP = sinatra_version < Gem::Version.new('4.0.0') ? parsed_app.first : parsed_app def app APP diff --git a/test/instrumentation/rails_action_controller_test.rb b/test/instrumentation/rails_action_controller_test.rb index 29730fec8..747033fc7 100644 --- a/test/instrumentation/rails_action_controller_test.rb +++ b/test/instrumentation/rails_action_controller_test.rb @@ -5,16 +5,15 @@ class RailsActionControllerTest < Minitest::Test include Rack::Test::Methods + parsed_app = nil begin ENV['INSTANA_TEST'] = nil - APP = Rack::Builder.parse_file('test/support/apps/action_controller/config.ru') + parsed_app = Rack::Builder.parse_file('test/support/apps/action_controller/config.ru') ensure ENV['INSTANA_TEST'] = 'true' end railties_version = Gem::Specification.find_by_name('railties').version - if railties_version < Gem::Version.new('7.1.0') - APP = APP.first - end + APP = railties_version < Gem::Version.new('7.1.0') ? parsed_app.first : parsed_app def app APP diff --git a/test/instrumentation/rails_action_view_test.rb b/test/instrumentation/rails_action_view_test.rb index 825c1d4a3..8f4fc6c15 100644 --- a/test/instrumentation/rails_action_view_test.rb +++ b/test/instrumentation/rails_action_view_test.rb @@ -5,11 +5,9 @@ class RailsActionViewTest < Minitest::Test include Rack::Test::Methods - APP = Rack::Builder.parse_file('test/support/apps/action_view/config.ru') + parsed_app = Rack::Builder.parse_file('test/support/apps/action_view/config.ru') railties_version = Gem::Specification.find_by_name('railties').version - if railties_version < Gem::Version.new('7.1.0') - APP = APP.first - end + APP = railties_version < Gem::Version.new('7.1.0') ? parsed_app.first : parsed_app def app APP diff --git a/test/instrumentation/rails_active_job_test.rb b/test/instrumentation/rails_active_job_test.rb index dd0f4e025..5883b89c1 100644 --- a/test/instrumentation/rails_active_job_test.rb +++ b/test/instrumentation/rails_active_job_test.rb @@ -10,7 +10,9 @@ class RailsActiveJobTest < Minitest::Test class SampleJob < ActiveJob::Base queue_as :test_queue - def perform(*args); end + def perform(*args) + # Intentionally empty: the test only checks enqueueing + end end def setup diff --git a/test/snapshot/lambda_function_test.rb b/test/snapshot/lambda_function_test.rb index 0d0ae6c68..8c84015af 100644 --- a/test/snapshot/lambda_function_test.rb +++ b/test/snapshot/lambda_function_test.rb @@ -9,29 +9,29 @@ def setup end def test_snapshot - Thread.current[:instana_function_arn] = 'test' + Fiber[:instana_function_arn] = 'test' assert_equal Instana::Snapshot::LambdaFunction::ID, @subject.snapshot[:name] - assert_equal Thread.current[:instana_function_arn], @subject.snapshot[:entityId] + assert_equal Fiber[:instana_function_arn], @subject.snapshot[:entityId] ensure - Thread.current[:instana_function_arn] = nil + Fiber[:instana_function_arn] = nil end def test_source - Thread.current[:instana_function_arn] = 'test' + Fiber[:instana_function_arn] = 'test' assert @subject.source[:hl] assert_equal 'aws', @subject.source[:cp] - assert_equal Thread.current[:instana_function_arn], @subject.source[:e] + assert_equal Fiber[:instana_function_arn], @subject.source[:e] ensure - Thread.current[:instana_function_arn] = nil + Fiber[:instana_function_arn] = nil end def test_host_name - Thread.current[:instana_function_arn] = 'test' + Fiber[:instana_function_arn] = 'test' - assert_equal Thread.current[:instana_function_arn], @subject.host_name + assert_equal Fiber[:instana_function_arn], @subject.host_name ensure - Thread.current[:instana_function_arn] = nil + Fiber[:instana_function_arn] = nil end end diff --git a/test/support/apps/grpc/boot.rb b/test/support/apps/grpc/boot.rb index 483f8ad79..d415d7aab 100644 --- a/test/support/apps/grpc/boot.rb +++ b/test/support/apps/grpc/boot.rb @@ -5,18 +5,16 @@ ::Instana.logger.info "Booting instrumented gRPC server on port 50051 for tests." -grpc_thread = Thread.new do - s = GRPC::RpcServer.new - Thread.current[:server] = s - - s.add_http2_port('127.0.0.1:50051', :this_port_is_insecure) - s.handle(PingPongServer) - s.run_till_terminated +grpc_server = GRPC::RpcServer.new +Thread.new do + grpc_server.add_http2_port('127.0.0.1:50051', :this_port_is_insecure) + grpc_server.handle(PingPongServer) + grpc_server.run_till_terminated end Minitest.after_run do ::Instana.logger.info "Killing gRPC server" - grpc_thread[:server].stop + grpc_server.stop sleep 2 end diff --git a/test/support/apps/grpc/grpc_server.rb b/test/support/apps/grpc/grpc_server.rb index 557b2e5b5..e1909e782 100644 --- a/test/support/apps/grpc/grpc_server.rb +++ b/test/support/apps/grpc/grpc_server.rb @@ -3,6 +3,7 @@ require 'google/protobuf' require 'google/protobuf/descriptor_pb' +require 'grpc' # The `DescriptorPool#build` DSL was removed in google-protobuf 4.x, so the # messages are registered through a serialized file descriptor, which works @@ -40,7 +41,6 @@ module PingPongService PingRequest = Google::Protobuf::DescriptorPool.generated_pool.lookup("PingPongService.PingRequest").msgclass PongReply = Google::Protobuf::DescriptorPool.generated_pool.lookup("PingPongService.PongReply").msgclass end -require 'grpc' module PingPongService # The greeting service definition. diff --git a/test/support/apps/sidekiq/jobs/sidekiq_job_1.rb b/test/support/apps/sidekiq/jobs/sidekiq_job_1.rb index 891576f8f..3aaec1fcf 100644 --- a/test/support/apps/sidekiq/jobs/sidekiq_job_1.rb +++ b/test/support/apps/sidekiq/jobs/sidekiq_job_1.rb @@ -5,5 +5,6 @@ class SidekiqJobOne include Sidekiq::Worker def perform(a, b, c) + # Intentionally empty: the test only checks that the job is traced end end diff --git a/test/support/apps/sidekiq/worker.rb b/test/support/apps/sidekiq/worker.rb index 4f651f536..944c3e663 100644 --- a/test/support/apps/sidekiq/worker.rb +++ b/test/support/apps/sidekiq/worker.rb @@ -22,16 +22,16 @@ cli.send :options end -sidekiq_thread = Thread.new do +launcher = nil +Thread.new do launcher = ::Sidekiq::Launcher.new( config_or_options ) launcher.run - Thread.current[:worker] = launcher end Minitest.after_run do ::Instana.logger.info "Killing Sidekiq worker" - sidekiq_thread[:worker].stop + launcher.stop sleep 1 end diff --git a/test/support/helpers.rb b/test/support/helpers.rb index 432abd284..92c7a5790 100644 --- a/test/support/helpers.rb +++ b/test/support/helpers.rb @@ -53,6 +53,8 @@ def find_first_span_by_name(spans, name) return span if span[:data][:sdk][:name] == name when name return span + else + next # not the span we are looking for end end raise StandardError, "Span (#{name}) not found"