Class: LogBrew::Client

Inherits:
Object
  • Object
show all
Includes:
TraceClientMethods
Defined in:
lib/logbrew.rb

Constant Summary collapse

DEFAULT_AUTOMATIC_FLUSH_INTERVAL =
5.0
DEFAULT_AUTOMATIC_FLUSH_THRESHOLD =
100
DEFAULT_AUTOMATIC_RETRY_BASE_DELAY =
0.25
DEFAULT_AUTOMATIC_RETRY_MAX_DELAY =
30.0

Class Method Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(api_key:, sdk:, max_retries:, max_queue_size: BoundedEventQueue::DEFAULT_MAX_SIZE, max_queue_bytes: BoundedEventQueue::DEFAULT_MAX_BYTES, on_event_dropped: nil, max_batch_size: EventBatcher::DEFAULT_MAX_SIZE, max_batch_bytes: EventBatcher::DEFAULT_MAX_BYTES, persistent_queue_path: nil, context: nil) ⇒ Client

Returns a new instance of Client.



955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
# File 'lib/logbrew.rb', line 955

def initialize(
  api_key:,
  sdk:,
  max_retries:,
  max_queue_size: BoundedEventQueue::DEFAULT_MAX_SIZE,
  max_queue_bytes: BoundedEventQueue::DEFAULT_MAX_BYTES,
  on_event_dropped: nil,
  max_batch_size: EventBatcher::DEFAULT_MAX_SIZE,
  max_batch_bytes: EventBatcher::DEFAULT_MAX_BYTES,
  persistent_queue_path: nil,
  context: nil
)
  @api_key = api_key
  @sdk = sdk
  @context = context
  @max_retries = max_retries
  @event_batcher = EventBatcher.new(sdk: sdk, max_size: max_batch_size, max_bytes: max_batch_bytes)
  event_store = nil
  begin
    event_store = PersistentEventStore.open(path: persistent_queue_path) unless persistent_queue_path.nil?
    @event_queue = BoundedEventQueue.new(
      max_size: max_queue_size,
      max_bytes: max_queue_bytes,
      max_event_bytes: @event_batcher.max_event_bytes,
      on_event_dropped: on_event_dropped,
      event_store: event_store
    )
  rescue StandardError
    event_store&.close
    raise
  end
  @state_mutex = Mutex.new
  @flush_mutex = Mutex.new
  @closed = false
  @closing = false
  @retry_batch = nil
  @automatic_delivery = nil
end

Class Method Details

.create(api_key:, sdk_name:, sdk_version:, max_retries: 2, max_queue_size: BoundedEventQueue::DEFAULT_MAX_SIZE, max_queue_bytes: BoundedEventQueue::DEFAULT_MAX_BYTES, on_event_dropped: nil, max_batch_size: EventBatcher::DEFAULT_MAX_SIZE, max_batch_bytes: EventBatcher::DEFAULT_MAX_BYTES, persistent_queue_path: nil, context: nil, capture_runtime_context: true) ⇒ Object

Raises:



869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
# File 'lib/logbrew.rb', line 869

def self.create(
  api_key:,
  sdk_name:,
  sdk_version:,
  max_retries: 2,
  max_queue_size: BoundedEventQueue::DEFAULT_MAX_SIZE,
  max_queue_bytes: BoundedEventQueue::DEFAULT_MAX_BYTES,
  on_event_dropped: nil,
  max_batch_size: EventBatcher::DEFAULT_MAX_SIZE,
  max_batch_bytes: EventBatcher::DEFAULT_MAX_BYTES,
  persistent_queue_path: nil,
  context: nil,
  capture_runtime_context: true
)
  Validation.require_non_empty("api_key", api_key)
  Validation.require_non_empty("sdk_name", sdk_name)
  Validation.require_non_empty("sdk_version", sdk_version)
  raise SdkError.new("validation_error", "max_retries must be non-negative") if max_retries.negative?
  unless context.nil? || context.is_a?(TelemetryContext)
    raise SdkError.new("validation_error", "client context must be a LogBrew::TelemetryContext")
  end
  unless capture_runtime_context == true || capture_runtime_context == false
    raise SdkError.new("validation_error", "capture_runtime_context must be a boolean")
  end

  resolved_context = if capture_runtime_context
                       TelemetryContext.merge(TelemetryContext.runtime_defaults, context)
                     else
                       TelemetryContext.merge(nil, context)
                     end

  new(
    api_key: api_key,
    sdk: {
      "name" => sdk_name.dup.freeze,
      "language" => "ruby",
      "version" => sdk_version.dup.freeze
    }.freeze,
    max_retries: max_retries,
    max_queue_size: max_queue_size,
    max_queue_bytes: max_queue_bytes,
    on_event_dropped: on_event_dropped,
    max_batch_size: max_batch_size,
    max_batch_bytes: max_batch_bytes,
    persistent_queue_path: persistent_queue_path,
    context: resolved_context
  )
