Class: Aws::Plugins::ClientMetricsPlugin::Handler

Inherits:
Seahorse::Client::Handler show all
Defined in:
lib/aws-sdk-core/plugins/client_metrics_plugin.rb

Instance Attribute Summary

Attributes inherited from Seahorse::Client::Handler

#handler

Instance Method Summary collapse

Methods inherited from Seahorse::Client::Handler

#initialize, #inspect

Constructor Details

This class inherits a constructor from Seahorse::Client::Handler

Instance Method Details

#call(context) ⇒ Object



96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
# File 'lib/aws-sdk-core/plugins/client_metrics_plugin.rb', line 96

def call(context)
  publisher = context.config.client_side_monitoring_publisher
  service_id = context.config.api.["serviceId"]
  # serviceId not present in all versions, need a fallback
  service_id ||= _calculate_service_id(context)

  request_metrics = ClientSideMonitoring::RequestMetrics.new(
    service: service_id,
    operation: context.operation.name,
    client_id: context.config.client_side_monitoring_client_id,
    region: context.config.region,
    timestamp: DateTime.now.strftime('%Q').to_i,
  )
  context.[:client_metrics] = request_metrics
  start_time = Aws::Util.monotonic_milliseconds
  final_error_retryable = false
  final_aws_exception = nil
  final_aws_exception_message = nil
  final_sdk_exception = nil
  final_sdk_exception_message = nil
  begin
    @handler.call(context)
  rescue StandardError => e
    # Handle SDK Exceptions
    inspector = Aws::Plugins::RetryErrors::ErrorInspector.new(
      e,
      context.http_response.status_code
    )
    if inspector.retryable?(context)
      final_error_retryable = true
    end
    if request_metrics.api_call_attempts.empty?
      attempt = request_metrics.build_call_attempt
      attempt.sdk_exception = e.class.to_s
      attempt.sdk_exception_msg = e.message
      request_metrics.add_call_attempt(attempt)
    elsif request_metrics.api_call_attempts.last.aws_exception.nil?
      # Handle exceptions during response handlers
      attempt = request_metrics.api_call_attempts.last
      attempt.sdk_exception = e.class.to_s
      attempt.sdk_exception_msg = e.message
    elsif !e.class.to_s.match(request_metrics.api_call_attempts.last.aws_exception)
      # Handle response handling exceptions that happened in addition to
      # an AWS exception
      attempt = request_metrics.api_call_attempts.last
      attempt.sdk_exception = e.class.to_s
      attempt.sdk_exception_msg = e.message
    end # Else we don't have an SDK exception and are done.
    final_attempt = request_metrics.api_call_attempts.last
    final_aws_exception = final_attempt.aws_exception
    final_aws_exception_message = final_attempt.aws_exception_msg
    final_sdk_exception = final_attempt.sdk_exception
    final_sdk_exception_message = final_attempt.sdk_exception_msg
    raise e
  ensure
    end_time = Aws::Util.monotonic_milliseconds
    complete_opts = {
      latency: end_time - start_time,
      attempt_count: context.retries + 1,
      user_agent: context.http_request.headers["user-agent"],
      final_error_retryable: final_error_retryable,
      final_http_status_code: context.http_response.status_code,
      final_aws_exception: final_aws_exception,
      final_aws_exception_message: final_aws_exception_message,
      final_sdk_exception: final_sdk_exception,
      final_sdk_exception_message: final_sdk_exception_message
    }
    if context.[:redirect_region]
      complete_opts[:region] = context.[:redirect_region]
    end
    request_metrics.api_call.complete(complete_opts)
    # Report the metrics by passing the complete RequestMetrics object
    if publisher
      publisher.publish(request_metrics)
    end # Else we drop all this on the floor.
  end
end