diff --git a/lib/instana/exporter/otlp/aws_converter.rb b/lib/instana/exporter/otlp/aws_converter.rb index 66068a5e..a9526724 100644 --- a/lib/instana/exporter/otlp/aws_converter.rb +++ b/lib/instana/exporter/otlp/aws_converter.rb @@ -15,6 +15,21 @@ module Exporter module Otlp # Converter for AWS SDK spans (SQS, SNS, DynamoDB) to OTLP format class AwsConverter < BaseConverter + LAMBDA_TRIGGER_MAP = { + 'aws:api.gateway' => 'http', + 'aws:api.gateway.noproxy' => 'http', + 'aws:application.load.balancer' => 'http', + 'aws:s3' => 'datasource', + 'aws:dynamodb' => 'datasource', + 'aws:sqs' => 'pubsub', + 'aws:sns' => 'pubsub', + 'aws:kinesis' => 'datasource', + 'aws:kinesis.firehouse' => 'datasource', + 'aws:cloudwatch.events' => 'timer', + 'aws:cloudwatch.logs' => 'datasource', + 'aws:lambda.invoke' => 'other' + }.freeze + # Build OTel-compliant span name for AWS SDK spans # # Formulas per SPAN_NAME_PATTERNS.txt Section 6: @@ -24,6 +39,7 @@ class AwsConverter < BaseConverter # DynamoDB → "DynamoDB.{op}" e.g. "DynamoDB.PutItem" # S3 → "S3.{op}" e.g. "S3.PutObject" # Lambda invoke → "Lambda.{function}" + # Lambda entry → "{functionName}" # # @return [String] The span name def span_name @@ -32,7 +48,8 @@ def span_name sns_span_name(data[:sns]) || dynamodb_span_name(data[:dynamodb]) || s3_span_name(data[:s3]) || - lambda_span_name(data.dig(:aws, :lambda, :invoke)) || + lambda_invoke_span_name(data.dig(:aws, :lambda, :invoke)) || + lambda_entry_span_name(data[:lambda]) || super end @@ -45,7 +62,8 @@ def convert_attributes convert_sns_attributes(attributes, data[:sns]) convert_dynamodb_attributes(attributes, data[:dynamodb]) convert_s3_attributes(attributes, data[:s3]) - convert_lambda_attributes(attributes, data.dig(:aws, :lambda, :invoke)) + convert_lambda_invoke_attributes(attributes, data.dig(:aws, :lambda, :invoke)) + convert_lambda_entry_attributes(attributes, data[:lambda]) attributes end @@ -82,11 +100,18 @@ def s3_span_name(s3_data) op.empty? ? 'S3' : "S3.#{op}" end - def lambda_span_name(lambda_data) + def lambda_invoke_span_name(lambda_data) return unless lambda_data fn = lambda_data[:function].to_s.strip - fn.empty? ? 'Lambda.invoke' : "Lambda.#{fn}" + fn.empty? ? 'Lambda.Invoke' : "Lambda.#{fn}" + end + + def lambda_entry_span_name(lambda_data) + return unless lambda_data + + fn = lambda_data[:functionName].to_s.strip + fn.empty? ? nil : fn end def convert_sqs_attributes(attributes, sqs_data) @@ -94,6 +119,7 @@ def convert_sqs_attributes(attributes, sqs_data) add_attribute(attributes, OpenTelemetry::SemConv::Incubating::MESSAGING::MESSAGING_SYSTEM, 'aws_sqs') add_attribute(attributes, OpenTelemetry::SemConv::Incubating::MESSAGING::MESSAGING_DESTINATION_NAME, sqs_data[:queue]) + add_attribute(attributes, OpenTelemetry::SemConv::Incubating::MESSAGING::MESSAGING_OPERATION_NAME, sqs_data[:type] || 'send') add_attribute(attributes, 'messaging.aws.sqs.message_group_id', sqs_data[:group]) add_attribute(attributes, OpenTelemetry::SemConv::Incubating::MESSAGING::MESSAGING_BATCH_MESSAGE_COUNT, sqs_data[:size]) add_attribute(attributes, OpenTelemetry::SemConv::Incubating::MESSAGING::MESSAGING_OPERATION_TYPE, sqs_operation_type(sqs_data[:type])) @@ -111,8 +137,9 @@ def sqs_operation_type(type) def convert_sns_attributes(attributes, sns_data) return unless sns_data - add_attribute(attributes, OpenTelemetry::SemConv::Incubating::MESSAGING::MESSAGING_SYSTEM, 'aws_sns') - add_attribute(attributes, OpenTelemetry::SemConv::Incubating::MESSAGING::MESSAGING_DESTINATION_NAME, sns_data[:topic]) + add_attribute(attributes, OpenTelemetry::SemConv::Incubating::MESSAGING::MESSAGING_SYSTEM, 'aws.sns') + add_attribute(attributes, OpenTelemetry::SemConv::Incubating::MESSAGING::MESSAGING_DESTINATION_NAME, sns_data[:topic] || sns_data[:target]) + add_attribute(attributes, OpenTelemetry::SemConv::Incubating::MESSAGING::MESSAGING_OPERATION_NAME, 'send') add_attribute(attributes, 'messaging.aws.sns.target_arn', sns_data[:target]) add_attribute(attributes, 'messaging.aws.sns.phone_number', sns_data[:phone]) add_attribute(attributes, 'messaging.aws.sns.subject', sns_data[:subject]) @@ -122,10 +149,9 @@ def convert_sns_attributes(attributes, sns_data) def convert_dynamodb_attributes(attributes, dynamodb_data) return unless dynamodb_data - add_attribute(attributes, OpenTelemetry::SemConv::DB::DB_SYSTEM_NAME, 'dynamodb') - add_attribute(attributes, OpenTelemetry::SemConv::DB::DB_OPERATION_NAME, dynamodb_data[:op]) - add_attribute(attributes, OpenTelemetry::SemConv::DB::DB_NAMESPACE, dynamodb_data[:table]) - add_attribute(attributes, 'aws.dynamodb.table_name', dynamodb_data[:table]) + add_attribute(attributes, OpenTelemetry::SemConv::DB::DB_SYSTEM_NAME, 'aws.dynamodb') + add_attribute(attributes, OpenTelemetry::SemConv::DB::DB_OPERATION_NAME, dynamodb_data[:op] || dynamodb_data[:operation]) + add_attribute(attributes, 'aws.dynamodb.table_names', [dynamodb_data[:table]].compact) if dynamodb_data[:table] end def convert_s3_attributes(attributes, s3_data) @@ -145,13 +171,41 @@ def s3_rpc_method(operation) "S3.#{operation}" end - def convert_lambda_attributes(attributes, lambda_data) + def convert_lambda_invoke_attributes(attributes, lambda_data) return unless lambda_data - add_attribute(attributes, 'aws.service', 'lambda') - add_attribute(attributes, 'aws.lambda.function_name', lambda_data[:function]) - add_attribute(attributes, 'aws.lambda.invocation_type', lambda_data[:type]) + add_attribute(attributes, Instana::Exporter::Otlp::RPC_SYSTEM_ATTR, 'aws-api') + add_attribute(attributes, OpenTelemetry::SemConv::Incubating::RPC::RPC_METHOD, 'Lambda.Invoke') + add_attribute(attributes, OpenTelemetry::SemConv::Incubating::CLOUD::CLOUD_PROVIDER, 'aws') add_attribute(attributes, OpenTelemetry::SemConv::Incubating::FAAS::FAAS_INVOKED_NAME, lambda_data[:function]) + add_attribute(attributes, OpenTelemetry::SemConv::Incubating::FAAS::FAAS_INVOKED_PROVIDER, 'aws') + end + + def convert_lambda_entry_attributes(attributes, lambda_data) + return unless lambda_data + + trigger = LAMBDA_TRIGGER_MAP[lambda_data[:trigger].to_s] || 'other' if lambda_data[:trigger] + add_attribute(attributes, OpenTelemetry::SemConv::Incubating::FAAS::FAAS_TRIGGER, trigger) + add_attribute(attributes, OpenTelemetry::SemConv::Incubating::FAAS::FAAS_INVOCATION_ID, lambda_data[:reqId]) + if lambda_data.key?(:coldStart) + cold_start = lambda_data[:coldStart].to_s == 'true' || lambda_data[:coldStart] == true + add_attribute(attributes, OpenTelemetry::SemConv::Incubating::FAAS::FAAS_COLDSTART, cold_start) + end + add_attribute(attributes, OpenTelemetry::SemConv::Incubating::FAAS::FAAS_NAME, lambda_data[:functionName]) + add_attribute(attributes, OpenTelemetry::SemConv::Incubating::FAAS::FAAS_VERSION, lambda_data[:functionVersion]) + add_attribute(attributes, OpenTelemetry::SemConv::Incubating::CLOUD::CLOUD_PROVIDER, 'aws') + add_attribute(attributes, OpenTelemetry::SemConv::Incubating::CLOUD::CLOUD_PLATFORM, 'aws_lambda') + + arn = lambda_data[:arn].to_s + if arn =~ /^arn:aws:lambda:([^:]+):([^:]+):function:/ + add_attribute(attributes, OpenTelemetry::SemConv::Incubating::CLOUD::CLOUD_REGION, Regexp.last_match(1)) + add_attribute(attributes, OpenTelemetry::SemConv::Incubating::CLOUD::CLOUD_ACCOUNT_ID, Regexp.last_match(2)) + add_attribute(attributes, OpenTelemetry::SemConv::Incubating::CLOUD::CLOUD_RESOURCE_ID, arn) + elsif !arn.empty? + add_attribute(attributes, OpenTelemetry::SemConv::Incubating::CLOUD::CLOUD_RESOURCE_ID, arn) + end + + add_attribute(attributes, 'exception.message', lambda_data[:error]) if lambda_data[:error] end end end diff --git a/lib/instana/exporter/otlp/background_job_converter.rb b/lib/instana/exporter/otlp/background_job_converter.rb index 4092bb9f..9f995c5c 100644 --- a/lib/instana/exporter/otlp/background_job_converter.rb +++ b/lib/instana/exporter/otlp/background_job_converter.rb @@ -12,9 +12,12 @@ module Otlp class BackgroundJobConverter < BaseConverter # Build OTel-compliant span name for background-job spans # - # Formula per SPAN_NAME_PATTERNS.txt Section 3 (sidekiq / resque): - # client/producer → "{queue} publish" - # worker/consumer → "{queue} process" + # Formula per SPAN_NAME_PATTERNS.txt Section 3 (sidekiq / resque / activejob): + # sidekiq client → "{queue} publish" + # sidekiq worker → "{queue} consume" + # resque client → "{queue} publish" + # resque worker → "{queue} process" + # activejob → "{queue} {action}" # # @return [String] The span name def span_name @@ -25,7 +28,19 @@ def span_name queue = job_data[:queue] || job_data['queue'] queue = queue.to_s.strip - operation = span_type.end_with?('-client') ? 'publish' : 'process' + operation = case span_type + when 'sidekiq-client', 'resque-client' + 'publish' + when 'sidekiq-worker' + 'consume' + when 'resque-worker' + 'process' + when 'activejob' + job_data[:action] || job_data['action'] || 'process' + else + 'process' + end + queue.empty? ? operation : "#{queue} #{operation}" end @@ -33,16 +48,17 @@ def convert_attributes attributes = {} span_type = span[:n].to_s - if span_type == 'sidekiq-client' # rubocop:disable Style/CaseLikeIf - convert_job_attributes(attributes, span[:'sidekiq-client'] || span[:data]&.[](:'sidekiq-client'), 'sidekiq', 'publish') - elsif span_type == 'sidekiq-worker' - convert_job_attributes(attributes, span[:'sidekiq-worker'] || span[:data]&.[](:'sidekiq-worker'), 'sidekiq', 'process') - elsif span_type == 'resque-client' - convert_job_attributes(attributes, span[:'resque-client'] || span[:data]&.[](:'resque-client'), 'resque', 'publish') - elsif span_type == 'resque-worker' - convert_job_attributes(attributes, span[:'resque-worker'] || span[:data]&.[](:'resque-worker'), 'resque', 'process') - elsif span_type == 'activejob' - convert_activejob_attributes(attributes, span[:data]&.[](:activejob)) + case span_type + when 'sidekiq-client' + convert_job_attributes(attributes, span[:'sidekiq-client'] || span[:data]&.[](:'sidekiq-client'), 'sidekiq', 'publish', 'send') + when 'sidekiq-worker' + convert_job_attributes(attributes, span[:'sidekiq-worker'] || span[:data]&.[](:'sidekiq-worker'), 'sidekiq', 'consume', 'process') + when 'resque-client' + convert_job_attributes(attributes, span[:'resque-client'] || span[:data]&.[](:'resque-client'), 'resque', 'publish', 'send') + when 'resque-worker' + convert_job_attributes(attributes, span[:'resque-worker'] || span[:data]&.[](:'resque-worker'), 'resque', 'process', 'process') + when 'activejob' + convert_activejob_attributes(attributes, span[:activejob] || span[:data]&.[](:activejob)) end attributes @@ -50,14 +66,14 @@ def convert_attributes private - def convert_job_attributes(attributes, data, system, operation) + def convert_job_attributes(attributes, data, system, operation_name, operation_type) return unless data add_attribute(attributes, OpenTelemetry::SemConv::Incubating::MESSAGING::MESSAGING_SYSTEM, system) add_attribute(attributes, OpenTelemetry::SemConv::Incubating::MESSAGING::MESSAGING_DESTINATION_NAME, data[:queue] || data['queue']) - add_attribute(attributes, OpenTelemetry::SemConv::Incubating::MESSAGING::MESSAGING_OPERATION_NAME, operation) - add_attribute(attributes, OpenTelemetry::SemConv::Incubating::MESSAGING::MESSAGING_MESSAGE_ID, data[:job_id] || data['job_id']) - add_attribute(attributes, OpenTelemetry::SemConv::Incubating::MESSAGING::MESSAGING_CONSUMER_GROUP_NAME, data[:job] || data['job']) + add_attribute(attributes, OpenTelemetry::SemConv::Incubating::MESSAGING::MESSAGING_OPERATION_NAME, operation_name) + add_attribute(attributes, OpenTelemetry::SemConv::Incubating::MESSAGING::MESSAGING_OPERATION_TYPE, operation_type) + add_attribute(attributes, OpenTelemetry::SemConv::Incubating::MESSAGING::MESSAGING_MESSAGE_ID, data[:job_id] || data['job_id']) unless system == 'resque' add_attribute(attributes, OpenTelemetry::SemConv::SERVER::SERVER_ADDRESS, extract_host(data[:'redis-url'] || data['redis-url'])) add_attribute(attributes, OpenTelemetry::SemConv::SERVER::SERVER_PORT, extract_port(data[:'redis-url'] || data['redis-url'])) end @@ -65,9 +81,13 @@ def convert_job_attributes(attributes, data, system, operation) def convert_activejob_attributes(attributes, data) return unless data + action = data[:action] || data['action'] || 'perform' + operation_type = action.to_s == 'perform' ? 'process' : 'send' + add_attribute(attributes, OpenTelemetry::SemConv::Incubating::MESSAGING::MESSAGING_SYSTEM, 'activejob') add_attribute(attributes, OpenTelemetry::SemConv::Incubating::MESSAGING::MESSAGING_DESTINATION_NAME, data[:queue] || data['queue']) - add_attribute(attributes, OpenTelemetry::SemConv::Incubating::MESSAGING::MESSAGING_OPERATION_NAME, data[:action] || data['action']) + add_attribute(attributes, OpenTelemetry::SemConv::Incubating::MESSAGING::MESSAGING_OPERATION_NAME, action) + add_attribute(attributes, OpenTelemetry::SemConv::Incubating::MESSAGING::MESSAGING_OPERATION_TYPE, operation_type) add_attribute(attributes, OpenTelemetry::SemConv::Incubating::MESSAGING::MESSAGING_MESSAGE_ID, data[:job_id] || data['job_id']) end diff --git a/lib/instana/exporter/otlp/converter_factory.rb b/lib/instana/exporter/otlp/converter_factory.rb index 7cdaf28f..5ed5fefb 100644 --- a/lib/instana/exporter/otlp/converter_factory.rb +++ b/lib/instana/exporter/otlp/converter_factory.rb @@ -104,7 +104,7 @@ def messaging_span?(span) # Check if span is a background job span def background_job_span?(span) - span[:n]&.match?(/sidekiq-(client|worker)|resque-(client|worker)/i) + span[:n]&.match?(/sidekiq-(client|worker)|resque-(client|worker)|activejob/i) end # Check if span is a Rails span diff --git a/lib/instana/exporter/otlp/database_converter.rb b/lib/instana/exporter/otlp/database_converter.rb index 21c604c9..a446e7f7 100644 --- a/lib/instana/exporter/otlp/database_converter.rb +++ b/lib/instana/exporter/otlp/database_converter.rb @@ -26,11 +26,11 @@ def convert_attributes # Build OTel-compliant span name for database spans # - # Formulas per SPAN_NAME_PATTERNS.txt Section 2: - # activerecord / sequel → "{adapter} {db}" e.g. "mysql2 myapp" - # redis → "redis {command}" e.g. "redis GET" - # memcache → "memcached {command}" e.g. "memcached get" - # mongo → "{namespace}.{command}" e.g. "users.find" + # Formulas per OTel DB SemConv v1.41.0: + # activerecord / sequel → "{operation} {db}.{table}" / "{operation} {db}" / "{operation}" + # redis → "{command}" e.g. "GET" + # memcache → "{command}" e.g. "get" + # mongo → "{collection}.{command}" e.g. "users.find" # # @return [String] The span name def span_name @@ -45,57 +45,81 @@ def span_name private + def extract_sql_operation(sql) + return unless sql + + first_word = sql.to_s.strip.split(/\s+/).first + first_word&.upcase + end + def activerecord_span_name(ar_data) return unless ar_data - parts = [ar_data[:adapter], ar_data[:db]].compact.reject(&:empty?) - parts.empty? ? 'activerecord' : parts.join(' ') + op = extract_sql_operation(ar_data[:sql]) + target = [ar_data[:db], ar_data[:table]].compact.reject(&:empty?).join('.') + parts = [op, target].reject { |p| p.nil? || p.empty? } + parts.empty? ? normalize_db_system(ar_data[:adapter]) : parts.join(' ') end def sequel_span_name(seq) return unless seq - parts = [seq[:adapter], seq[:db]].compact.reject(&:empty?) - parts.empty? ? 'sequel' : parts.join(' ') + op = extract_sql_operation(seq[:sql]) + target = [seq[:db], seq[:table]].compact.reject(&:empty?).join('.') + parts = [op, target].reject { |p| p.nil? || p.empty? } + parts.empty? ? normalize_db_system(seq[:adapter]) : parts.join(' ') end def redis_span_name(redis) return unless redis cmd = redis[:command].to_s.strip - cmd.empty? ? 'redis' : "redis #{cmd}" + cmd.empty? ? 'redis' : cmd end def memcache_span_name(mc_data) return unless mc_data cmd = mc_data[:command].to_s.strip - cmd.empty? ? 'memcached' : "memcached #{cmd}" + cmd.empty? ? 'memcached' : cmd end def mongo_span_name(mongo) return unless mongo + col = mongo[:collection].to_s.strip ns = mongo[:namespace].to_s.strip cmd = mongo[:command].to_s.strip - parts = [ns, cmd].reject(&:empty?) + + target = col.empty? ? ns : col + parts = [target, cmd].reject(&:empty?) parts.empty? ? 'mongodb' : parts.join('.') end - # Normalize an ActiveRecord/Sequel adapter name to OTel db.system.name. - # Only adapters that are actively instrumented and tested are listed here. + # Normalize an adapter name to OTel db.system.name. + # Fallback to 'other_sql' for unlisted SQL adapters. DB_SYSTEM_NORMALIZATION = { 'mysql2' => 'mysql', + 'mysql' => 'mysql', + 'trilogy' => 'mysql', 'sqlite3' => 'sqlite', + 'sqlite' => 'sqlite', 'postgresql' => 'postgresql', - 'mysql' => 'mysql', - 'sqlite' => 'sqlite' + 'postgres' => 'postgresql', + 'mariadb' => 'mariadb', + 'mssql' => 'mssql', + 'sqlserver' => 'mssql', + 'oracle_enhanced' => 'oracle.db', + 'oracle' => 'oracle.db', + 'ibm_db' => 'ibm.db2', + 'db2' => 'ibm.db2', + 'sql' => 'other_sql' }.freeze def normalize_db_system(adapter) - return unless adapter + return 'other_sql' unless adapter - DB_SYSTEM_NORMALIZATION.fetch(adapter.to_s.downcase, adapter.to_s.downcase) + DB_SYSTEM_NORMALIZATION.fetch(adapter.to_s.downcase, 'other_sql') end def convert_activerecord_attributes(attributes, ar_data) @@ -104,8 +128,10 @@ def convert_activerecord_attributes(attributes, ar_data) add_attribute(attributes, OpenTelemetry::SemConv::DB::DB_SYSTEM_NAME, normalize_db_system(ar_data[:adapter])) add_attribute(attributes, OpenTelemetry::SemConv::DB::DB_NAMESPACE, ar_data[:db]) add_attribute(attributes, OpenTelemetry::SemConv::DB::DB_QUERY_TEXT, ar_data[:sql]) - add_attribute(attributes, OpenTelemetry::SemConv::Incubating::DB::DB_USER, ar_data[:username]) - add_attribute(attributes, OpenTelemetry::SemConv::SERVER::SERVER_ADDRESS, ar_data[:host]) + add_attribute(attributes, OpenTelemetry::SemConv::DB::DB_OPERATION_NAME, extract_sql_operation(ar_data[:sql])) + add_attribute(attributes, 'db.user', ar_data[:username]) + add_attribute(attributes, OpenTelemetry::SemConv::SERVER::SERVER_ADDRESS, extract_host(ar_data[:host])) + add_attribute(attributes, OpenTelemetry::SemConv::SERVER::SERVER_PORT, ar_data[:port]&.to_i || extract_port(ar_data[:host])) add_attribute(attributes, OpenTelemetry::SemConv::Incubating::DB::DB_COLLECTION_NAME, ar_data[:table]) end @@ -115,8 +141,10 @@ def convert_sequel_attributes(attributes, seq_data) add_attribute(attributes, OpenTelemetry::SemConv::DB::DB_SYSTEM_NAME, normalize_db_system(seq_data[:adapter])) add_attribute(attributes, OpenTelemetry::SemConv::DB::DB_NAMESPACE, seq_data[:db]) add_attribute(attributes, OpenTelemetry::SemConv::DB::DB_QUERY_TEXT, seq_data[:sql]) - add_attribute(attributes, OpenTelemetry::SemConv::Incubating::DB::DB_USER, seq_data[:username]) - add_attribute(attributes, OpenTelemetry::SemConv::SERVER::SERVER_ADDRESS, seq_data[:host]) + add_attribute(attributes, OpenTelemetry::SemConv::DB::DB_OPERATION_NAME, extract_sql_operation(seq_data[:sql])) + add_attribute(attributes, 'db.user', seq_data[:username]) + add_attribute(attributes, OpenTelemetry::SemConv::SERVER::SERVER_ADDRESS, extract_host(seq_data[:host])) + add_attribute(attributes, OpenTelemetry::SemConv::SERVER::SERVER_PORT, seq_data[:port]&.to_i || extract_port(seq_data[:host])) add_attribute(attributes, OpenTelemetry::SemConv::Incubating::DB::DB_COLLECTION_NAME, seq_data[:table]) end @@ -125,7 +153,6 @@ def convert_redis_attributes(attributes, redis_data) add_attribute(attributes, OpenTelemetry::SemConv::DB::DB_SYSTEM_NAME, 'redis') add_attribute(attributes, OpenTelemetry::SemConv::DB::DB_OPERATION_NAME, redis_data[:command]) - add_attribute(attributes, OpenTelemetry::SemConv::DB::DB_QUERY_TEXT, redis_data[:command]) add_attribute(attributes, OpenTelemetry::SemConv::DB::DB_NAMESPACE, redis_data[:db]&.to_s) add_attribute(attributes, OpenTelemetry::SemConv::SERVER::SERVER_ADDRESS, extract_host(redis_data[:connection])) add_attribute(attributes, OpenTelemetry::SemConv::SERVER::SERVER_PORT, extract_port(redis_data[:connection])) @@ -136,8 +163,6 @@ def convert_memcache_attributes(attributes, mc_data) add_attribute(attributes, OpenTelemetry::SemConv::DB::DB_SYSTEM_NAME, 'memcached') add_attribute(attributes, OpenTelemetry::SemConv::DB::DB_OPERATION_NAME, mc_data[:command]) - add_attribute(attributes, 'db.memcached.key', mc_data[:key]) - add_attribute(attributes, 'db.memcached.keys', mc_data[:keys]) add_attribute(attributes, OpenTelemetry::SemConv::DB::DB_NAMESPACE, mc_data[:namespace]) add_attribute(attributes, OpenTelemetry::SemConv::SERVER::SERVER_ADDRESS, extract_host(mc_data[:server])) add_attribute(attributes, OpenTelemetry::SemConv::SERVER::SERVER_PORT, extract_port(mc_data[:server])) diff --git a/lib/instana/exporter/otlp/graphql_converter.rb b/lib/instana/exporter/otlp/graphql_converter.rb index 58850738..b307cca5 100644 --- a/lib/instana/exporter/otlp/graphql_converter.rb +++ b/lib/instana/exporter/otlp/graphql_converter.rb @@ -39,10 +39,15 @@ def convert_attributes add_attribute(attributes, OpenTelemetry::SemConv::Incubating::GRAPHQL::GRAPHQL_OPERATION_NAME, graphql_data[:operationName]) add_attribute(attributes, OpenTelemetry::SemConv::Incubating::GRAPHQL::GRAPHQL_OPERATION_TYPE, graphql_data[:operationType]) - add_attribute(attributes, OpenTelemetry::SemConv::Incubating::GRAPHQL::GRAPHQL_DOCUMENT, format_fields(graphql_data[:fields])) - # Add arguments as custom attribute - add_attribute(attributes, 'graphql.arguments', format_arguments(graphql_data[:arguments])) if graphql_data[:arguments] + doc = graphql_data[:document] || format_fields(graphql_data[:fields]) + add_attribute(attributes, OpenTelemetry::SemConv::Incubating::GRAPHQL::GRAPHQL_DOCUMENT, doc) + + if graphql_data[:errors] + error_val = graphql_data[:errors] + error_str = error_val.is_a?(Array) ? error_val.join(', ') : error_val.to_s + add_attribute(attributes, 'error.type', error_str) + end attributes end @@ -54,12 +59,6 @@ def format_fields(fields) fields.map { |obj, flds| "#{obj} { #{flds.join(', ')} }" }.join(', ') end - - def format_arguments(arguments) - return nil unless arguments - - arguments.map { |obj, args| "#{obj}(#{args.join(', ')})" }.join(', ') - end end end end diff --git a/lib/instana/exporter/otlp/http_converter.rb b/lib/instana/exporter/otlp/http_converter.rb index 332271e6..09daf2db 100644 --- a/lib/instana/exporter/otlp/http_converter.rb +++ b/lib/instana/exporter/otlp/http_converter.rb @@ -5,6 +5,7 @@ require_relative 'base_converter' require 'opentelemetry/semconv/http' require 'opentelemetry/semconv/url' +require 'opentelemetry/semconv/incubating/url' require 'opentelemetry/semconv/server' require 'opentelemetry/semconv/user_agent' require 'opentelemetry/semconv/network' @@ -21,16 +22,20 @@ def convert_attributes attributes = {} http_data = span[:data]&.[](:http) || {} + template = http_data[:path_tpl] || http_data[:route_id] + path = http_data[:path] || http_data[:context_root] + add_attribute(attributes, OpenTelemetry::SemConv::HTTP::HTTP_REQUEST_METHOD, http_data[:method]) add_attribute(attributes, OpenTelemetry::SemConv::URL::URL_FULL, http_data[:url]) - add_attribute(attributes, OpenTelemetry::SemConv::URL::URL_PATH, http_data[:path]) + add_attribute(attributes, OpenTelemetry::SemConv::URL::URL_PATH, path) + add_attribute(attributes, OpenTelemetry::SemConv::Incubating::URL::URL_TEMPLATE, template) add_attribute(attributes, OpenTelemetry::SemConv::URL::URL_QUERY, http_data[:params]) add_attribute(attributes, OpenTelemetry::SemConv::SERVER::SERVER_ADDRESS, extract_host(http_data[:host])) add_attribute(attributes, OpenTelemetry::SemConv::SERVER::SERVER_PORT, extract_port(http_data[:host], http_data[:url])) add_attribute(attributes, OpenTelemetry::SemConv::URL::URL_SCHEME, extract_scheme(http_data[:url])) add_attribute(attributes, OpenTelemetry::SemConv::HTTP::HTTP_RESPONSE_STATUS_CODE, http_data[:status]) - add_attribute(attributes, OpenTelemetry::SemConv::USER_AGENT::USER_AGENT_ORIGINAL, http_data.dig(:header, 'user-agent')) + add_header_attributes(attributes, http_data[:header]) add_protocol_attributes(attributes, http_data[:protocol]) attributes @@ -56,7 +61,7 @@ def build_status(error_count, error_msg) # Build OTel-compliant span name for HTTP spans # - # Convention (stable): "{METHOD}" or "{METHOD} {url.template/path}" + # Convention (stable): "{METHOD}" or "{METHOD} {url.template}" # Falls back to "HTTP" when no method is present. # # @return [String] The span name @@ -65,8 +70,8 @@ def span_name method = http_data[:method].to_s.upcase method = 'HTTP' if method.empty? - path = http_data[:path].to_s.strip - path.empty? ? method : "#{method} #{path}" + template = (http_data[:path_tpl] || http_data[:route_id]).to_s.strip + template.empty? ? method : "#{method} #{template}" end private @@ -111,6 +116,24 @@ def extract_port(host_str, url) nil end + # Map headers to user_agent.original or http.request.header. + # @param attributes [Hash] + # @param headers [Hash, nil] + def add_header_attributes(attributes, headers) + return unless headers.is_a?(Hash) + + headers.each do |key, value| + next unless value + + norm_key = key.to_s.downcase.tr('_', '-') + if norm_key == 'user-agent' + add_attribute(attributes, OpenTelemetry::SemConv::USER_AGENT::USER_AGENT_ORIGINAL, value) + else + add_attribute(attributes, "http.request.header.#{norm_key}", value) + end + end + end + # Emit network.protocol.name and network.protocol.version from e.g. "HTTP/1.1" # @param attributes [Hash] # @param protocol [String, nil] e.g. "HTTP/1.1" or "h2" diff --git a/lib/instana/exporter/otlp/messaging_converter.rb b/lib/instana/exporter/otlp/messaging_converter.rb index 2d9ca138..ed684f8e 100644 --- a/lib/instana/exporter/otlp/messaging_converter.rb +++ b/lib/instana/exporter/otlp/messaging_converter.rb @@ -66,13 +66,14 @@ def convert_rabbitmq_attributes(attributes, rabbitmq_data) add_attribute(attributes, OpenTelemetry::SemConv::Incubating::MESSAGING::MESSAGING_SYSTEM, 'rabbitmq') add_attribute(attributes, OpenTelemetry::SemConv::Incubating::MESSAGING::MESSAGING_DESTINATION_NAME, rabbitmq_destination_name(rabbitmq_data)) + add_attribute(attributes, OpenTelemetry::SemConv::Incubating::MESSAGING::MESSAGING_DESTINATION_TEMPLATE, + rabbitmq_data[:template] || rabbitmq_data[:destination_template]) add_attribute(attributes, OpenTelemetry::SemConv::Incubating::MESSAGING::MESSAGING_RABBITMQ_DESTINATION_ROUTING_KEY, rabbitmq_data[:key]) - add_attribute(attributes, 'messaging.rabbitmq.queue', rabbitmq_data[:queue]) add_attribute(attributes, OpenTelemetry::SemConv::SERVER::SERVER_ADDRESS, extract_host(rabbitmq_data[:address])) add_attribute(attributes, OpenTelemetry::SemConv::SERVER::SERVER_PORT, extract_port(rabbitmq_data[:address])) add_attribute(attributes, OpenTelemetry::SemConv::Incubating::MESSAGING::MESSAGING_MESSAGE_BODY_SIZE, rabbitmq_data[:size]) - operation = rabbitmq_data[:sort] == 'publish' ? 'send' : 'receive' + operation = rabbitmq_data[:sort].to_s == 'publish' ? 'send' : 'receive' add_attribute(attributes, OpenTelemetry::SemConv::Incubating::MESSAGING::MESSAGING_OPERATION_TYPE, operation) add_attribute(attributes, OpenTelemetry::SemConv::Incubating::MESSAGING::MESSAGING_OPERATION_NAME, rabbitmq_data[:sort]) end @@ -80,10 +81,17 @@ def convert_rabbitmq_attributes(attributes, rabbitmq_data) def convert_kafka_attributes(attributes, kafka_data) return unless kafka_data + addr = kafka_data[:address] || kafka_data[:server] || kafka_data[:connection] + add_attribute(attributes, OpenTelemetry::SemConv::Incubating::MESSAGING::MESSAGING_SYSTEM, 'kafka') add_attribute(attributes, OpenTelemetry::SemConv::Incubating::MESSAGING::MESSAGING_DESTINATION_NAME, kafka_data[:service]) + add_attribute(attributes, OpenTelemetry::SemConv::Incubating::MESSAGING::MESSAGING_DESTINATION_TEMPLATE, + kafka_data[:template] || kafka_data[:destination_template]) add_attribute(attributes, OpenTelemetry::SemConv::Incubating::MESSAGING::MESSAGING_OPERATION_NAME, kafka_data[:access]) - add_attribute(attributes, OpenTelemetry::SemConv::Incubating::MESSAGING::MESSAGING_OPERATION_TYPE, kafka_data[:access]) + operation = kafka_data[:access].to_s == 'consume' ? 'receive' : 'send' + add_attribute(attributes, OpenTelemetry::SemConv::Incubating::MESSAGING::MESSAGING_OPERATION_TYPE, operation) + add_attribute(attributes, OpenTelemetry::SemConv::SERVER::SERVER_ADDRESS, extract_host(addr)) + add_attribute(attributes, OpenTelemetry::SemConv::SERVER::SERVER_PORT, extract_port(addr)) end def extract_host(address) diff --git a/lib/instana/exporter/otlp/rpc_converter.rb b/lib/instana/exporter/otlp/rpc_converter.rb index 57dc5548..8e186629 100644 --- a/lib/instana/exporter/otlp/rpc_converter.rb +++ b/lib/instana/exporter/otlp/rpc_converter.rb @@ -21,14 +21,21 @@ class RpcConverter < BaseConverter # # @return [String] The span name def span_name - rpc_data = span[:data]&.[](:rpc) || {} + rpc_data = span[:data]&.[](:rpc) + return super unless rpc_data - if rpc_data[:flavor] == :actioncable - rpc_data[:call].to_s + system_name = rpc_data[:flavor] == :actioncable ? 'actioncable' : 'grpc' + method_name = if rpc_data[:flavor] == :actioncable + rpc_data[:call].to_s.strip + else + rpc_data[:call].to_s.delete_prefix('/').strip + end + + if method_name.empty? || method_name == '_OTHER' + system_name else - # Strip the mandatory leading slash per gRPC/OTel spec - rpc_data[:call].to_s.delete_prefix('/') - end.then { |n| n.empty? ? super : n } + method_name + end end def convert_attributes @@ -49,20 +56,13 @@ def convert_attributes private - # gRPC status code mapping: 0 = OK for success, otherwise derive from error - GRPC_STATUS_CODE_OK = 0 - # Convert gRPC span attributes def convert_grpc_attributes(attributes, rpc_data) # RPC system add_attribute(attributes, Instana::Exporter::Otlp::RPC_SYSTEM_ATTR, 'grpc') - # RPC service and method - if rpc_data[:call] - service, method = parse_grpc_call(rpc_data[:call]) - add_attribute(attributes, OpenTelemetry::SemConv::Incubating::RPC::RPC_SERVICE, service) - add_attribute(attributes, OpenTelemetry::SemConv::Incubating::RPC::RPC_METHOD, method) - end + # RPC method and method_original + convert_grpc_method(attributes, rpc_data) # Server host and port add_attribute(attributes, OpenTelemetry::SemConv::SERVER::SERVER_ADDRESS, rpc_data[:host]) @@ -75,14 +75,28 @@ def convert_grpc_attributes(attributes, rpc_data) add_attribute(attributes, OpenTelemetry::SemConv::NETWORK::NETWORK_PEER_PORT, parse_port(peer[:port])) end - # gRPC status code (integer): 0 = OK for success spans - grpc_status = grpc_status_code(rpc_data) - add_attribute(attributes, OpenTelemetry::SemConv::Incubating::RPC::RPC_GRPC_STATUS_CODE, grpc_status) + # gRPC response status code (string, e.g. "OK", "DEADLINE_EXCEEDED") + status_code = grpc_response_status_code(rpc_data) + add_attribute(attributes, 'rpc.response.status_code', status_code) # gRPC-specific attributes add_attribute(attributes, 'rpc.grpc.call_type', rpc_data[:call_type]) end + def convert_grpc_method(attributes, rpc_data) + return unless rpc_data[:call] + + raw_call = rpc_data[:call].to_s + cleaned = raw_call.delete_prefix('/') + + if cleaned == '_OTHER' || raw_call == '_OTHER' + add_attribute(attributes, OpenTelemetry::SemConv::Incubating::RPC::RPC_METHOD, '_OTHER') + add_attribute(attributes, 'rpc.method_original', rpc_data[:method_original] || raw_call) + else + add_attribute(attributes, OpenTelemetry::SemConv::Incubating::RPC::RPC_METHOD, cleaned) + end + end + # Convert ActionCable span attributes def convert_action_cable_attributes(attributes, rpc_data) # RPC system @@ -91,7 +105,6 @@ def convert_action_cable_attributes(attributes, rpc_data) # ActionCable-specific attributes add_attribute(attributes, 'rails.actioncable.channel', rpc_data[:call]) add_attribute(attributes, 'rails.actioncable.call_type', rpc_data[:call_type]) - add_attribute(attributes, OpenTelemetry::SemConv::Incubating::RPC::RPC_SERVICE, span[:data]&.[](:service) || span[:service]) # Extract channel class and action from the call attribute # Format can be either "ChannelClass" (for transmit) or "ChannelClass#action" (for action dispatch) @@ -105,13 +118,6 @@ def convert_action_cable_attributes(attributes, rpc_data) add_attribute(attributes, OpenTelemetry::SemConv::SERVER::SERVER_ADDRESS, rpc_data[:host]) end - def parse_grpc_call(call) - parts = call.to_s.split('/') - return [nil, nil] if parts.size < 3 - - [parts[1], parts[2]] - end - # Parse port value to integer, returns nil if invalid or out of range def parse_port(port) return nil unless port @@ -120,41 +126,38 @@ def parse_port(port) int_port if int_port >= 0 && int_port <= 65_535 end - # Map to gRPC status code integer. - # For successful spans (ec = 0) → 0 (OK). - # For error spans with a recognized gRPC status string in the error → map it. - # Otherwise → nil (omit the attribute). - GRPC_STATUS_CODES = { - 'OK' => 0, - 'CANCELLED' => 1, - 'UNKNOWN' => 2, - 'INVALID_ARGUMENT' => 3, - 'DEADLINE_EXCEEDED' => 4, - 'NOT_FOUND' => 5, - 'ALREADY_EXISTS' => 6, - 'PERMISSION_DENIED' => 7, - 'RESOURCE_EXHAUSTED' => 8, - 'FAILED_PRECONDITION' => 9, - 'ABORTED' => 10, - 'OUT_OF_RANGE' => 11, - 'UNIMPLEMENTED' => 12, - 'INTERNAL' => 13, - 'UNAVAILABLE' => 14, - 'DATA_LOSS' => 15, - 'UNAUTHENTICATED' => 16 - }.freeze - - def grpc_status_code(rpc_data) + # Known gRPC status code names + GRPC_STATUS_CODE_NAMES = %w[ + OK + CANCELLED + UNKNOWN + INVALID_ARGUMENT + DEADLINE_EXCEEDED + NOT_FOUND + ALREADY_EXISTS + PERMISSION_DENIED + RESOURCE_EXHAUSTED + FAILED_PRECONDITION + ABORTED + OUT_OF_RANGE + UNIMPLEMENTED + INTERNAL + UNAVAILABLE + DATA_LOSS + UNAUTHENTICATED + ].freeze + + def grpc_response_status_code(rpc_data) error_count = span[:ec].to_i - return GRPC_STATUS_CODE_OK if error_count.zero? + return 'OK' if error_count.zero? # Attempt to extract gRPC status string from error message error_msg = rpc_data[:error].to_s - GRPC_STATUS_CODES.each do |name, code| - return code if error_msg.include?(name) + GRPC_STATUS_CODE_NAMES.each do |name| + return name if error_msg.include?(name) end - GRPC_STATUS_CODES['UNKNOWN'] + 'UNKNOWN' end end end diff --git a/lib/instana/instrumentation/resque.rb b/lib/instana/instrumentation/resque.rb index 841bac54..2177cf0c 100644 --- a/lib/instana/instrumentation/resque.rb +++ b/lib/instana/instrumentation/resque.rb @@ -16,6 +16,21 @@ def collect_kvs(op, klass, args) begin kvs[:job] = klass.to_s kvs[:queue] = klass.instance_variable_get(:@queue) + + if defined?(::Resque) && ::Resque.respond_to?(:redis) && ::Resque.redis + client = ::Resque.redis + client = client.redis if client.respond_to?(:redis) + + host, port = if client.respond_to?(:config) && client.config.respond_to?(:host) && client.config.respond_to?(:port) + [client.config.host, client.config.port] + elsif client.respond_to?(:connection) + [client.connection[:host], client.connection[:port]] + elsif client.respond_to?(:client) && client.client.respond_to?(:options) + [client.client.options[:host], client.client.options[:port]] + end + + kvs[:'redis-url'] = "#{host}:#{port}" if host && port + end rescue => e Instana.logger.debug { "#{__method__}:#{File.basename(__FILE__)}:#{__LINE__}: #{e.message}" } end @@ -64,13 +79,28 @@ def dequeue(klass, *args) end module ResqueWorker - def perform(job) + def perform(job) # rubocop:disable Metrics/CyclomaticComplexity, Metrics/PerceivedComplexity kvs = {} kvs[:'resque-worker'] = {} begin kvs[:'resque-worker'][:job] = job.payload['class'].to_s kvs[:'resque-worker'][:queue] = job.queue + + if defined?(::Resque) && ::Resque.respond_to?(:redis) && ::Resque.redis + client = ::Resque.redis + client = client.redis if client.respond_to?(:redis) + + host, port = if client.respond_to?(:config) && client.config.respond_to?(:host) && client.config.respond_to?(:port) + [client.config.host, client.config.port] + elsif client.respond_to?(:connection) + [client.connection[:host], client.connection[:port]] + elsif client.respond_to?(:client) && client.client.respond_to?(:options) + [client.client.options[:host], client.client.options[:port]] + end + + kvs[:'resque-worker'][:'redis-url'] = "#{host}:#{port}" if host && port + end rescue => e ::Instana.logger.debug { "#{__method__}:#{File.basename(__FILE__)}:#{__LINE__}: #{e.message}" } if Instana::Config[:verbose] end diff --git a/lib/instana/serverless.rb b/lib/instana/serverless.rb index db26b31c..0d8a21f8 100644 --- a/lib/instana/serverless.rb +++ b/lib/instana/serverless.rb @@ -21,6 +21,7 @@ def initialize(agent: ::Instana.agent, tracer: ::Instana.tracer, logger: ::Insta @agent = agent @tracer = tracer @logger = logger + @cold_start = true end def wrap_aws(event, context, &block) @@ -33,9 +34,12 @@ def wrap_aws(event, context, &block) functionName: context.function_name, functionVersion: context.function_version, runtime: 'ruby', - trigger: trigger - } + trigger: trigger, + reqId: context.aws_request_id, + coldStart: @cold_start + }.compact } + @cold_start = false if event_tags.key?(:http) tags = tags.merge(event_tags) @@ -44,7 +48,12 @@ def wrap_aws(event, context, &block) end Trace.with_span(OpenTelemetry::Trace.non_recording_span(::Instana::SpanContext.new(trace_id: span_context[:trace_id], span_id: span_context[:span_id], level: span_context[:level]))) do - @tracer.in_span(:'aws.lambda.entry', attributes: tags, &block) + @tracer.in_span(:'aws.lambda.entry', attributes: tags) do |span, ctx| + block.call(span, ctx) + rescue StandardError => e + span.add_attributes(lambda: { error: e.message }) + raise + end end ensure begin diff --git a/test/exporter/otlp/aws_converter_test.rb b/test/exporter/otlp/aws_converter_test.rb index b0dde1a6..6004c58a 100644 --- a/test/exporter/otlp/aws_converter_test.rb +++ b/test/exporter/otlp/aws_converter_test.rb @@ -16,6 +16,7 @@ def test_sqs_send_operation_conversion assert_equal 'aws_sqs', attrs['messaging.system'] assert_equal 'my-queue', attrs['messaging.destination.name'] + assert_equal 'send', attrs['messaging.operation.name'] assert_equal 'group-1', attrs['messaging.aws.sqs.message_group_id'] assert_equal 5, attrs['messaging.batch.message_count'] assert_equal 'send', attrs['messaging.operation.type'] @@ -100,8 +101,9 @@ def test_sns_basic_conversion converter = Instana::Exporter::Otlp::AwsConverter.new(span) attrs = converter.send(:convert_attributes) - assert_equal 'aws_sns', attrs['messaging.system'] + assert_equal 'aws.sns', attrs['messaging.system'] assert_equal 'my-topic', attrs['messaging.destination.name'] + assert_equal 'send', attrs['messaging.operation.name'] assert_equal 'arn:aws:sns:us-east-1:123456789:my-topic', attrs['messaging.aws.sns.target_arn'] assert_equal 'send', attrs['messaging.operation.type'] end @@ -113,7 +115,7 @@ def test_sns_with_phone_number converter = Instana::Exporter::Otlp::AwsConverter.new(span) attrs = converter.send(:convert_attributes) - assert_equal 'aws_sns', attrs['messaging.system'] + assert_equal 'aws.sns', attrs['messaging.system'] assert_equal '+1234567890', attrs['messaging.aws.sns.phone_number'] end @@ -124,7 +126,7 @@ def test_sns_with_subject converter = Instana::Exporter::Otlp::AwsConverter.new(span) attrs = converter.send(:convert_attributes) - assert_equal 'aws_sns', attrs['messaging.system'] + assert_equal 'aws.sns', attrs['messaging.system'] assert_equal 'Important Alert', attrs['messaging.aws.sns.subject'] end @@ -140,8 +142,9 @@ def test_sns_with_all_attributes converter = Instana::Exporter::Otlp::AwsConverter.new(span) attrs = converter.send(:convert_attributes) - assert_equal 'aws_sns', attrs['messaging.system'] + assert_equal 'aws.sns', attrs['messaging.system'] assert_equal 'full-topic', attrs['messaging.destination.name'] + assert_equal 'send', attrs['messaging.operation.name'] assert_equal 'arn:aws:sns:us-west-2:987654321:full-topic', attrs['messaging.aws.sns.target_arn'] assert_equal '+9876543210', attrs['messaging.aws.sns.phone_number'] assert_equal 'Test Subject', attrs['messaging.aws.sns.subject'] @@ -156,10 +159,10 @@ def test_dynamodb_basic_conversion converter = Instana::Exporter::Otlp::AwsConverter.new(span) attrs = converter.send(:convert_attributes) - assert_equal 'dynamodb', attrs['db.system.name'] + assert_equal 'aws.dynamodb', attrs['db.system.name'] assert_equal 'GetItem', attrs['db.operation.name'] - assert_equal 'users-table', attrs['db.namespace'] - assert_equal 'users-table', attrs['aws.dynamodb.table_name'] + assert_equal ['users-table'], attrs['aws.dynamodb.table_names'] + assert_nil attrs['db.namespace'] end def test_dynamodb_put_item_operation @@ -169,10 +172,10 @@ def test_dynamodb_put_item_operation converter = Instana::Exporter::Otlp::AwsConverter.new(span) attrs = converter.send(:convert_attributes) - assert_equal 'dynamodb', attrs['db.system.name'] + assert_equal 'aws.dynamodb', attrs['db.system.name'] assert_equal 'PutItem', attrs['db.operation.name'] - assert_equal 'orders-table', attrs['db.namespace'] - assert_equal 'orders-table', attrs['aws.dynamodb.table_name'] + assert_equal ['orders-table'], attrs['aws.dynamodb.table_names'] + assert_nil attrs['db.namespace'] end def test_dynamodb_query_operation @@ -182,9 +185,9 @@ def test_dynamodb_query_operation converter = Instana::Exporter::Otlp::AwsConverter.new(span) attrs = converter.send(:convert_attributes) - assert_equal 'dynamodb', attrs['db.system.name'] + assert_equal 'aws.dynamodb', attrs['db.system.name'] assert_equal 'Query', attrs['db.operation.name'] - assert_equal 'products-table', attrs['db.namespace'] + assert_equal ['products-table'], attrs['aws.dynamodb.table_names'] end def test_dynamodb_scan_operation @@ -194,9 +197,9 @@ def test_dynamodb_scan_operation converter = Instana::Exporter::Otlp::AwsConverter.new(span) attrs = converter.send(:convert_attributes) - assert_equal 'dynamodb', attrs['db.system.name'] + assert_equal 'aws.dynamodb', attrs['db.system.name'] assert_equal 'Scan', attrs['db.operation.name'] - assert_equal 'analytics-table', attrs['db.namespace'] + assert_equal ['analytics-table'], attrs['aws.dynamodb.table_names'] end # AWS S3 Tests @@ -293,7 +296,7 @@ def test_s3_without_op_omits_rpc_method end # AWS Lambda Tests - def test_lambda_basic_conversion + def test_lambda_invoke_basic_conversion span = create_span('aws.lambda', { aws: { lambda: { @@ -304,13 +307,14 @@ def test_lambda_basic_conversion converter = Instana::Exporter::Otlp::AwsConverter.new(span) attrs = converter.send(:convert_attributes) - assert_equal 'lambda', attrs['aws.service'] - assert_equal 'my-function', attrs['aws.lambda.function_name'] - assert_equal 'RequestResponse', attrs['aws.lambda.invocation_type'] + assert_equal 'aws-api', attrs[Instana::Exporter::Otlp::RPC_SYSTEM_ATTR] + assert_equal 'Lambda.Invoke', attrs['rpc.method'] + assert_equal 'aws', attrs['cloud.provider'] assert_equal 'my-function', attrs['faas.invoked_name'] + assert_equal 'aws', attrs['faas.invoked_provider'] end - def test_lambda_event_invocation + def test_lambda_invoke_event_invocation span = create_span('aws.lambda', { aws: { lambda: { @@ -321,26 +325,76 @@ def test_lambda_event_invocation converter = Instana::Exporter::Otlp::AwsConverter.new(span) attrs = converter.send(:convert_attributes) - assert_equal 'lambda', attrs['aws.service'] - assert_equal 'async-function', attrs['aws.lambda.function_name'] - assert_equal 'Event', attrs['aws.lambda.invocation_type'] + assert_equal 'aws-api', attrs[Instana::Exporter::Otlp::RPC_SYSTEM_ATTR] + assert_equal 'Lambda.Invoke', attrs['rpc.method'] assert_equal 'async-function', attrs['faas.invoked_name'] end - def test_lambda_dry_run_invocation - span = create_span('aws.lambda', { - aws: { - lambda: { - invoke: { function: 'test-function', type: 'DryRun' } - } + def test_lambda_entry_conversion + span = create_span('aws.lambda.entry', { + lambda: { + arn: 'arn:aws:lambda:us-east-1:123456789012:function:my-function:1', + functionName: 'my-function', + functionVersion: '1', + trigger: 'aws:api.gateway', + reqId: 'req-12345', + coldStart: 'true' } }) converter = Instana::Exporter::Otlp::AwsConverter.new(span) attrs = converter.send(:convert_attributes) - assert_equal 'lambda', attrs['aws.service'] - assert_equal 'test-function', attrs['aws.lambda.function_name'] - assert_equal 'DryRun', attrs['aws.lambda.invocation_type'] + assert_equal 'http', attrs['faas.trigger'] + assert_equal 'my-function', attrs['faas.name'] + assert_equal '1', attrs['faas.version'] + assert_equal 'req-12345', attrs['faas.invocation_id'] + assert_equal true, attrs['faas.coldstart'] + assert_equal 'aws', attrs['cloud.provider'] + assert_equal 'aws_lambda', attrs['cloud.platform'] + assert_equal 'us-east-1', attrs['cloud.region'] + assert_equal '123456789012', attrs['cloud.account.id'] + assert_equal 'arn:aws:lambda:us-east-1:123456789012:function:my-function:1', attrs['cloud.resource_id'] + assert_equal 'my-function', converter.span_name + end + + def test_lambda_entry_triggers + triggers = { + 'aws:api.gateway' => 'http', + 'aws:api.gateway.noproxy' => 'http', + 'aws:application.load.balancer' => 'http', + 'aws:s3' => 'datasource', + 'aws:dynamodb' => 'datasource', + 'aws:sqs' => 'pubsub', + 'aws:sns' => 'pubsub', + 'aws:kinesis' => 'datasource', + 'aws:kinesis.firehouse' => 'datasource', + 'aws:cloudwatch.events' => 'timer', + 'aws:cloudwatch.logs' => 'datasource', + 'aws:lambda.invoke' => 'other', + 'unknown.trigger' => 'other' + } + + triggers.each do |raw, expected| + span = create_span('aws.lambda.entry', { + lambda: { + functionName: 'fn', + trigger: raw + } + }) + attrs = Instana::Exporter::Otlp::AwsConverter.new(span).send(:convert_attributes) + assert_equal expected, attrs['faas.trigger'], "Failed for trigger #{raw}" + end + end + + def test_lambda_entry_error + span = create_span('aws.lambda.entry', { + lambda: { + functionName: 'fn', + error: 'Unhandled runtime error' + } + }) + attrs = Instana::Exporter::Otlp::AwsConverter.new(span).send(:convert_attributes) + assert_equal 'Unhandled runtime error', attrs['exception.message'] end # Edge Cases and Mixed Tests @@ -373,7 +427,7 @@ def test_multiple_aws_services_in_same_span # SNS overwrites SQS since both set messaging.system and messaging.destination.name # In practice, a span should only have one AWS service type - assert_equal 'aws_sns', attrs['messaging.system'] + assert_equal 'aws.sns', attrs['messaging.system'] assert_equal 'test-topic', attrs['messaging.destination.name'] assert_equal 'send', attrs['messaging.operation.type'] end @@ -471,9 +525,9 @@ def test_full_span_conversion_with_dynamodb # Verify DynamoDB-specific attributes attrs = result[:attributes] - assert_equal 'dynamodb', attrs['db.system.name'] + assert_equal 'aws.dynamodb', attrs['db.system.name'] assert_equal 'BatchGetItem', attrs['db.operation.name'] - assert_equal 'batch-table', attrs['db.namespace'] + assert_equal ['batch-table'], attrs['aws.dynamodb.table_names'] end private diff --git a/test/exporter/otlp/background_job_converter_test.rb b/test/exporter/otlp/background_job_converter_test.rb index 32f43931..f2bf7a2e 100644 --- a/test/exporter/otlp/background_job_converter_test.rb +++ b/test/exporter/otlp/background_job_converter_test.rb @@ -14,8 +14,8 @@ def test_sidekiq_client_conversion assert_equal 'sidekiq', attrs['messaging.system'] assert_equal 'default', attrs['messaging.destination.name'] assert_equal 'publish', attrs['messaging.operation.name'] + assert_equal 'send', attrs['messaging.operation.type'] assert_equal '123', attrs['messaging.message.id'] - assert_equal 'TestWorker', attrs['messaging.consumer.group.name'] assert_equal 'localhost', attrs['server.address'] assert_equal 6379, attrs['server.port'] end @@ -29,9 +29,9 @@ def test_sidekiq_worker_conversion assert_equal 'sidekiq', attrs['messaging.system'] assert_equal 'critical', attrs['messaging.destination.name'] - assert_equal 'process', attrs['messaging.operation.name'] + assert_equal 'consume', attrs['messaging.operation.name'] + assert_equal 'process', attrs['messaging.operation.type'] assert_equal '456', attrs['messaging.message.id'] - assert_equal 'EmailWorker', attrs['messaging.consumer.group.name'] end def test_resque_client_conversion @@ -44,17 +44,25 @@ def test_resque_client_conversion assert_equal 'resque', attrs['messaging.system'] assert_equal 'low', attrs['messaging.destination.name'] assert_equal 'publish', attrs['messaging.operation.name'] + assert_equal 'send', attrs['messaging.operation.type'] + assert_nil attrs['messaging.message.id'] + assert_equal '127.0.0.1', attrs['server.address'] + assert_equal 6379, attrs['server.port'] end def test_resque_worker_conversion span = create_span('resque-worker', { - 'resque-worker': { queue: 'high', job_id: '101', job: 'DataWorker' } + 'resque-worker': { queue: 'high', job_id: '101', job: 'DataWorker', 'redis-url': 'redis.internal:6381' } }) converter = Instana::Exporter::Otlp::BackgroundJobConverter.new(span) attrs = converter.send(:convert_attributes) assert_equal 'resque', attrs['messaging.system'] assert_equal 'process', attrs['messaging.operation.name'] + assert_equal 'process', attrs['messaging.operation.type'] + assert_nil attrs['messaging.message.id'] + assert_equal 'redis.internal', attrs['server.address'] + assert_equal 6381, attrs['server.port'] end def test_activejob_conversion @@ -67,6 +75,7 @@ def test_activejob_conversion assert_equal 'activejob', attrs['messaging.system'] assert_equal 'mailers', attrs['messaging.destination.name'] assert_equal 'perform', attrs['messaging.operation.name'] + assert_equal 'process', attrs['messaging.operation.type'] assert_equal 'abc-123', attrs['messaging.message.id'] end @@ -116,10 +125,10 @@ def test_span_name_sidekiq_client_publish assert_equal 'default publish', result[:name] end - def test_span_name_sidekiq_worker_process + def test_span_name_sidekiq_worker_consume span = create_span('sidekiq-worker', { 'sidekiq-worker': { queue: 'critical', job: 'MyWorker' } }) result = Instana::Exporter::Otlp::BackgroundJobConverter.new(span).convert - assert_equal 'critical process', result[:name] + assert_equal 'critical consume', result[:name] end def test_span_name_resque_client_publish @@ -137,7 +146,7 @@ def test_span_name_resque_worker_process def test_span_name_falls_back_to_operation_when_no_queue span = create_span('sidekiq-worker', { 'sidekiq-worker': {} }) result = Instana::Exporter::Otlp::BackgroundJobConverter.new(span).convert - assert_equal 'process', result[:name] + assert_equal 'consume', result[:name] end private diff --git a/test/exporter/otlp/converter_factory_test.rb b/test/exporter/otlp/converter_factory_test.rb index dc9db50f..9ed72e10 100644 --- a/test/exporter/otlp/converter_factory_test.rb +++ b/test/exporter/otlp/converter_factory_test.rb @@ -226,6 +226,7 @@ def test_background_job_span_detection assert @factory.send(:background_job_span?, create_test_span(name: 'sidekiq-worker')) assert @factory.send(:background_job_span?, create_test_span(name: 'resque-client')) assert @factory.send(:background_job_span?, create_test_span(name: 'resque-worker')) + assert @factory.send(:background_job_span?, create_test_span(name: 'activejob')) refute @factory.send(:background_job_span?, create_test_span(name: :http)) end @@ -258,7 +259,7 @@ def test_handles_empty_span_name end def test_returns_background_job_converter_for_background_job_spans - %w[sidekiq-client sidekiq-worker resque-client resque-worker].each do |name| + %w[sidekiq-client sidekiq-worker resque-client resque-worker activejob].each do |name| span = create_test_span(name: name) converter = @factory.create(span) diff --git a/test/exporter/otlp/database_converter_test.rb b/test/exporter/otlp/database_converter_test.rb index ad3fc584..aa02956b 100644 --- a/test/exporter/otlp/database_converter_test.rb +++ b/test/exporter/otlp/database_converter_test.rb @@ -6,7 +6,7 @@ class DatabaseConverterTest < Minitest::Test def test_activerecord_conversion span = create_span(:activerecord, { - activerecord: { adapter: 'postgresql', db: 'mydb', sql: 'SELECT * FROM users', username: 'admin', host: 'db.example.com' } + activerecord: { adapter: 'postgresql', db: 'mydb', sql: 'SELECT * FROM users', username: 'admin', host: 'db.example.com:5432' } }) converter = Instana::Exporter::Otlp::DatabaseConverter.new(span) attrs = converter.send(:convert_attributes) @@ -14,8 +14,18 @@ def test_activerecord_conversion assert_equal 'postgresql', attrs['db.system.name'] assert_equal 'mydb', attrs['db.namespace'] assert_equal 'SELECT * FROM users', attrs['db.query.text'] + assert_equal 'SELECT', attrs['db.operation.name'] assert_equal 'admin', attrs['db.user'] assert_equal 'db.example.com', attrs['server.address'] + assert_equal 5432, attrs['server.port'] + end + + def test_activerecord_unknown_adapter_falls_back_to_other_sql + span = create_span(:activerecord, { + activerecord: { adapter: 'custom_sql_driver', db: 'mydb', sql: 'SELECT 1' } + }) + attrs = Instana::Exporter::Otlp::DatabaseConverter.new(span).send(:convert_attributes) + assert_equal 'other_sql', attrs['db.system.name'] end def test_activerecord_adapter_normalization_mysql2 @@ -52,7 +62,7 @@ def test_activerecord_without_table_omits_collection_name def test_sequel_conversion span = create_span(:sequel, { - sequel: { adapter: 'mysql2', db: 'testdb', sql: 'INSERT INTO logs', username: 'root', host: 'localhost' } + sequel: { adapter: 'mysql2', db: 'testdb', sql: 'INSERT INTO logs', username: 'root', host: 'localhost', port: 3306 } }) converter = Instana::Exporter::Otlp::DatabaseConverter.new(span) attrs = converter.send(:convert_attributes) @@ -60,8 +70,10 @@ def test_sequel_conversion assert_equal 'mysql', attrs['db.system.name'] assert_equal 'testdb', attrs['db.namespace'] assert_equal 'INSERT INTO logs', attrs['db.query.text'] + assert_equal 'INSERT', attrs['db.operation.name'] assert_equal 'root', attrs['db.user'] assert_equal 'localhost', attrs['server.address'] + assert_equal 3306, attrs['server.port'] end def test_sequel_with_table_sets_collection_name @@ -81,39 +93,25 @@ def test_redis_conversion assert_equal 'redis', attrs['db.system.name'] assert_equal 'GET key', attrs['db.operation.name'] - assert_equal 'GET key', attrs['db.query.text'] assert_equal '2', attrs['db.namespace'] - assert_nil attrs['db.redis.database_index'] assert_equal 'redis.local', attrs['server.address'] assert_equal 6379, attrs['server.port'] end def test_memcache_conversion span = create_span(:memcache, { - memcache: { command: 'get', key: 'user:123', namespace: 'app', server: '127.0.0.1:11211' } + memcache: { command: 'get', namespace: 'app', server: '127.0.0.1:11211' } }) converter = Instana::Exporter::Otlp::DatabaseConverter.new(span) attrs = converter.send(:convert_attributes) assert_equal 'memcached', attrs['db.system.name'] assert_equal 'get', attrs['db.operation.name'] - assert_equal 'user:123', attrs['db.memcached.key'] assert_equal 'app', attrs['db.namespace'] - assert_nil attrs['db.memcached.namespace'] assert_equal '127.0.0.1', attrs['server.address'] assert_equal 11211, attrs['server.port'] end - def test_memcache_with_keys - span = create_span(:memcache, { - memcache: { command: 'get_multi', keys: ['key1', 'key2'], server: 'localhost:11211' } - }) - converter = Instana::Exporter::Otlp::DatabaseConverter.new(span) - attrs = converter.send(:convert_attributes) - - assert_equal ['key1', 'key2'], attrs['db.memcached.keys'] - end - def test_mongodb_conversion span = create_span(:mongo, { mongo: { namespace: 'mydb.users', command: 'find', json: '{"name":"John"}', peer: { hostname: 'mongo.local', port: 27017 } } @@ -175,27 +173,33 @@ def test_missing_data # --- span_name tests --- def test_span_name_activerecord + span = create_span(:activerecord, { activerecord: { adapter: 'postgresql', db: 'mydb', sql: 'SELECT * FROM users', table: 'users' } }) + result = Instana::Exporter::Otlp::DatabaseConverter.new(span).convert + assert_equal 'SELECT mydb.users', result[:name] + end + + def test_span_name_activerecord_without_sql_falls_back_to_db span = create_span(:activerecord, { activerecord: { adapter: 'postgresql', db: 'mydb' } }) result = Instana::Exporter::Otlp::DatabaseConverter.new(span).convert - assert_equal 'postgresql mydb', result[:name] + assert_equal 'mydb', result[:name] end def test_span_name_sequel - span = create_span(:sequel, { sequel: { adapter: 'mysql2', db: 'testdb' } }) + span = create_span(:sequel, { sequel: { adapter: 'mysql2', db: 'testdb', sql: 'INSERT INTO logs' } }) result = Instana::Exporter::Otlp::DatabaseConverter.new(span).convert - assert_equal 'mysql2 testdb', result[:name] + assert_equal 'INSERT testdb', result[:name] end def test_span_name_redis span = create_span(:redis, { redis: { command: 'GET key', db: 0, connection: 'redis.local:6379' } }) result = Instana::Exporter::Otlp::DatabaseConverter.new(span).convert - assert_equal 'redis GET key', result[:name] + assert_equal 'GET key', result[:name] end def test_span_name_memcache - span = create_span(:memcache, { memcache: { command: 'get', key: 'u:1', server: '127.0.0.1:11211' } }) + span = create_span(:memcache, { memcache: { command: 'get', server: '127.0.0.1:11211' } }) result = Instana::Exporter::Otlp::DatabaseConverter.new(span).convert - assert_equal 'memcached get', result[:name] + assert_equal 'get', result[:name] end def test_span_name_mongo diff --git a/test/exporter/otlp/graphql_converter_test.rb b/test/exporter/otlp/graphql_converter_test.rb new file mode 100644 index 00000000..614cb120 --- /dev/null +++ b/test/exporter/otlp/graphql_converter_test.rb @@ -0,0 +1,80 @@ +# (c) Copyright IBM Corp. 2026 + +require 'test_helper' +require 'instana/exporter/otlp/graphql_converter' + +class GraphqlConverterTest < Minitest::Test + def test_graphql_conversion + span = create_span(:'graphql.server', { + graphql: { + operationName: 'GetUser', + operationType: 'query', + document: 'query GetUser { user { id name } }' + } + }) + converter = Instana::Exporter::Otlp::GraphqlConverter.new(span) + attrs = converter.send(:convert_attributes) + + assert_equal 'GetUser', attrs['graphql.operation.name'] + assert_equal 'query', attrs['graphql.operation.type'] + assert_equal 'query GetUser { user { id name } }', attrs['graphql.document'] + assert_nil attrs['graphql.arguments'] + assert_nil attrs['error.type'] + assert_equal 'query GetUser', converter.span_name + end + + def test_graphql_fallback_to_fields_when_no_document + span = create_span(:'graphql.server', { + graphql: { + operationName: 'ListTasks', + operationType: 'query', + fields: { 'tasks' => ['nodes'], 'nodes' => ['action'] } + } + }) + converter = Instana::Exporter::Otlp::GraphqlConverter.new(span) + attrs = converter.send(:convert_attributes) + + assert_equal 'tasks { nodes }, nodes { action }', attrs['graphql.document'] + end + + def test_graphql_with_errors + span = create_span(:'graphql.server', { + graphql: { + operationName: 'CreatePost', + operationType: 'mutation', + errors: ['Unauthorized', 'Validation failed'] + } + }) + converter = Instana::Exporter::Otlp::GraphqlConverter.new(span) + attrs = converter.send(:convert_attributes) + + assert_equal 'Unauthorized, Validation failed', attrs['error.type'] + end + + def test_graphql_span_names + # Operation type and name + span = create_span(:'graphql.server', { graphql: { operationType: 'mutation', operationName: 'UpdateUser' } }) + assert_equal 'mutation UpdateUser', Instana::Exporter::Otlp::GraphqlConverter.new(span).span_name + + # Operation type only + span = create_span(:'graphql.server', { graphql: { operationType: 'subscription' } }) + assert_equal 'subscription', Instana::Exporter::Otlp::GraphqlConverter.new(span).span_name + + # Neither operation type nor name + span = create_span(:'graphql.server', { graphql: {} }) + assert_equal 'graphql', Instana::Exporter::Otlp::GraphqlConverter.new(span).span_name + + # No data + span = create_span(:'graphql.server', {}) + assert_equal 'graphql', Instana::Exporter::Otlp::GraphqlConverter.new(span).span_name + end + + private + + def create_span(name, data) + span = Instana::Span.new(name) + span[:data] = data + span.close + span + end +end diff --git a/test/exporter/otlp/http_converter_test.rb b/test/exporter/otlp/http_converter_test.rb index abc38a6c..a743461f 100644 --- a/test/exporter/otlp/http_converter_test.rb +++ b/test/exporter/otlp/http_converter_test.rb @@ -33,7 +33,7 @@ def test_convert_http_client_span_with_all_attributes assert_equal format_trace_id(span.trace_id), result[:trace_id] assert_equal format_span_id(span.id), result[:span_id] assert_equal format_span_id(span.parent_id), result[:parent_span_id] - assert_equal 'GET /users/123', result[:name] + assert_equal 'GET', result[:name] assert_equal :client, result[:kind] # CLIENT kind # Verify HTTP attributes are present (using new semantic conventions) @@ -186,6 +186,41 @@ def test_user_agent_from_header attributes = result[:attributes] assert_http_attribute(attributes, 'user_agent.original', 'Mozilla/5.0') + assert_http_attribute(attributes, 'http.request.header.content-type', 'application/json') + end + + def test_custom_headers_capture + span = create_http_span( + header: { + 'X-Capture-This' => 'Value1', + 'X_Custom_Header' => 'Value2' + } + ) + + converter = Instana::Exporter::Otlp::HttpConverter.new(span) + result = converter.convert + + attributes = result[:attributes] + assert_http_attribute(attributes, 'http.request.header.x-capture-this', 'Value1') + assert_http_attribute(attributes, 'http.request.header.x-custom-header', 'Value2') + end + + def test_url_template_from_path_tpl_and_route_id + span = create_http_span(method: 'GET', path_tpl: '/users/{id}') + result = Instana::Exporter::Otlp::HttpConverter.new(span).convert + assert_http_attribute(result.attributes, 'url.template', '/users/{id}') + assert_equal 'GET /users/{id}', result[:name] + + span2 = create_http_span(method: 'POST', route_id: '/orders/{order_id}') + result2 = Instana::Exporter::Otlp::HttpConverter.new(span2).convert + assert_http_attribute(result2.attributes, 'url.template', '/orders/{order_id}') + assert_equal 'POST /orders/{order_id}', result2[:name] + end + + def test_context_root_fallback_for_url_path + span = create_http_span(method: 'GET', context_root: '/app/v1') + result = Instana::Exporter::Otlp::HttpConverter.new(span).convert + assert_http_attribute(result.attributes, 'url.path', '/app/v1') end def test_user_agent_not_present_when_header_missing @@ -298,12 +333,18 @@ def test_span_name_method_only assert_equal 'DELETE', result[:name] end - def test_span_name_method_and_path - span = create_http_span(method: 'GET', path: '/users/{id}') + def test_span_name_method_and_path_tpl + span = create_http_span(method: 'GET', path_tpl: '/users/{id}') result = Instana::Exporter::Otlp::HttpConverter.new(span).convert assert_equal 'GET /users/{id}', result[:name] end + def test_span_name_uses_method_only_when_no_template_even_if_path_present + span = create_http_span(method: 'GET', path: '/users/123') + result = Instana::Exporter::Otlp::HttpConverter.new(span).convert + assert_equal 'GET', result[:name] + end + def test_span_name_falls_back_to_http_when_no_method span = create_http_span(method: nil) result = Instana::Exporter::Otlp::HttpConverter.new(span).convert diff --git a/test/exporter/otlp/messaging_converter_test.rb b/test/exporter/otlp/messaging_converter_test.rb index 8db6a7a5..3f08966b 100644 --- a/test/exporter/otlp/messaging_converter_test.rb +++ b/test/exporter/otlp/messaging_converter_test.rb @@ -15,7 +15,7 @@ def test_rabbitmq_publish_conversion # Composite: exchange:key (producer side) assert_equal 'orders:order.created', attrs['messaging.destination.name'] assert_equal 'order.created', attrs['messaging.rabbitmq.destination.routing_key'] - assert_equal 'order_queue', attrs['messaging.rabbitmq.queue'] + assert_nil attrs['messaging.rabbitmq.queue'] assert_equal 'rabbitmq.local', attrs['server.address'] assert_equal 5672, attrs['server.port'] assert_equal 'send', attrs['messaging.operation.type'] @@ -89,7 +89,26 @@ def test_kafka_consume_conversion assert_equal 'kafka', attrs['messaging.system'] assert_equal 'events-topic', attrs['messaging.destination.name'] assert_equal 'consume', attrs['messaging.operation.name'] - assert_equal 'consume', attrs['messaging.operation.type'] + assert_equal 'receive', attrs['messaging.operation.type'] + end + + def test_kafka_with_server_address + span = create_span(:kafka, { + kafka: { service: 'orders-topic', access: 'send', address: 'kafka.prod:9092' } + }) + converter = Instana::Exporter::Otlp::MessagingConverter.new(span) + attrs = converter.send(:convert_attributes) + + assert_equal 'kafka.prod', attrs['server.address'] + assert_equal 9092, attrs['server.port'] + end + + def test_destination_template_mapping + span = create_span(:kafka, { + kafka: { service: 'user-orders-123', access: 'send', template: 'user-orders-{id}' } + }) + attrs = Instana::Exporter::Otlp::MessagingConverter.new(span).send(:convert_attributes) + assert_equal 'user-orders-{id}', attrs['messaging.destination.template'] end def test_span_name_kafka diff --git a/test/exporter/otlp/rpc_converter_test.rb b/test/exporter/otlp/rpc_converter_test.rb index 6edb854f..01a5b850 100644 --- a/test/exporter/otlp/rpc_converter_test.rb +++ b/test/exporter/otlp/rpc_converter_test.rb @@ -12,11 +12,11 @@ def test_grpc_conversion attrs = converter.send(:convert_attributes) assert_equal 'grpc', attrs[Instana::Exporter::Otlp::RPC_SYSTEM_ATTR] - assert_equal 'package.Service', attrs['rpc.service'] - assert_equal 'Method', attrs['rpc.method'] + assert_nil attrs['rpc.service'] + assert_equal 'package.Service/Method', attrs['rpc.method'] assert_equal 'grpc.example.com', attrs['server.address'] assert_equal 'unary', attrs['rpc.grpc.call_type'] - assert_equal 0, attrs['rpc.grpc.status_code'] + assert_equal 'OK', attrs['rpc.response.status_code'] end def test_grpc_with_port @@ -45,21 +45,28 @@ def test_grpc_with_peer_address def test_grpc_status_code_ok_on_success span = create_span(:grpc, { rpc: { call: '/svc/Method', host: 'h' } }) attrs = Instana::Exporter::Otlp::RpcConverter.new(span).send(:convert_attributes) - assert_equal 0, attrs['rpc.grpc.status_code'] + assert_equal 'OK', attrs['rpc.response.status_code'] end def test_grpc_status_code_from_error_message span = create_span(:grpc, { rpc: { call: '/svc/Method', error: 'DEADLINE_EXCEEDED: context deadline exceeded' } }) span[:ec] = 1 attrs = Instana::Exporter::Otlp::RpcConverter.new(span).send(:convert_attributes) - assert_equal 4, attrs['rpc.grpc.status_code'] + assert_equal 'DEADLINE_EXCEEDED', attrs['rpc.response.status_code'] end def test_grpc_status_code_unknown_on_unrecognized_error span = create_span(:grpc, { rpc: { call: '/svc/Method', error: 'some unknown error' } }) span[:ec] = 1 attrs = Instana::Exporter::Otlp::RpcConverter.new(span).send(:convert_attributes) - assert_equal 2, attrs['rpc.grpc.status_code'] + assert_equal 'UNKNOWN', attrs['rpc.response.status_code'] + end + + def test_grpc_other_method_mapping + span = create_span(:grpc, { rpc: { call: '_OTHER', method_original: 'com.custom.UnknownMethod' } }) + attrs = Instana::Exporter::Otlp::RpcConverter.new(span).send(:convert_attributes) + assert_equal '_OTHER', attrs['rpc.method'] + assert_equal 'com.custom.UnknownMethod', attrs['rpc.method_original'] end def test_actioncable_conversion @@ -73,7 +80,7 @@ def test_actioncable_conversion assert_equal 'actioncable', attrs[Instana::Exporter::Otlp::RPC_SYSTEM_ATTR] assert_equal 'ChatChannel#speak', attrs['rails.actioncable.channel'] assert_equal 'action', attrs['rails.actioncable.call_type'] - assert_equal 'my-app', attrs['rpc.service'] + assert_nil attrs['rpc.service'] assert_equal 'ChatChannel', attrs['code.namespace'] assert_equal 'speak', attrs['code.function'] assert_equal 'ws.example.com', attrs['server.address'] @@ -90,17 +97,10 @@ def test_actioncable_transmit assert_nil attrs['code.function'] end - def test_parse_grpc_call - span = create_span(:grpc, {}) - converter = Instana::Exporter::Otlp::RpcConverter.new(span) - - service, method = converter.send(:parse_grpc_call, '/pkg.Service/Method') - assert_equal 'pkg.Service', service - assert_equal 'Method', method - - service, method = converter.send(:parse_grpc_call, 'invalid') - assert_nil service - assert_nil method + def test_span_name_grpc_other_falls_back_to_system_name + span = create_span(:grpc, { rpc: { call: '_OTHER' } }) + result = Instana::Exporter::Otlp::RpcConverter.new(span).convert + assert_equal 'grpc', result[:name] end def test_missing_rpc_data diff --git a/test/serverless_test.rb b/test/serverless_test.rb index b014a277..e260ae67 100644 --- a/test/serverless_test.rb +++ b/test/serverless_test.rb @@ -57,6 +57,32 @@ def test_lambda_data assert_equal 'ruby', data[:runtime] end + def test_lambda_request_id_cold_start_and_error + clear_all! + @mock_agent.expect(:send_bundle, true, []) + @mock_agent.expect(:send_bundle, true, []) + + mock_context = OpenStruct.new( + invoked_function_arn: 'test_arn', + function_name: 'test_function', + function_version: '$TEST', + aws_request_id: 'req-1' + ) + + @subject.wrap_aws(nil, mock_context) { 0 } + assert_raises(StandardError) { @subject.wrap_aws(nil, mock_context) { raise StandardError, 'boom' } } + @subject.wrap_aws(nil, mock_context) { 0 } + + first, second, third = Instana.processor.queued_spans.map { |s| s[:data][:lambda] } + + assert_equal 'req-1', first[:reqId] + assert_equal true, first[:coldStart] + assert_equal false, second[:coldStart] + assert_equal 'boom', second[:error] + assert_equal false, third[:coldStart] + assert_nil third[:error] + end + def test_lambda_http clear_all!