end

.create_automatic(api_key:, sdk_name:, sdk_version:, transport:, flush_interval: DEFAULT_AUTOMATIC_FLUSH_INTERVAL, flush_threshold: DEFAULT_AUTOMATIC_FLUSH_THRESHOLD, retry_base_delay: DEFAULT_AUTOMATIC_RETRY_BASE_DELAY, retry_max_delay: DEFAULT_AUTOMATIC_RETRY_MAX_DELAY, **options) ⇒ Object



918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
# File 'lib/logbrew.rb', line 918

def self.create_automatic(
  api_key:,
  sdk_name:,
  sdk_version:,
  transport:,
  flush_interval: DEFAULT_AUTOMATIC_FLUSH_INTERVAL,
  flush_threshold: DEFAULT_AUTOMATIC_FLUSH_THRESHOLD,
  retry_base_delay: DEFAULT_AUTOMATIC_RETRY_BASE_DELAY,
  retry_max_delay: DEFAULT_AUTOMATIC_RETRY_MAX_DELAY,
  **options
)
  max_queue_size = options.fetch(:max_queue_size, BoundedEventQueue::DEFAULT_MAX_SIZE)
  AutomaticDelivery.validate!(
    transport: transport,
    flush_interval: flush_interval,
    flush_threshold: flush_threshold,
    retry_base_delay: retry_base_delay,
    retry_max_delay: retry_max_delay,
    max_queue_size: max_queue_size
  )
  client = create(
    api_key: api_key,
    sdk_name: sdk_name,
    sdk_version: sdk_version,
    **options
  )
  client.send(
    :enable_automatic_delivery,
    transport: transport,
    flush_interval: flush_interval,
    flush_threshold: flush_threshold,
    retry_base_delay: retry_base_delay,
    retry_max_delay: retry_max_delay
  )
  client
end

Instance Method Details

#action(id, timestamp, attributes) ⇒ Object



1083
1084
1085
# File 'lib/logbrew.rb', line 1083

def action(id, timestamp, attributes)
  push_event("action", id, timestamp, validate_action(attributes), event_context(attributes))
end

#delivery_healthObject



1006
1007
1008
1009
1010
1011
1012
1013
1014
1015
1016
1017
1018
1019
1020
1021
1022
1023
1024
1025
1026
1027
1028
# File 'lib/logbrew.rb', line 1006

def delivery_health
  controller = @automatic_delivery
  unless controller.nil?
    controller.assert_process_ownership!
    return controller.snapshot
  end

  metrics = queue_metrics
  state = @state_mutex.synchronize { @closed ? "closed" : "manual" }
  DeliveryHealth.new(
    state: state,
    queued_events: metrics.fetch(:queued_events),
    queued_bytes: metrics.fetch(:queued_bytes),
    dropped_events: metrics.fetch(:dropped_events),
    in_flight: false,
    last_outcome: "none",
    consecutive_failures: 0,
    pause_reason: nil,
    successful_flushes: 0,
    failed_flushes: 0,
    retry_delay_ms: 0
  )
end

#dropped_eventsObject



1002
1003
1004
# File 'lib/logbrew.rb', line 1002

def dropped_events
  @event_queue.dropped_events
end

#environment(id, timestamp, attributes) ⇒ Object



1063
1064
1065
# File 'lib/logbrew.rb', line 1063

def environment(id, timestamp, attributes)
  push_event("environment", id, timestamp, validate_environment(attributes), event_context(attributes))
end

#flush(transport = nil) ⇒ Object



1087
1088
1089
1090
1091
1092
1093
1094
1095
1096
1097
1098
1099
1100
1101
1102
1103
1104
# File 'lib/logbrew.rb', line 1087

