Class: Azure::Loganalytics::Datacollectorapi::Client
- Inherits:
-
Object
- Object
- Azure::Loganalytics::Datacollectorapi::Client
- Defined in:
- lib/azure/loganalytics/datacollectorapi/client.rb
Constant Summary collapse
- DEFAUT_MAX_RETRIES =
3.freeze
- DEFAULT_RETRY_SLEEP_PERIOD =
5.freeze
Class Method Summary collapse
Instance Method Summary collapse
-
#initialize(customer_id, shared_key, endpoint = 'ods.opinsights.azure.com') ⇒ Client
constructor
A new instance of Client.
- #post_data(log_type, json_records, record_timestamp = '', azure_resource_id = '') ⇒ Object
- #set_default_azure_resoruce_id(azure_resource_id) ⇒ Object
- #set_proxy(proxy = '') ⇒ Object
- #set_retres(max_retries, retry_sleep_period) ⇒ Object
Constructor Details
#initialize(customer_id, shared_key, endpoint = 'ods.opinsights.azure.com') ⇒ Client
Returns a new instance of Client.
14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 |
# File 'lib/azure/loganalytics/datacollectorapi/client.rb', line 14 def initialize (customer_id, shared_key, endpoint ='ods.opinsights.azure.com') require 'rest-client' require 'json' require 'openssl' require 'base64' require 'time' @customer_id = customer_id @shared_key = shared_key @endpoint = endpoint @default_azure_resource_id = '' @max_retries = DEFAUT_MAX_RETRIES @retry_sleep_period = DEFAULT_RETRY_SLEEP_PERIOD end |
Class Method Details
.is_success(res) ⇒ Object
79 80 81 |
# File 'lib/azure/loganalytics/datacollectorapi/client.rb', line 79 def self.is_success(res) return (res.code == 200) ? true : false end |
Instance Method Details
#post_data(log_type, json_records, record_timestamp = '', azure_resource_id = '') ⇒ Object
30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 |
# File 'lib/azure/loganalytics/datacollectorapi/client.rb', line 30 def post_data(log_type, json_records, ='', azure_resource_id ='' ) raise ConfigError, 'no log_type' if log_type.empty? raise ConfigError, 'log_type must only contain alpha numeric and _, and not exceed 100 chars' if not is_valid_log_type(log_type) raise ConfigError, 'no json_records' if json_records.empty? body = json_records.to_json uri = sprintf("https://%s.%s/api/logs?api-version=%s", @customer_id, @endpoint, API_VERSION) date = rfc1123date() sig = signature(date, body.bytesize) headers = { 'Content-Type' => 'application/json', 'Authorization' => sig, 'Log-Type' => log_type, 'x-ms-date' => date, 'x-ms-AzureResourceId' => azure_resource_id.empty? ? @default_azure_resource_id : azure_resource_id, 'time-generated-field' => } retries = 0 begin res = RestClient.post( uri, body, headers) res rescue => e c = e.response.code.to_i if c == 429 || c == 500 || c==503 if retries < @max_retries retries += 1 sleep(@retry_sleep_period) retry end end raise e end end |
#set_default_azure_resoruce_id(azure_resource_id) ⇒ Object
70 71 72 |
# File 'lib/azure/loganalytics/datacollectorapi/client.rb', line 70 def set_default_azure_resoruce_id(azure_resource_id) @default_azure_resource_id = azure_resource_id end |
#set_proxy(proxy = '') ⇒ Object
66 67 68 |
# File 'lib/azure/loganalytics/datacollectorapi/client.rb', line 66 def set_proxy(proxy='') RestClient.proxy = proxy.empty? ? ENV['http_proxy'] : proxy end |
#set_retres(max_retries, retry_sleep_period) ⇒ Object
74 75 76 77 |
# File 'lib/azure/loganalytics/datacollectorapi/client.rb', line 74 def set_retres(max_retries, retry_sleep_period) @max_retries = max_retries @retry_sleep_period = retry_sleep_period end |