Class: LogStash::Outputs::AzureEventHubs

Inherits:
Base
  • Object
show all
Defined in:
lib/logstash/outputs/azure_event_hub.rb

Overview

Output plugin to send events to an Azure Event Hub.

Instance Method Summary collapse

Instance Method Details

#closeObject



125
126
127
128
129
130
# File 'lib/logstash/outputs/azure_event_hub.rb', line 125

def close
  if (!@eventhub_client.nil?)
    @eventhub_client.closeSync();
  end
  @executor_service.shutdown();
end

#receive(event) ⇒ Object



133
134
135
136
137
138
139
# File 'lib/logstash/outputs/azure_event_hub.rb', line 133

def receive(event)
  begin
    @codec.encode(event)
  rescue => e
    @logger.warn("Error encoding event", :exception => e, :event => event)
  end
end

#registerObject



66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
# File 'lib/logstash/outputs/azure_event_hub.rb', line 66

def register

  # The Executor handles all the asynchronous tasks and this is passed to the EventHubClient.

  # The gives the user control to segregate their thread pool based on the work load.

  # This pool can then be shared across multiple EventHubClient instances.

  @executor_service = Executors.newScheduledThreadPool(@client_threads)

  # Handle Transient errors when creating the Event Hubs Client

  try = 0
  retry_interval = 2
  begin
    # Each EventHubClient instance spins up a new TCP/SSL connection, which is expensive.

    # It is always a best practice to reuse these instances.

    if !@connection_string.nil? and @connection_string !~ /\A\s*\Z/
      @eventhub_client = EventHubClient.createSync(@connection_string, @executor_service)
      @codec.on_event(&method(:send_record))
    else
      @logger.warn("Connection String is empty, azure_event_hub output will be ignored and should not be called...")
      @codec.on_event(&method(:log_no_msg_sent))
    end

  rescue IllegalConnectionStringFormatException, IOException => e
    @logger.error(
      "Unable to establish connection to Azure Event Hubs.",
      :error_message => e.getMessage(),
      :class => e.class.name
      )
    close()
    exit(1)
  rescue EventHubException, ExecutionException => e
    # Log error, no retry

    if (e.is_a?(EventHubException) and e.getIsTransient() != true) or try >= @connection_retry_count
      @logger.error(
        "Unable to establish connection to Azure Event Hubs.",
        :error_message => e.getMessage(),
        :class => e.class.name
        )
      close()
      exit(1)
    end

    # Log error with retry

    @logger.error(
      "Connection to Event Hubs failed, will attempt connection again.",
      :error_message => e.getMessage(),
      :class => e.class.name,
      :retry_in_seconds => retry_interval
    )

    # Wait for interval

    sleep(retry_interval)
    
    # Add attempt and retry

    try += 1
    retry
  end
end