Skip to content

Commit

Permalink
version 0.4.0
Browse files Browse the repository at this point in the history
  • Loading branch information
yokawasa committed Jul 17, 2020
1 parent 07c91af commit 5136f27
Show file tree
Hide file tree
Showing 4 changed files with 49 additions and 3 deletions.
6 changes: 6 additions & 0 deletions ChangeLog.md
Original file line number Diff line number Diff line change
@@ -1,3 +1,9 @@
## 0.4.0
* restclient retries request on the following status code - [issue #10](https://github.com/yokawasa/azure-log-analytics-data-collector/issues/10)
* 429 - Too Many Requests
* 500 - Internal Server Error
* 503 - Service Unavailable

## 0.3.0
* Enhance log type validation: check not only alpha but also numeric, underscore, and character length (may not exceed 100) - [issue #11](https://github.com/yokawasa/azure-log-analytics-data-collector/issues/11)

Expand Down
16 changes: 16 additions & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,22 @@
[Azure Log Analytics Data Collector API](https://docs.microsoft.com/en-us/azure/azure-monitor/platform/data-collector-api) Client Libraries for Ruby. The repository was originally created for multiple programming languages, but it was refactored as a dedicated one for Ruby client. Python and PHP client libraries were moved to [azure-log-analytics-data-colloector-python](https://github.com/yokawasa/azure-log-analytics-data-collector-python) and [azure-log-analytics-data-colloector-php](https://github.com/yokawasa/azure-log-analytics-data-collector-php) respectively.


## Retry policy

The client internal leverage [rest-client] to send HTTP request to the API. The client library retries request using the rest-client on the following status code (which is [recommended action](https://docs.microsoft.com/en-us/azure/azure-monitor/platform/data-collector-api)).
* `429` - Too Many Requests
* `500` - Internal Server Error
* `503` - Service Unavailable

By default, the client library retres for a total of `3` times, sleeping `5 sec` between retries. The number of retries and sleeping time between retries can be changed with `set_retries` in the client class.

```ruby
def set_retries(max_retries, retry_sleep_period)
@max_retries = max_retries
@retry_sleep_period = retry_sleep_period
end
```

## Installation
```bash
gem install azure-loganalytics-datacollector-api
Expand Down
28 changes: 26 additions & 2 deletions lib/azure/loganalytics/datacollectorapi/client.rb
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,9 @@ module Datacollectorapi

class Client

DEFAUT_MAX_RETRIES = 3.freeze
DEFAULT_RETRY_SLEEP_PERIOD = 5.freeze

def initialize (customer_id, shared_key, endpoint ='ods.opinsights.azure.com')
require 'rest-client'
require 'json'
Expand All @@ -19,6 +22,9 @@ def initialize (customer_id, shared_key, endpoint ='ods.opinsights.azure.com')
@shared_key = shared_key
@endpoint = endpoint
@default_azure_resource_id = ''

@max_retries = DEFAUT_MAX_RETRIES
@retry_sleep_period = DEFAULT_RETRY_SLEEP_PERIOD
end

def post_data(log_type, json_records, record_timestamp ='', azure_resource_id ='' )
Expand All @@ -40,8 +46,21 @@ def post_data(log_type, json_records, record_timestamp ='', azure_resource_id ='
'time-generated-field' => record_timestamp
}

res = RestClient.post( uri, body, headers)
res
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

def set_proxy(proxy='')
Expand All @@ -51,6 +70,11 @@ def set_proxy(proxy='')
def set_default_azure_resoruce_id(azure_resource_id)
@default_azure_resource_id = azure_resource_id
end

def set_retres(max_retries, retry_sleep_period)
@max_retries = max_retries
@retry_sleep_period = retry_sleep_period
end

def self.is_success(res)
return (res.code == 200) ? true : false
Expand Down
2 changes: 1 addition & 1 deletion lib/azure/loganalytics/datacollectorapi/version.rb
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
module Azure
module Loganalytics
module Datacollectorapi
VERSION = "0.3.0"
VERSION = "0.4.0"
end
end
end

0 comments on commit 5136f27

Please sign in to comment.