Repository navigation
Add structured logs and Prometheus metrics for blobstore and packer operations #5575
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
base: main
Are you sure you want to change the base?
Changes from all commits
7213657
2ff1297
b8427c3
f479098
5fe779c
0750a12
9098d09
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
| Original file line number | Diff line number | Diff line change |
|---|---|---|
|
|
@@ -20,7 +20,7 @@ class StorageCliClient < BaseClient | |
| # Native storage-cli type names supported by CC | ||
| STORAGE_CLI_TYPES = %w[azurebs alioss s3 gcs dav].freeze | ||
|
|
||
| def initialize(directory_key:, resource_type:, root_dir:, min_size: nil, max_size: nil) | ||
| def initialize(directory_key:, resource_type:, root_dir:, min_size: nil, max_size: nil, metrics_updater: nil) | ||
| raise 'Missing resource_type' if resource_type.nil? | ||
|
|
||
| config_file_path = config_path_for(resource_type) | ||
|
|
@@ -40,6 +40,7 @@ def initialize(directory_key:, resource_type:, root_dir:, min_size: nil, max_siz | |
| @root_dir = root_dir | ||
| @min_size = min_size || 0 | ||
| @max_size = max_size | ||
| @metrics_updater = metrics_updater | ||
| end | ||
|
|
||
| def fetch_config(resource_type) | ||
|
|
@@ -102,23 +103,53 @@ def download_from_blobstore(source_key, destination_path, mode: nil) | |
| def cp_to_blobstore(source_path, destination_key) | ||
| start = Time.now.utc | ||
| log_entry = 'cp-skip' | ||
| size = -1 | ||
| size = nil | ||
| metric_emitted = false | ||
|
|
||
| logger.info('cp-start', destination_key: destination_key, source_path: source_path, bucket: @directory_key) | ||
| logger.info('cp-start', destination_key: destination_key, source_path: source_path, bucket: @directory_key) rescue nil # rubocop:disable Style/RescueModifier | ||
|
|
||
| File.open(source_path) do |file| | ||
| size = file.size | ||
| next unless within_limits?(size) | ||
|
|
||
| run_cli('put', source_path, partitioned_key(destination_key)) | ||
| log_entry = 'cp-finish' | ||
| begin | ||
| File.open(source_path) do |file| | ||
| size = file.size | ||
| if within_limits?(size) | ||
| dest_key = partitioned_key(destination_key) | ||
| begin | ||
| run_cli('put', source_path, dest_key, blob_size: size) | ||
| ensure | ||
| metric_emitted = true | ||
| end | ||
| log_entry = 'cp-finish' | ||
| else | ||
| safe_emit_metrics('put', -4, Time.now.utc - start, 'cp_to_blobstore skip') | ||
| metric_emitted = true | ||
| log_entry = 'cp-skip' | ||
| end | ||
| end | ||
| rescue Interrupt, SignalException | ||
| log_entry = 'cp-error' | ||
| safe_emit_metrics('put', -5, Time.now.utc - start, 'signal before CLI') unless metric_emitted | ||
| raise | ||
| rescue StandardError | ||
| log_entry = 'cp-error' | ||
| safe_emit_metrics('put', -3, Time.now.utc - start, 'cp_to_blobstore') unless metric_emitted | ||
| raise | ||
| rescue Exception # rubocop:disable Lint/RescueException | ||
| log_entry = 'cp-error' | ||
| raise | ||
| ensure | ||
| logger.info(log_entry, | ||
| destination_key: destination_key, | ||
| duration_seconds: Time.now.utc - start, | ||
| size: size) rescue nil # rubocop:disable Style/RescueModifier | ||
| end | ||
| end | ||
|
|
||
| duration = Time.now.utc - start | ||
| logger.info(log_entry, | ||
| destination_key: destination_key, | ||
| duration_seconds: duration, | ||
| size: size) | ||
| def safe_emit_metrics(operation, exit_code, duration, context, blob_size: nil) | ||
| return if @metrics_updater.nil? | ||
|
|
||
| @metrics_updater.report_storage_cli_metrics(operation: operation, exit_code: exit_code, duration_seconds: duration, blob_size: blob_size) | ||
| rescue StandardError => e | ||
| logger.warn("storage-cli metrics failed in #{context}: #{e.inspect}") rescue nil # rubocop:disable Style/RescueModifier | ||
| end | ||
|
|
||
| def cp_file_between_keys(source_key, destination_key) | ||
|
|
@@ -170,25 +201,119 @@ def additional_flags | |
| flags_string.split | ||
| end | ||
|
|
||
| def run_cli(command, *args, allow_exit_code_three: false) | ||
| logger.debug("running storage-cli: #{@cli_path} -s #{@storage_type} -c #{@config_file} #{additional_flags.join(' ')} #{command} #{args.join(' ')}") | ||
|
|
||
| def run_cli(command, *args, allow_exit_code_three: false, blob_size: nil) | ||
| t = Time.now.utc | ||
| emitted = false | ||
| capture3_called = false | ||
| exit_code = nil | ||
| success = false | ||
| begin | ||
| stdout, stderr, status = Open3.capture3(@cli_path, '-s', @storage_type, '-c', @config_file, *additional_flags, command, *args) | ||
| stderr.split("\n").each do |line| | ||
| logger.info("[INFO] storage-cli: #{line}") | ||
| end | ||
| flags = additional_flags | ||
| cli_logger.debug("running storage-cli: #{@cli_path} -s #{@storage_type} -c #{@config_file} #{flags.join(' ')} #{command} #{args.join(' ')}") rescue nil # rubocop:disable Style/RescueModifier | ||
| capture3_called = true | ||
| stdout, stderr, status = Open3.capture3(@cli_path, '-s', @storage_type, '-c', @config_file, *flags, command, *args) | ||
| exit_code = resolve_exit_code(status) | ||
| success = status.success? || (allow_exit_code_three && status.exitstatus == 3) | ||
| rescue StandardError => e | ||
| duration = Time.now.utc - t | ||
| # -1: spawn failure (binary missing/permission); -3: setup error before capture3 (e.g. config load) | ||
| pre_spawn_code = capture3_called ? -1 : -3 | ||
| safe_emit_metrics(command, pre_spawn_code, duration, 'spawn error') | ||
| emitted = true # Set before logging so failures cannot trigger a second metric. | ||
| cli_logger.error("storage_cli.call.error #{command}#{log_message_arg_suffix(command, args)}", | ||
| log_fields(command, args, blob_size: blob_size).merge('duration_seconds' => duration, 'error' => e.inspect)) rescue nil # rubocop:disable Style/RescueModifier | ||
| raise BlobstoreError.new(e.inspect) | ||
| ensure | ||
|
Member
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Cancellation can become a retry If logging in this ensure block raises an IOError while this ensure block runs for a propagating Interrupt, the new IOError will replace the original Interrupt. The caller (RetryableClient) will retry the operation on an IOError instead of stopping. Logging and telemetry need to become "non-throwing". |
||
| emit_and_log_cli_result(command, args, status, exit_code, success, capture3_called, allow_exit_code_three, t, blob_size) unless emitted | ||
| end | ||
|
|
||
| unless status.success? || (allow_exit_code_three && status.exitstatus == 3) | ||
| raise "storage-cli #{command} failed with exit code #{status.exitstatus}, output: '#{stdout}', error: '#{stderr}'" | ||
| stderr_log_level = success ? :info : :error | ||
| stderr.split("\n").each do |line| | ||
| cli_logger.public_send(stderr_log_level, "[#{stderr_log_level.upcase}] storage-cli: #{line}") rescue nil # rubocop:disable Style/RescueModifier | ||
| end | ||
|
|
||
| raise "storage-cli #{command} failed with exit code #{exit_code.inspect}, output: '#{stdout}', error: '#{stderr}'" unless success | ||
|
|
||
| [stdout, status] | ||
| end | ||
|
|
||
| def emit_and_log_cli_result(command, args, status, exit_code, success, capture3_called, allow_exit_code_three, start_time, blob_size) | ||
| duration = Time.now.utc - start_time | ||
| if status | ||
| metric_exit_code = allow_exit_code_three && exit_code == 3 ? 0 : exit_code | ||
| safe_emit_metrics(command, metric_exit_code, duration, 'run_cli', blob_size: blob_size) | ||
| cli_logger.public_send(success ? :info : :error, "storage_cli.call.finish #{command}#{log_message_arg_suffix(command, args)}", | ||
| log_fields(command, args, blob_size: blob_size).merge('duration_seconds' => duration, 'exit_code' => exit_code, 'success' => success)) rescue nil # rubocop:disable Style/RescueModifier | ||
| else | ||
| mid_exec_code = mid_execution_exit_code(capture3_called) | ||
| safe_emit_metrics(command, mid_exec_code, duration, 'run_cli no status') | ||
| cli_logger.error("storage_cli.call.error #{command}#{log_message_arg_suffix(command, args)}", | ||
| log_fields(command, args, blob_size: blob_size).merge('duration_seconds' => duration, 'exit_code' => mid_exec_code, 'error' => $!.inspect)) rescue nil # rubocop:disable Style/RescueModifier, Style/SpecialGlobalVars | ||
| end | ||
| end | ||
|
|
||
| def log_message_arg_suffix(command, args) | ||
| case command | ||
| when 'put', 'get', 'copy' | ||
| " src=#{File.basename(args[0].to_s)} dest=#{File.basename(args[1].to_s)}" | ||
| when 'exists', 'delete', 'properties' | ||
| " key=#{File.basename(args[0].to_s)}" | ||
| when 'sign', 'sign-internal', 'sign-public' | ||
| " key=#{File.basename(args[0].to_s)} verb=#{args[1]} expires_in=#{args[2]}" | ||
| when 'list' | ||
| " prefix=#{File.basename(args[0].to_s)}" | ||
| else | ||
| args.empty? ? '' : " #{args.map { |a| File.basename(a.to_s) }.join(' ')}" | ||
| end | ||
| end | ||
|
|
||
| def log_fields(command, args, blob_size: nil) | ||
| fields = { 'operation' => command, 'bucket' => @directory_key, 'resource_type' => @resource_type } | ||
| case command | ||
| when 'put' | ||
| fields['source_path'] = args[0] | ||
| fields['destination_key'] = args[1] | ||
| fields['blob_size'] = blob_size unless blob_size.nil? | ||
| when 'get' | ||
| fields['source_key'] = args[0] | ||
| fields['destination_path'] = args[1] | ||
| when 'exists', 'delete', 'properties' | ||
| fields['key'] = args[0] | ||
| when 'sign', 'sign-internal', 'sign-public' | ||
| fields['key'] = args[0] | ||
| fields['verb'] = args[1] | ||
| fields['expires_in'] = args[2] | ||
| when 'copy' | ||
| fields['source_key'] = args[0] | ||
| fields['destination_key'] = args[1] | ||
| when 'list' | ||
| fields['prefix'] = args[0] | ||
| when 'delete-recursive' | ||
| fields['path'] = args[0] | ||
| end | ||
| fields | ||
| end | ||
|
|
||
| def resolve_exit_code(status) | ||
| if status.exitstatus | ||
| status.exitstatus | ||
| elsif status.termsig | ||
| 128 + status.termsig | ||
| else | ||
| # exit_code=-2: process exited with neither exitstatus nor termsig (platform anomaly). | ||
| -2 | ||
| end | ||
| end | ||
|
|
||
| def mid_execution_exit_code(capture3_called) | ||
| signal = $!.is_a?(Interrupt) || $!.is_a?(SignalException) # rubocop:disable Style/SpecialGlobalVars | ||
| if capture3_called | ||
| signal ? -6 : -7 | ||
| else | ||
| signal ? -5 : -7 | ||
| end | ||
| end | ||
|
|
||
| def sign_url(key, verb:, expires_in_seconds:, use_internal_url: false) | ||
| command = | ||
| if @storage_type == 'dav' | ||
|
|
@@ -228,6 +353,13 @@ def tmpdir | |
| def logger | ||
| @logger ||= Steno.logger('cc.blobstore.storage_cli_client') | ||
| end | ||
|
|
||
| def cli_logger | ||
| @cli_logger ||= Steno.logger('cc.blobstore.storage_cli').tap do |log| | ||
| level = VCAP::CloudController::Config.config.get(:storage_cli_logging_level) | ||
| log.level = level.to_sym if level | ||
| end | ||
| end | ||
| end | ||
| end | ||
| end | ||
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Consider refactoring this function, the math calculations are mixed into the logging, which makes it a little hard to follow. Here is example refactored code might look like:
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Worth doing. I will do the refactor.