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.



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
# File 'lib/logbrew.rb', line 840

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:



769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
# File 'lib/logbrew.rb', line 769

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



803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
# File 'lib/logbrew.rb', line 803

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



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

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

#delivery_healthObject



889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
# File 'lib/logbrew.rb', line 889

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



885
886
887
# File 'lib/logbrew.rb', line 885

def dropped_events
  @event_queue.dropped_events
end

#environment(id, timestamp, attributes) ⇒ Object



946
947
948
# File 'lib/logbrew.rb', line 946

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

#flush(transport = nil) ⇒ Object



970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
# File 'lib/logbrew.rb', line 970

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



950
951
952
# File 'lib/logbrew.rb', line 950

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

#log(id, timestamp, attributes) ⇒ Object



954
955
956
# File 'lib/logbrew.rb', line 954

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

#metric(id, timestamp, attributes) ⇒ Object



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

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

#pending_event_bytesObject



881
882
883
# File 'lib/logbrew.rb', line 881

def pending_event_bytes
  @event_queue.pending_bytes
end

#pending_eventsObject



877
878
879
# File 'lib/logbrew.rb', line 877

def pending_events
  @event_queue.length
end

#preview_jsonObject



924
925
926
927
# File 'lib/logbrew.rb', line 924

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

#purge_pending_eventsObject



929
930
931
932
933
934
935
936
937
938
939
940
# File 'lib/logbrew.rb', line 929

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



913
914
915
916
917
# File 'lib/logbrew.rb', line 913

def recover_automatic_delivery
  controller = require_automatic_delivery
  controller.assert_process_ownership!
  flush
end

#release(id, timestamp, attributes) ⇒ Object



942
943
944
# File 'lib/logbrew.rb', line 942

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

#shutdown(transport = nil) ⇒ Object



989
990
991
992
993
994
995
996
997
998
999
1000
1001
1002
1003
1004
1005
1006
1007
1008
1009
1010
1011
1012
1013
1014
1015
1016
1017
1018
1019
1020
# File 'lib/logbrew.rb', line 989

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



958
959
960
# File 'lib/logbrew.rb', line 958

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

#stop_automatic_deliveryObject



919
920
921
922
# File 'lib/logbrew.rb', line 919

def stop_automatic_delivery
  controller = require_automatic_delivery
  controller.stop
end