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.



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
954
955
956
957
958
959
960
# File 'lib/logbrew.rb', line 923

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:



837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
# File 'lib/logbrew.rb', line 837

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



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
917
918
919
920
921
# File 'lib/logbrew.rb', line 886

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



1051
1052
1053
# File 'lib/logbrew.rb', line 1051

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

#delivery_healthObject



974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
# File 'lib/logbrew.rb', line 974

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



970
971
972
# File 'lib/logbrew.rb', line 970

def dropped_events
  @event_queue.dropped_events
end

#environment(id, timestamp, attributes) ⇒ Object



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

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

#flush(transport = nil) ⇒ Object



1055
1056
1057
1058
1059
1060
1061
1062
1063
1064
1065
1066
1067
1068
1069
1070
1071
1072
# File 'lib/logbrew.rb', line 1055

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



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

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

#log(id, timestamp, attributes) ⇒ Object



1039
1040
1041
# File 'lib/logbrew.rb', line 1039

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

#metric(id, timestamp, attributes) ⇒ Object



1047
1048
1049
# File 'lib/logbrew.rb', line 1047

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

#pending_event_bytesObject



966
967
968
# File 'lib/logbrew.rb', line 966

def pending_event_bytes
  @event_queue.pending_bytes
end

#pending_eventsObject



962
963
964
# File 'lib/logbrew.rb', line 962

def pending_events
  @event_queue.length
end

#preview_jsonObject



1009
1010
1011
1012
# File 'lib/logbrew.rb', line 1009

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

#purge_pending_eventsObject



1014
1015
1016
1017
1018
1019
1020
1021
1022
1023
1024
1025
# File 'lib/logbrew.rb', line 1014

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



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

def recover_automatic_delivery
  controller = require_automatic_delivery
  controller.assert_process_ownership!
  flush
end

#release(id, timestamp, attributes) ⇒ Object



1027
1028
1029
# File 'lib/logbrew.rb', line 1027

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

#shutdown(transport = nil) ⇒ Object



1074
1075
1076
1077
1078
1079
1080
1081
1082
1083
1084
1085
1086
1087
1088
1089
1090
1091
1092
1093
1094
1095
1096
1097
1098
1099
1100
1101
1102
1103
1104
1105
# File 'lib/logbrew.rb', line 1074

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



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

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

#stop_automatic_deliveryObject



1004
1005
1006
1007
# File 'lib/logbrew.rb', line 1004

def stop_automatic_delivery
  controller = require_automatic_delivery
  controller.stop
end