Skip to content

Commit 653c476

Browse files
authored
Merge pull request #37 from vtex/feature/support_channel_into_request_head
Improve HEC token
2 parents 3970a63 + b3dcaa5 commit 653c476

File tree

5 files changed

+121
-44
lines changed

5 files changed

+121
-44
lines changed

src/SplunkLogger/Configurations/HECConfiguration.cs

Lines changed: 17 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -6,6 +6,13 @@ namespace Splunk.Configurations
66
/// </summary>
77
public class HECConfiguration
88
{
9+
public enum ChannelIdOption
10+
{
11+
None,
12+
QueryString,
13+
RequestHeader
14+
}
15+
916
/// <summary>
1017
/// Gets or sets the batch interval in miliseconds.
1118
/// </summary>
@@ -26,6 +33,16 @@ public class HECConfiguration
2633
/// </summary>
2734
public string SplunkCollectorUrl { get; set; }
2835

36+
/// <summary>
37+
/// Gets or sets indication to use or not hec token autentication at query string
38+
/// </summary>
39+
public bool UseAuthTokenAsQueryString { get; set; }
40+
41+
/// <summary>
42+
/// Gets or sets indication to use or not channel identification when using raw endpoint
43+
/// </summary>
44+
public ChannelIdOption ChannelIdType { get; set; } = ChannelIdOption.None;
45+
2946
/// <summary>
3047
/// Gets or sets the token.
3148
/// </summary>

src/VTEXSplunkLogger/LoggerFactoryExtensions.cs renamed to src/SplunkLogger/LoggerFactoryExtensions.cs

Lines changed: 1 addition & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,9 +1,8 @@
11
using Microsoft.Extensions.Logging;
22
using Splunk.Providers;
33
using Splunk.Configurations;
4-
using Splunk;
54

6-
namespace Vtex
5+
namespace Splunk
76
{
87
/// <summary>
98
/// This class contains ILoggerFactory extension method to simplify the process to add a Splunk logger provider.
Lines changed: 74 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,74 @@
1+
using System;
2+
using System.Net.Http;
3+
using System.Net.Http.Headers;
4+
using Microsoft.Extensions.Logging;
5+
using Splunk.Configurations;
6+
7+
namespace Splunk.Providers
8+
{
9+
/// <summary>
10+
/// Splunk HECB ase provider.
11+
/// </summary>
12+
public abstract class SplunkHECBaseProvider : ILoggerProvider
13+
{
14+
protected HttpClient httpClient;
15+
16+
/// <summary>
17+
/// Get a <see cref="T:Splunk.Loggers.HECJsonLogger"/> instance to the category name provided.
18+
/// </summary>
19+
/// <returns><see cref="T:Splunk.Loggers.HECJsonLogger"/> instance.</returns>
20+
/// <param name="categoryName">Category name.</param>
21+
public abstract ILogger CreateLogger(string categoryName);
22+
23+
/// <summary>
24+
/// Releases all resource used by the <see cref="T:Splunk.Providers.SplunkHECJsonLoggerProvider"/> object.
25+
/// </summary>
26+
/// <remarks>Call <see cref="Dispose"/> when you are finished using the
27+
/// <see cref="T:Splunk.Providers.SplunkHECJsonLoggerProvider"/>. The <see cref="Dispose"/> method leaves the
28+
/// <see cref="T:Splunk.Providers.SplunkHECJsonLoggerProvider"/> in an unusable state. After calling
29+
/// <see cref="Dispose"/>, you must release all references to the
30+
/// <see cref="T:Splunk.Providers.SplunkHECJsonLoggerProvider"/> so the garbage collector can reclaim the memory
31+
/// that the <see cref="T:Splunk.Providers.SplunkHECJsonLoggerProvider"/> was occupying.</remarks>
32+
public abstract void Dispose();
33+
34+
/// <summary>
35+
/// Create a <see cref="T:Splunk.Loggers.HECJsonLogger"/> instance to the category name provided.
36+
/// </summary>
37+
/// <returns>The logger instance.</returns>
38+
/// <param name="categoryName">Category name.</param>
39+
public abstract ILogger CreateLoggerInstance(string categoryName);
40+
41+
protected void SetupHttpClient(SplunkLoggerConfiguration configuration, string endPointCustomization)
42+
{
43+
httpClient = new HttpClient
44+
{
45+
BaseAddress = GetSplunkCollectorUrl(configuration, endPointCustomization),
46+
Timeout = TimeSpan.FromMilliseconds(configuration.HecConfiguration.DefaultTimeoutInMiliseconds)
47+
};
48+
httpClient.DefaultRequestHeaders.Authorization = new AuthenticationHeaderValue("Splunk", configuration.HecConfiguration.Token);
49+
if (configuration.HecConfiguration.ChannelIdType == HECConfiguration.ChannelIdOption.RequestHeader)
50+
httpClient.DefaultRequestHeaders.Add("x-splunk-request-channel", Guid.NewGuid().ToString());
51+
}
52+
53+
Uri GetSplunkCollectorUrl(SplunkLoggerConfiguration configuration, string endPointCustomization)
54+
{
55+
var splunkCollectorUrl = configuration.HecConfiguration.SplunkCollectorUrl;
56+
if (!splunkCollectorUrl.EndsWith("/", StringComparison.InvariantCulture))
57+
splunkCollectorUrl = splunkCollectorUrl + "/";
58+
59+
if(!string.IsNullOrWhiteSpace(endPointCustomization))
60+
splunkCollectorUrl = splunkCollectorUrl + endPointCustomization;
61+
62+
if (configuration.HecConfiguration.ChannelIdType == HECConfiguration.ChannelIdOption.QueryString)
63+
splunkCollectorUrl = splunkCollectorUrl + "?channel=" + Guid.NewGuid().ToString();
64+
65+
if(configuration.HecConfiguration.UseAuthTokenAsQueryString)
66+
{
67+
var tokenParameter = "token=" + configuration.HecConfiguration.Token;
68+
splunkCollectorUrl = string.Format("{0}{1}{2}", splunkCollectorUrl, splunkCollectorUrl.Contains("?") ? "&" : "?", tokenParameter);
69+
}
70+
71+
return new Uri(splunkCollectorUrl);
72+
}
73+
}
74+
}

src/SplunkLogger/Providers/SplunkHECJsonLoggerProvider.cs

Lines changed: 15 additions & 21 deletions
Original file line numberDiff line numberDiff line change
@@ -16,10 +16,9 @@ namespace Splunk.Providers
1616
/// <summary>
1717
/// This class is used to provide a Splunk HEC Json logger for each categoryName.
1818
/// </summary>
19-
public class SplunkHECJsonLoggerProvider : ILoggerProvider
19+
public class SplunkHECJsonLoggerProvider : SplunkHECBaseProvider, ILoggerProvider
2020
{
2121
readonly BatchManager batchController;
22-
readonly HttpClient httpClient;
2322
readonly LogLevel threshold;
2423
readonly ILoggerFormatter loggerFormatter;
2524
readonly ConcurrentDictionary<string, ILogger> loggers;
@@ -37,32 +36,17 @@ public SplunkHECJsonLoggerProvider(SplunkLoggerConfiguration configuration, ILog
3736

3837
this.loggerFormatter = loggerFormatter;
3938

40-
httpClient = new HttpClient();
41-
42-
var splunkCollectorUrl = configuration.HecConfiguration.SplunkCollectorUrl;
43-
if (!splunkCollectorUrl.EndsWith("/", StringComparison.InvariantCulture))
44-
splunkCollectorUrl += "/";
45-
46-
var baseAddress = new Uri(splunkCollectorUrl + "event");
47-
httpClient.BaseAddress = baseAddress;
48-
49-
httpClient.Timeout = TimeSpan.FromMilliseconds(configuration.HecConfiguration.DefaultTimeoutInMiliseconds);
50-
httpClient.DefaultRequestHeaders.Authorization = new AuthenticationHeaderValue("Splunk", configuration.HecConfiguration.Token);
39+
SetupHttpClient(configuration, "event");
5140

5241
batchController = new BatchManager(configuration.HecConfiguration.BatchSizeCount, configuration.HecConfiguration.BatchIntervalInMiliseconds, Emit);
5342
}
5443

55-
ILogger CreateLoggerInstance(string categoryName)
56-
{
57-
return new HECJsonLogger(categoryName, threshold, httpClient, batchController, loggerFormatter);
58-
}
59-
6044
/// <summary>
61-
/// Create a <see cref="T:Splunk.Loggers.HECJsonLogger"/> instance to the category name provided.
45+
/// Get a <see cref="T:Splunk.Loggers.HECJsonLogger"/> instance to the category name provided.
6246
/// </summary>
6347
/// <returns><see cref="T:Splunk.Loggers.HECJsonLogger"/> instance.</returns>
6448
/// <param name="categoryName">Category name.</param>
65-
public ILogger CreateLogger(string categoryName)
49+
public override ILogger CreateLogger(string categoryName)
6650
{
6751
return loggers.GetOrAdd(categoryName, CreateLoggerInstance(categoryName));
6852
}
@@ -76,11 +60,21 @@ public ILogger CreateLogger(string categoryName)
7660
/// <see cref="Dispose"/>, you must release all references to the
7761
/// <see cref="T:Splunk.Providers.SplunkHECJsonLoggerProvider"/> so the garbage collector can reclaim the memory
7862
/// that the <see cref="T:Splunk.Providers.SplunkHECJsonLoggerProvider"/> was occupying.</remarks>
79-
public void Dispose()
63+
public override void Dispose()
8064
{
8165
loggers.Clear();
8266
}
8367

68+
/// <summary>
69+
/// Create a <see cref="T:Splunk.Loggers.HECJsonLogger"/> instance to the category name provided.
70+
/// </summary>
71+
/// <returns>The logger instance.</returns>
72+
/// <param name="categoryName">Category name.</param>
73+
public override ILogger CreateLoggerInstance(string categoryName)
74+
{
75+
return new HECJsonLogger(categoryName, threshold, httpClient, batchController, loggerFormatter);
76+
}
77+
8478
/// <summary>
8579
/// Method used to emit batched events.
8680
/// </summary>

src/SplunkLogger/Providers/SplunkHECRawLoggerProvider.cs

Lines changed: 14 additions & 21 deletions
Original file line numberDiff line numberDiff line change
@@ -4,7 +4,6 @@
44
using System.Diagnostics;
55
using System.Linq;
66
using System.Net.Http;
7-
using System.Net.Http.Headers;
87
using Microsoft.Extensions.Logging;
98
using Splunk.Configurations;
109
using Splunk.Loggers;
@@ -14,10 +13,9 @@ namespace Splunk.Providers
1413
/// <summary>
1514
/// This class is used to provide a Splunk HEC Raw logger for each categoryName.
1615
/// </summary>
17-
public class SplunkHECRawLoggerProvider : ILoggerProvider
16+
public class SplunkHECRawLoggerProvider : SplunkHECBaseProvider, ILoggerProvider
1817
{
1918
readonly BatchManager batchManager;
20-
readonly HttpClient httpClient;
2119
readonly LogLevel threshold;
2220
readonly ILoggerFormatter loggerFormatter;
2321
readonly ConcurrentDictionary<string, ILogger> loggers;
@@ -35,32 +33,17 @@ public SplunkHECRawLoggerProvider(SplunkLoggerConfiguration configuration, ILogg
3533

3634
this.loggerFormatter = loggerFormatter;
3735

38-
httpClient = new HttpClient();
39-
40-
var splunkCollectorUrl = configuration.HecConfiguration.SplunkCollectorUrl;
41-
if (!splunkCollectorUrl.EndsWith("/", StringComparison.InvariantCulture))
42-
splunkCollectorUrl += "/";
43-
44-
var baseAddress = new Uri(splunkCollectorUrl + "raw?channel=" + Guid.NewGuid().ToString());
45-
httpClient.BaseAddress = baseAddress;
46-
47-
httpClient.Timeout = TimeSpan.FromMilliseconds(configuration.HecConfiguration.DefaultTimeoutInMiliseconds);
48-
httpClient.DefaultRequestHeaders.Authorization = new AuthenticationHeaderValue("Splunk", configuration.HecConfiguration.Token);
36+
SetupHttpClient(configuration, "raw");
4937

5038
batchManager = new BatchManager(configuration.HecConfiguration.BatchSizeCount, configuration.HecConfiguration.BatchIntervalInMiliseconds, Emit);
5139
}
5240

53-
ILogger CreateLoggerInstance(string categoryName)
54-
{
55-
return new HECRawLogger(categoryName, threshold, httpClient, batchManager, loggerFormatter);
56-
}
57-
5841
/// <summary>
5942
/// Create a <see cref="T:Splunk.Loggers.HECRawLogger"/> instance to the category name provided.
6043
/// </summary>
6144
/// <returns><see cref="T:Splunk.Loggers.HECRawLogger"/> instance.</returns>
6245
/// <param name="categoryName">Category name.</param>
63-
public ILogger CreateLogger(string categoryName)
46+
public override ILogger CreateLogger(string categoryName)
6447
{
6548
return loggers.GetOrAdd(categoryName, CreateLoggerInstance(categoryName));
6649
}
@@ -74,11 +57,21 @@ public ILogger CreateLogger(string categoryName)
7457
/// <see cref="Dispose"/>, you must release all references to the
7558
/// <see cref="T:Splunk.Providers.SplunkHECRawLoggerProvider"/> so the garbage collector can reclaim the memory
7659
/// that the <see cref="T:Splunk.Providers.SplunkHECRawLoggerProvider"/> was occupying.</remarks>
77-
public void Dispose()
60+
public override void Dispose()
7861
{
7962
loggers.Clear();
8063
}
8164

65+
/// <summary>
66+
/// Create a <see cref="T:Splunk.Loggers.HECRawLogger"/> instance to the category name provided.
67+
/// </summary>
68+
/// <returns><see cref="T:Splunk.Loggers.HECRawLogger"/> instance.</returns>
69+
/// <param name="categoryName">Category name.</param>
70+
public override ILogger CreateLoggerInstance(string categoryName)
71+
{
72+
return new HECRawLogger(categoryName, threshold, httpClient, batchManager, loggerFormatter);
73+
}
74+
8275
/// <summary>
8376
/// Method used to emit batched events.
8477
/// </summary>

0 commit comments

Comments
 (0)