/// <summary> /// Initializes an instance of <see cref="SplunkHttpCollectorClient"/> with the specified <see cref="SplunkHttpCollectorClientOptions"/> /// and optional <see cref="RetryPolicy"/>. /// </summary> /// <param name="options"></param> /// <param name="retryPolicy"></param> /// <param name="fallback">Action to perform if Splunk is unavailable.</param> private SplunkHttpCollectorClient(SplunkHttpCollectorClientOptions options, RetryPolicy retryPolicy = null, Action <object> fallback = null) { this.options = options; this.fallback = fallback; HttpClientHandler httpClientHandler = new HttpClientHandler { //Decompress the response AutomaticDecompression = DecompressionMethods.GZip | DecompressionMethods.Deflate }; //Failure Injection Testing chaosHandler = new SplunkFitDelegatingHandler(httpClientHandler); //Transient Fault Handling RetryDelegatingHandler retryHandler = new RetryDelegatingHandler(chaosHandler); if (retryPolicy != null) { retryHandler.RetryPolicy = retryPolicy; } retryHandler.RetryPolicy.Retrying += OnTransientRetry; //Compress requests CompressionDelegatingHandler commpressionHandler = new CompressionDelegatingHandler(retryHandler); httpClient = new HttpClient(commpressionHandler); httpClient.DefaultRequestHeaders.Accept.Add(new MediaTypeWithQualityHeaderValue("application/json")); httpClient.DefaultRequestHeaders.AcceptEncoding.Add(new StringWithQualityHeaderValue("gzip")); httpClient.DefaultRequestHeaders.Authorization = new AuthenticationHeaderValue("Splunk", options.ApiKey); httpClient.Timeout = options.Timeout; }
/// <summary> /// Creates or returns an existing instance of <see cref="SplunkHttpCollectorClient"/>. /// </summary> /// <remarks> /// If an instance already exists, the new options passed in will be used for that instance. This allows /// applications to change options like the CircuitBreakerRetryWindow and Timeout on the fly. /// However, changing the Key or Url will have no impact. /// </remarks> /// <param name="options"></param> /// <param name="retryPolicy"></param> /// <param name="fallback">Action to perform if Splunk is unavailable.</param> /// <returns>A singleton instance of <see cref="SplunkHttpCollectorClient"/></returns> public static SplunkHttpCollectorClient GetInstance(SplunkHttpCollectorClientOptions options, RetryPolicy retryPolicy = null, Action <object> fallback = null) { if (_instance == null) { _instance = new SplunkHttpCollectorClient(options, retryPolicy, fallback); } else { _instance.options = options; _instance.httpClient.Timeout = options.Timeout; } return(_instance); }