From b375af1c2b737601bc3ec7037e6261470eb7f6d3 Mon Sep 17 00:00:00 2001 From: Robert Date: Mon, 22 Nov 2021 09:09:34 -0600 Subject: [PATCH] fix: aws-sdk nitpicks (#1023) --- .../instrumentation/aws_sdk/db_helper.rb | 28 --------- .../instrumentation/aws_sdk/handler.rb | 57 +++++++------------ .../aws_sdk/instrumentation.rb | 12 ++-- 3 files changed, 28 insertions(+), 69 deletions(-) delete mode 100644 instrumentation/aws_sdk/lib/opentelemetry/instrumentation/aws_sdk/db_helper.rb diff --git a/instrumentation/aws_sdk/lib/opentelemetry/instrumentation/aws_sdk/db_helper.rb b/instrumentation/aws_sdk/lib/opentelemetry/instrumentation/aws_sdk/db_helper.rb deleted file mode 100644 index ec17d3aa0..000000000 --- a/instrumentation/aws_sdk/lib/opentelemetry/instrumentation/aws_sdk/db_helper.rb +++ /dev/null @@ -1,28 +0,0 @@ -# frozen_string_literal: true - -# Copyright The OpenTelemetry Authors -# -# SPDX-License-Identifier: Apache-2.0 - -module OpenTelemetry - module Instrumentation - module AwsSdk - # DbHelper class provides methods for calculating aws db span attributes - class DbHelper - class << self - def get_db_attributes(context, service_name, operation) - return get_dynamodb_attributes(context, operation) if service_name == 'DynamoDB' - - {} - end - - def get_dynamodb_attributes(context, operation) - { - SemanticConventions::Trace::DB_SYSTEM => 'dynamodb' - } - end - end - end - end - end -end diff --git a/instrumentation/aws_sdk/lib/opentelemetry/instrumentation/aws_sdk/handler.rb b/instrumentation/aws_sdk/lib/opentelemetry/instrumentation/aws_sdk/handler.rb index a131803b7..4227c4cf6 100644 --- a/instrumentation/aws_sdk/lib/opentelemetry/instrumentation/aws_sdk/handler.rb +++ b/instrumentation/aws_sdk/lib/opentelemetry/instrumentation/aws_sdk/handler.rb @@ -9,51 +9,34 @@ module Instrumentation module AwsSdk # Generates Spans for all interactions with AwsSdk class Handler < Seahorse::Client::Handler - def call(context) - span_name = get_span_name(context) - attributes = get_span_attributes(context) + def call(context) # rubocop:disable Metrics/AbcSize, Metrics/MethodLength + return super unless context - tracer.in_span(span_name, kind: OpenTelemetry::Trace::SpanKind::CLIENT, attributes: attributes) do |span| - execute = proc { - super(context).tap do |response| - if (err = response.error) - span.record_exception(err) - span.status = Trace::Status.error(err) - end - end - } + service_name = context.client.class.api.metadata['serviceId'] || context.client.class.to_s.split('::')[1] + operation = context.operation&.name + attributes = { + 'aws.region' => context.config.region, + OpenTelemetry::SemanticConventions::Trace::RPC_SYSTEM => 'aws-api', + OpenTelemetry::SemanticConventions::Trace::RPC_METHOD => operation, + OpenTelemetry::SemanticConventions::Trace::RPC_SERVICE => service_name + } + attributes[SemanticConventions::Trace::DB_SYSTEM] = 'dynamodb' if service_name == 'DynamoDB' + tracer.in_span("#{service_name}.#{operation}", attributes: attributes, kind: OpenTelemetry::Trace::SpanKind::CLIENT) do |span| if instrumentation_config[:suppress_internal_instrumentation] - OpenTelemetry::Common::Utilities.untraced(&execute) + OpenTelemetry::Common::Utilities.untraced { super } else - execute.call + super + end.tap do |response| + if (err = response.error) + span.record_exception(err) + span.status = Trace::Status.error(err) + end end end end - def get_span_attributes(context) - span_attributes = { - 'aws.region' => context.config.region, - OpenTelemetry::SemanticConventions::Trace::RPC_SYSTEM => 'aws-api', - OpenTelemetry::SemanticConventions::Trace::RPC_METHOD => get_operation(context), - OpenTelemetry::SemanticConventions::Trace::RPC_SERVICE => get_service_name(context) - } - - db_attributes = DbHelper.get_db_attributes(context, get_service_name(context), get_operation(context)) - span_attributes.merge(db_attributes) - end - - def get_service_name(context) - context&.client.class.api.metadata['serviceId'] || context&.client.class.to_s.split('::')[1] - end - - def get_operation(context) - context&.operation&.name - end - - def get_span_name(context) - "#{get_service_name(context)}.#{get_operation(context)}" - end + private def tracer AwsSdk::Instrumentation.instance.tracer diff --git a/instrumentation/aws_sdk/lib/opentelemetry/instrumentation/aws_sdk/instrumentation.rb b/instrumentation/aws_sdk/lib/opentelemetry/instrumentation/aws_sdk/instrumentation.rb index f38d4922d..de3bd65fd 100644 --- a/instrumentation/aws_sdk/lib/opentelemetry/instrumentation/aws_sdk/instrumentation.rb +++ b/instrumentation/aws_sdk/lib/opentelemetry/instrumentation/aws_sdk/instrumentation.rb @@ -12,10 +12,7 @@ class Instrumentation < OpenTelemetry::Instrumentation::Base MINIMUM_VERSION = Gem::Version.new('2.0') install do |_config| - require_relative 'handler' - require_relative 'services' - require_relative 'db_helper' - + require_dependencies add_plugin(Seahorse::Client::Base, *loaded_constants) end @@ -29,6 +26,13 @@ class Instrumentation < OpenTelemetry::Instrumentation::Base option :suppress_internal_instrumentation, default: false, validate: :boolean + private + + def require_dependencies + require_relative 'handler' + require_relative 'services' + end + def gem_version if Gem.loaded_specs['aws-sdk'] Gem.loaded_specs['aws-sdk'].version