Module: Util::LogAnalytics
- Extended by:
- Logging
- Defined in:
- lib/util/log_analytics.rb
Constant Summary collapse
- CLIENT_INFO =
Client information for raw request signing
'Oracle-ORD/Raw Request'.freeze
- CLIENT_AGENT =
'Oracle-ORD/Raw Request (net/http)'.freeze
- LOG_ANALYTICS_ENDPOINT_TEMPLATE =
'https://loganalytics.{region}.oci.{secondLevelDomain}'.freeze
- RETRYABLE_STATUS_CODE =
Status codes for which retry is attempted (To represent 5xx, use 5)
[408, 429, 5].freeze
- MAX_ATTEMPTS =
5
- INITIAL_DELAY =
seconds
1
- EXPONENT_CONSTANT =
āeā in exponential function (e^x) where x is number of retries
2
Constants included from Logging
Util::Logging::SEV_LABEL, Util::Logging::TRACE
Class Method Summary collapse
- .upload_discovery_data(namespace, payload, opts) ⇒ Object
- .upload_discovery_data_raw_request(namespace, payload, opts, auth_config) ⇒ Object
Methods included from Logging
Class Method Details
.upload_discovery_data(namespace, payload, opts) ⇒ Object
28 29 30 31 32 33 34 35 36 37 38 39 40 |
# File 'lib/util/log_analytics.rb', line 28 def upload_discovery_data(namespace, payload, opts) logger.info("Uploading payload to 'Log Analytics' with Discovery API") begin client = Util::OCIClients.get_clients[:la_client] response = client.upload_discovery_data(namespace_name = namespace, upload_discovery_data_details = payload, opts) rescue StandardError => e logger.error("Error while uploading payload to 'Log Analytics': #{e}") raise StandardError, "Unable to upload data to 'Log Analytics' client." end response end |
.upload_discovery_data_raw_request(namespace, payload, opts, auth_config) ⇒ Object
42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 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 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 |
# File 'lib/util/log_analytics.rb', line 42 def upload_discovery_data_raw_request(namespace, payload, opts, auth_config) logger.info('Uploading payload to \'Log Analytics\' with Discovery API (raw request)') # Prepare config and signer signer = nil operation_signing_strategy = :exclude_body case auth_config[:auth_type] when Enum::AuthTypeEnum::CONFIG config = auth_config[:oci_config] signer = OCI::Signer.config_file_auth_builder(config) region = config.region when Enum::AuthTypeEnum::INSTANCE_PRINCIPAL signer = auth_config[:instance_principals_signer] region = signer.region else logger.warn("Unknown auth_type provided for Discovery API (raw request): #{auth_object[:auth_type]}") raise StandardError 'Unknown auth_type for Discovery API (raw request)' end # Construct URL protocol, subdomain, domain and port endpoint = if !auth_config[:endpoint].nil? auth_config[:endpoint] + '/20200601' else OCI::Regions.get_service_endpoint_for_template(region, LOG_ANALYTICS_ENDPOINT_TEMPLATE) + '/20200601' end # Construct URL path path = '/namespaces/{namespaceName}/actions/uploadDiscoveryData'.sub('{namespaceName}', namespace.to_s) # Construct Query Params query_params = '?discoveryDataType={discoveryDataType}&payloadType={payloadType}' .sub('{discoveryDataType}', opts[:discovery_data_type]) .sub('{payloadType}', opts[:payload_type]) # Prepare URI url = endpoint + path + query_params uri = URI(url) # Set user agent and OPC parameters for request http_method = :post request = Net::HTTP::Post.new(uri) request['opc-client-info'] = CLIENT_INFO request['opc-request-id'] = SecureRandom.uuid.delete!('-').upcase request['User-Agent'] = CLIENT_AGENT # Header Params header_params = {} header_params = opts[:additional_headers].clone if opts[:additional_headers].is_a?(Hash) header_params[:accept] = 'application/json' header_params[:expect] = '100-continue' header_params[:'content-type'] = 'application/octet-stream' header_params[:'opc-meta-properties'] = opts[:opc_meta_properties] if opts[:opc_meta_properties] header_params[:'opc-retry-token'] = OCI::Retry.generate_opc_retry_token # Set the payload if payload.respond_to?(:read) && payload.respond_to?(:write) request.body_stream = payload else request.body = payload end # Sign the request signer.sign(http_method, url, header_params, payload, operation_signing_strategy) # Add header parameters header_params.each do |key, value| request[key.to_s] = value end # Set HTTP parameters http = Net::HTTP.new(uri.hostname, uri.port) http.use_ssl = (uri.scheme == 'https') http.open_timeout = 10 http.read_timeout = 180 # 180 seconds = 3 minutes http.continue_timeout = 3 # expect 100 continue timeout # Fetch response retry_count = 0 response = nil begin http.start do http.request(request) do |resp| response = resp end end rescue StandardError => e code_from_response = !response.nil? ? response.code.to_i : 0 if (RETRYABLE_STATUS_CODE.include? code_from_response) || RETRYABLE_STATUS_CODE.include?(code_from_response / 100) || e.instance_of?(Net::ReadTimeout) if retry_count < MAX_ATTEMPTS # If e = 2, and initial delay is 10, intervals will be (1, 2, 4, 8, 16) sleep_interval = EXPONENT_CONSTANT.pow(retry_count) * INITIAL_DELAY logger.error("Error while uploading data to discovery API (raw request). Retrying in #{sleep_interval} seconds. HTTP status code: #{code_from_response}") sleep(sleep_interval) retry_count += 1 retry end end raise OCI::Errors::NetworkError.new(e., code_from_response, request_made: request, response_received: response) end if response.is_a?(Net::HTTPSuccess) return OCI::Response.new(response.code.to_i, response.header, nil) end # Success safe check raise "Response from Discovery API (raw request) suggests failure. Response code: + #{response.code}" end |