Skip to content

Commit 7eb72ea

Browse files
committed
Improve storage_cli log message readability
- Add operation and key args to storage_cli.call.finish message for quick scanning - Use labeled args (src=, dest=, key=, verb=, expires_in=) for clarity - Use File.basename to keep message concise without losing context - Log spawn errors with storage_cli.call.error including duration and error detail - Fix symbol key serialization bug in operation_log_fields (was logging :operation instead of operation)
1 parent 2ff1297 commit 7eb72ea

2 files changed

Lines changed: 109 additions & 41 deletions

File tree

‎lib/cloud_controller/blobstore/storage_cli/storage_cli_client.rb‎

Lines changed: 57 additions & 21 deletions
Original file line numberDiff line numberDiff line change
@@ -285,11 +285,11 @@ def run_cli(command, *args, allow_exit_code_three: false, blob_size: nil)
285285
exit_code = resolve_exit_code(status)
286286
success = status.success? || (allow_exit_code_three && status.exitstatus == 3)
287287

288-
logger.info('storage_cli.call.finish',
288+
logger.info("storage_cli.call.finish #{command}#{log_message_arg_suffix(command, args)}",
289289
log_fields.merge(
290-
duration_seconds: duration,
291-
exit_code: exit_code,
292-
success: success
290+
'duration_seconds' => duration,
291+
'exit_code' => exit_code,
292+
'success' => success
293293
))
294294

295295
unless success
@@ -307,45 +307,81 @@ def run_cli(command, *args, allow_exit_code_three: false, blob_size: nil)
307307

308308
def operation_log_fields(command, args, blob_size: nil)
309309
fields = {
310-
operation: command,
311-
bucket: @directory_key,
312-
resource_type: @resource_type
310+
'operation' => command,
311+
'bucket' => @directory_key,
312+
'resource_type' => @resource_type
313313
}
314314

315315
case command
316316
when 'put'
317-
fields[:source_path] = args[0]
318-
fields[:destination_key] = args[1]
317+
fields['source_path'] = args[0]
318+
fields['destination_key'] = args[1]
319319
when 'get'
320-
fields[:source_key] = args[0]
321-
fields[:destination_path] = args[1]
320+
fields['source_key'] = args[0]
321+
fields['destination_path'] = args[1]
322322
when 'copy'
323-
fields[:source_key] = args[0]
324-
fields[:destination_key] = args[1]
323+
fields['source_key'] = args[0]
324+
fields['destination_key'] = args[1]
325325
when 'exists', 'delete', 'properties'
326-
fields[:key] = args[0]
326+
fields['key'] = args[0]
327327
when 'list'
328-
fields[:prefix] = args[0]
328+
fields['prefix'] = args[0]
329329
when 'delete-recursive'
330-
fields[:path] = args[0]
330+
fields['path'] = args[0]
331331
when 'sign', 'sign-public', 'sign-internal'
332-
fields[:key] = args[0]
333-
fields[:verb] = args[1]
334-
fields[:expires_in] = args[2]
332+
fields['key'] = args[0]
333+
fields['verb'] = args[1]
334+
fields['expires_in'] = args[2]
335335
end
336336

337-
fields[:size] = blob_size if blob_size
337+
fields['size'] = blob_size if blob_size
338338
fields
339339
end
340340

341341
def capture_cli(command, args, start_time)
342342
Open3.capture3(@cli_path, '-s', @storage_type, '-c', @config_file, *additional_flags, command, *args)
343343
rescue StandardError => e
344344
# exit_code=-1 is a sentinel for spawn failures (binary missing, permission denied).
345-
safe_emit_metrics(command, -1, Time.now.utc - start_time, 'spawn error')
345+
duration = Time.now.utc - start_time
346+
logger.error("storage_cli.call.error #{command}#{log_message_arg_suffix(command, args)}",
347+
'operation' => command,
348+
'bucket' => @directory_key,
349+
'resource_type' => @resource_type,
350+
'duration_seconds' => duration,
351+
'error' => e.inspect)
352+
safe_emit_metrics(command, -1, duration, 'spawn error')
346353
raise BlobstoreError.new(e.inspect)
347354
end
348355

356+
def log_message_arg_suffix(command, args)
357+
printable_args =
358+
case command
359+
when 'put', 'copy', 'get'
360+
["src=#{summarize_log_arg(args[0])}", "dest=#{summarize_log_arg(args[1])}"]
361+
when 'sign', 'sign-public', 'sign-internal'
362+
[
363+
"key=#{summarize_log_arg(args[0])}",
364+
"verb=#{args[1]}",
365+
"expires_in=#{args[2]}"
366+
]
367+
when 'exists', 'delete', 'properties'
368+
["key=#{summarize_log_arg(args[0])}"]
369+
when 'list'
370+
["prefix=#{summarize_log_arg(args[0])}"]
371+
when 'delete-recursive'
372+
["path=#{summarize_log_arg(args[0])}"]
373+
else
374+
[]
375+
end
376+
377+
printable_args = printable_args.compact.map(&:to_s).reject(&:empty?)
378+
printable_args.empty? ? '' : " #{printable_args.join(' ')}"
379+
end
380+
381+
def summarize_log_arg(arg)
382+
File.basename(arg.to_s)
383+
end
384+
349385
def resolve_exit_code(status)
350386
if status.exitstatus
351387
status.exitstatus

‎spec/unit/lib/cloud_controller/blobstore/storage_cli/storage_cli_client_spec.rb‎

Lines changed: 52 additions & 20 deletions
Original file line numberDiff line numberDiff line change
@@ -510,17 +510,17 @@ def build_client(resource_type)
510510

511511
client.send(:run_cli, 'put', 'src-path', 'dest-key', blob_size: 1024)
512512

513-
expect(logger_double).to have_received(:info).with('storage_cli.call.finish', hash_including(
514-
operation: 'put',
515-
bucket: directory_key,
516-
resource_type: resource_type,
517-
source_path: 'src-path',
518-
destination_key: 'dest-key',
519-
size: 1024,
520-
exit_code: 0,
521-
success: true,
522-
duration_seconds: a_kind_of(Float)
523-
))
513+
expect(logger_double).to have_received(:info).with('storage_cli.call.finish put src=src-path dest=dest-key', hash_including(
514+
'operation' => 'put',
515+
'bucket' => directory_key,
516+
'resource_type' => resource_type,
517+
'source_path' => 'src-path',
518+
'destination_key' => 'dest-key',
519+
'size' => 1024,
520+
'exit_code' => 0,
521+
'success' => true,
522+
'duration_seconds' => a_kind_of(Float)
523+
))
524524
end
525525

526526
it 'logs finish fields for failed calls' do
@@ -531,15 +531,40 @@ def build_client(resource_type)
531531
client.send(:run_cli, 'exists', 'arg1')
532532
end.to raise_error(RuntimeError, /storage-cli exists failed with exit code 1/)
533533

534-
expect(logger_double).to have_received(:info).with('storage_cli.call.finish', hash_including(
535-
operation: 'exists',
536-
bucket: directory_key,
537-
resource_type: resource_type,
538-
key: 'arg1',
539-
exit_code: 1,
540-
success: false,
541-
duration_seconds: a_kind_of(Float)
542-
))
534+
expect(logger_double).to have_received(:info).with('storage_cli.call.finish exists key=arg1', hash_including(
535+
'operation' => 'exists',
536+
'bucket' => directory_key,
537+
'resource_type' => resource_type,
538+
'key' => 'arg1',
539+
'exit_code' => 1,
540+
'success' => false,
541+
'duration_seconds' => a_kind_of(Float)
542+
))
543+
end
544+
545+
it 'uses basename of key arg in message text with key= label but keeps full structured key' do
546+
status = instance_double(Process::Status, success?: true, exitstatus: 0)
547+
allow(Open3).to receive(:capture3).and_return(['ok', '', status])
548+
549+
long_key = 'app_bits_cache/97/d0/97d004b6ef4a8c8f5e3b6a0f4c1d2e3f7a8b9c00112233445566778899aabbcc'
550+
client.send(:run_cli, 'exists', long_key)
551+
552+
expect(logger_double).to have_received(:info).with(
553+
'storage_cli.call.finish exists key=97d004b6ef4a8c8f5e3b6a0f4c1d2e3f7a8b9c00112233445566778899aabbcc',
554+
hash_including('key' => long_key, 'success' => true)
555+
)
556+
end
557+
558+
it 'labels sign args in message text' do
559+
status = instance_double(Process::Status, success?: true, exitstatus: 0)
560+
allow(Open3).to receive(:capture3).and_return(['ok', '', status])
561+
562+
client.send(:run_cli, 'sign', 'abc123def456', 'get', '3600s')
563+
564+
expect(logger_double).to have_received(:info).with(
565+
'storage_cli.call.finish sign key=abc123def456 verb=get expires_in=3600s',
566+
hash_including('key' => 'abc123def456', 'verb' => 'get', 'expires_in' => '3600s', 'success' => true)
567+
)
543568
end
544569

545570
it 'returns output and status on success' do
@@ -611,6 +636,13 @@ def build_client(resource_type)
611636
expect(metrics_updater).to have_received(:report_storage_cli_metrics).with(
612637
operation: 'get', exit_code: -1, duration_seconds: a_kind_of(Float), blob_size: nil
613638
)
639+
expect(logger_double).to have_received(:error).with('storage_cli.call.error get src=some-key dest=', hash_including(
640+
'operation' => 'get',
641+
'bucket' => directory_key,
642+
'resource_type' => resource_type,
643+
'duration_seconds' => a_kind_of(Float),
644+
'error' => a_string_matching(/Errno::ENOENT/)
645+
))
614646
end
615647

616648
it 'lets Interrupt propagate from Open3.capture3' do

0 commit comments

Comments
 (0)