def flush(transport = nil)
  ensure_not_reentrant_flush
  controller = @automatic_delivery
  controller&.assert_process_ownership!
  resolved_transport = resolve_transport(transport)
  @flush_mutex.synchronize do
    @state_mutex.synchronize { ensure_open }
    previous = controller&.begin_explicit_flush
    begin
      response = flush_internal(resolved_transport)
      controller&.complete_explicit_flush(previous, response)
      response
    rescue StandardError => error
      controller&.fail_explicit_flush(previous, error)
      raise
    end
  end
end

#issue(id, timestamp, attributes) ⇒ Object



1067
1068
1069
# File 'lib/logbrew.rb', line 1067

def issue(id, timestamp, attributes)
  push_event("issue", id, timestamp, validate_issue(attributes), event_context(attributes))
end

#log(id, timestamp, attributes) ⇒ Object



1071
1072
1073
# File 'lib/logbrew.rb', line 1071

def log(id, timestamp, attributes)
  push_event("log", id, timestamp, validate_log(attributes), event_context(attributes))
end

#metric(id, timestamp, attributes) ⇒ Object



1079
1080
1081
# File 'lib/logbrew.rb', line 1079

def metric(id, timestamp, attributes)
  push_event("metric", id, timestamp, validate_metric(attributes), event_context(attributes))
end

#pending_event_bytesObject



998
999
1000
# File 'lib/logbrew.rb', line 998

def pending_event_bytes
  @event_queue.pending_bytes
end

#pending_eventsObject



994
995
996
# File 'lib/logbrew.rb', line 994

def pending_events
  @event_queue.length
end

#preview_jsonObject



1041
1042
1043
1044
# File 'lib/logbrew.rb', line 1041

def preview_json
  snapshot = @event_queue.snapshot
  JSON.pretty_generate("sdk" => @sdk, "events" => snapshot.events)
end

#purge_pending_eventsObject



1046
1047
1048
1049
1050
1051
1052
1053
1054
1055
1056
1057
# File 'lib/logbrew.rb', line 1046

def purge_pending_events
  ensure_not_reentrant_flush
  controller = @automatic_delivery
  controller&.assert_process_ownership!
  @flush_mutex.synchronize do
    @state_mutex.synchronize { ensure_open }
    @retry_batch = nil
    removed = @event_queue.purge
    controller&.queue_changed
    removed
  end
end

#recover_automatic_deliveryObject



1030
1031
1032
1033
1034
# File 'lib/logbrew.rb', line 1030

def recover_automatic_delivery
  controller = require_automatic_delivery
  controller.assert_process_ownership!
  flush
end

#release(id, timestamp, attributes) ⇒ Object



1059
1060
1061
# File 'lib/logbrew.rb', line 1059

def release(id, timestamp, attributes)
  push_event("release", id, timestamp, validate_release(attributes), event_context(attributes))
end

#shutdown(transport = nil) ⇒ Object



1106
1107
1108
1109
1110
1111
1112
1113
1114
1115
1116
1117
1118
1119
1120
1121
1122
1123
1124
1125
1126
1127
1128
1129
1130
1131
1132
1133
1134
1135
1136
1137
# File 'lib/logbrew.rb', line 1106

def shutdown(transport = nil)
  ensure_not_reentrant_flush
  controller = @automatic_delivery
  controller&.assert_process_ownership!
  resolved_transport = resolve_transport(transport)
  controller&.prepare_shutdown
  @flush_mutex.synchronize do
    @state_mutex.synchronize do
      ensure_open
      @closing = true
    end

    succeeded = false
    failure = nil
    begin
      response = flush_internal(resolved_transport)
      @event_queue.close
      succeeded = true
      controller&.complete_shutdown(response)
      response
    rescue StandardError => error
      failure = error
      raise
    ensure
      @state_mutex.synchronize do
        @closed = true if succeeded
        @closing = false
      end
      controller&.resume_after_failed_shutdown(failure) unless succeeded
    end
  end
end

#span(id, timestamp, attributes) ⇒ Object



1075
1076
1077
# File 'lib/logbrew.rb', line 1075

def span(id, timestamp, attributes)
  push_event("span", id, timestamp, validate_span(attributes), event_context(attributes))
end

#stop_automatic_deliveryObject



1036
1037
1038
1039
# File 'lib/logbrew.rb', line 1036

def stop_automatic_delivery
  controller = require_automatic_delivery
  controller.stop
end