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) ⇒ Client

Returns a new instance of Client.



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
885
886
887
888
889
# File 'lib/logbrew.rb', line 854

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
)
  @api_key = api_key
  @sdk = sdk
  @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) ⇒ Object

Raises:



783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
# File 'lib/logbrew.rb', line 783

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
)
  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?

  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
  )
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



817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
# File 'lib/logbrew.rb', line 817

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



980
981
982
# File 'lib/logbrew.rb', line 980

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

#delivery_healthObject



903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
# File 'lib/logbrew.rb', line 903

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



899
900
901
# File 'lib/logbrew.rb', line 899

def dropped_events
  @event_queue.dropped_events
end

#environment(id, timestamp, attributes) ⇒ Object



960
961
962
# File 'lib/logbrew.rb', line 960

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

#flush(transport = nil) ⇒ Object



984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
1001
# File 'lib/logbrew.rb', line 984

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



964
965
966
# File 'lib/logbrew.rb', line 964

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

#log(id, timestamp, attributes) ⇒ Object



968
969
970
# File 'lib/logbrew.rb', line 968

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

#metric(id, timestamp, attributes) ⇒ Object



976
977
978
# File 'lib/logbrew.rb', line 976

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

#pending_event_bytesObject



895
896
897
# File 'lib/logbrew.rb', line 895

def pending_event_bytes
  @event_queue.pending_bytes
end

#pending_eventsObject



891
892
893
# File 'lib/logbrew.rb', line 891

def pending_events
  @event_queue.length
end

#preview_jsonObject



938
939
940
941
# File 'lib/logbrew.rb', line 938

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

#purge_pending_eventsObject



943
944
945
946
947
948
949
950
951
952
953
954
# File 'lib/logbrew.rb', line 943

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



927
928
929
930
931
# File 'lib/logbrew.rb', line 927

def recover_automatic_delivery
  controller = require_automatic_delivery
  controller.assert_process_ownership!
  flush
end

#release(id, timestamp, attributes) ⇒ Object



956
957
958
# File 'lib/logbrew.rb', line 956

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

#shutdown(transport = nil) ⇒ Object



1003
1004
1005
1006
1007
1008
1009
1010
1011
1012
1013
1014
1015
1016
1017
1018
1019
1020
1021
1022
1023
1024
1025
1026
1027
1028
1029
1030
1031
1032
1033
1034
# File 'lib/logbrew.rb', line 1003

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



972
973
974
# File 'lib/logbrew.rb', line 972

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

#stop_automatic_deliveryObject



933
934
935
936
# File 'lib/logbrew.rb', line 933

def stop_automatic_delivery
  controller = require_automatic_delivery
  controller.stop
end