From fe0c50d356127df7d2f66df475e67c1255ff24f3 Mon Sep 17 00:00:00 2001 From: Stefano Raggi Date: Wed, 11 Sep 2019 15:01:01 +0200 Subject: [PATCH] Add RetentionPolicy support The retention policy parameter has been added to allow writing to non-default retention policies. --- .../Configuration/CollectorEmitConfiguration.cs | 6 +++--- .../PipelinedCollectorEmitConfiguration.cs | 6 +++--- .../Client/LineProtocolClient.cs | 11 +++++++---- .../Client/LineProtocolClientBase.cs | 5 +++-- .../Client/LineProtocolUdpClient.cs | 5 +++-- .../Client/MockLineProtocolClient.cs | 2 +- 6 files changed, 20 insertions(+), 15 deletions(-) diff --git a/src/InfluxDB.Collector/Configuration/CollectorEmitConfiguration.cs b/src/InfluxDB.Collector/Configuration/CollectorEmitConfiguration.cs index 37546e3..5b557f2 100644 --- a/src/InfluxDB.Collector/Configuration/CollectorEmitConfiguration.cs +++ b/src/InfluxDB.Collector/Configuration/CollectorEmitConfiguration.cs @@ -5,11 +5,11 @@ namespace InfluxDB.Collector.Configuration { public abstract class CollectorEmitConfiguration { - public abstract CollectorConfiguration InfluxDB(Uri serverBaseAddress, string database, string username = null, string password = null); + public abstract CollectorConfiguration InfluxDB(Uri serverBaseAddress, string database, string username = null, string password = null, string retentionPolicy = null); - public CollectorConfiguration InfluxDB(string serverBaseAddress, string database, string username = null, string password = null) + public CollectorConfiguration InfluxDB(string serverBaseAddress, string database, string username = null, string password = null, string retentionPolicy = null) { - return InfluxDB(new Uri(serverBaseAddress), database, username, password); + return InfluxDB(new Uri(serverBaseAddress), database, username, password, retentionPolicy); } public abstract CollectorConfiguration Emitter(Action emitter); diff --git a/src/InfluxDB.Collector/Configuration/PipelinedCollectorEmitConfiguration.cs b/src/InfluxDB.Collector/Configuration/PipelinedCollectorEmitConfiguration.cs index 81666b1..640f60e 100644 --- a/src/InfluxDB.Collector/Configuration/PipelinedCollectorEmitConfiguration.cs +++ b/src/InfluxDB.Collector/Configuration/PipelinedCollectorEmitConfiguration.cs @@ -20,12 +20,12 @@ public PipelinedCollectorEmitConfiguration( _configuration = configuration; } - public override CollectorConfiguration InfluxDB(Uri serverBaseAddress, string database, string username = null, string password = null) + public override CollectorConfiguration InfluxDB(Uri serverBaseAddress, string database, string username = null, string password = null, string retentionPolicy = null) { if (string.Compare(serverBaseAddress.Scheme, "udp", ignoreCase: true) == 0) - _client = new LineProtocolUdpClient(serverBaseAddress, database, username, password); + _client = new LineProtocolUdpClient(serverBaseAddress, database, username, password, retentionPolicy); else - _client = new LineProtocolClient(serverBaseAddress, database, username, password); + _client = new LineProtocolClient(serverBaseAddress, database, username, password, retentionPolicy); return _configuration; } diff --git a/src/InfluxDB.LineProtocol/Client/LineProtocolClient.cs b/src/InfluxDB.LineProtocol/Client/LineProtocolClient.cs index 279962c..cb5bf8b 100644 --- a/src/InfluxDB.LineProtocol/Client/LineProtocolClient.cs +++ b/src/InfluxDB.LineProtocol/Client/LineProtocolClient.cs @@ -14,8 +14,8 @@ public class LineProtocolClient : LineProtocolClientBase { private readonly HttpClient _httpClient; - public LineProtocolClient(Uri serverBaseAddress, string database, string username = null, string password = null) - : this(new HttpClientHandler(), serverBaseAddress, database, username, password) + public LineProtocolClient(Uri serverBaseAddress, string database, string username = null, string password = null, string retentionPolicy = null) + : this(new HttpClientHandler(), serverBaseAddress, database, username, password, retentionPolicy) { } @@ -24,8 +24,9 @@ protected LineProtocolClient( Uri serverBaseAddress, string database, string username, - string password) - :base(serverBaseAddress, database, username, password) + string password, + string retentionPolicy) + :base(serverBaseAddress, database, username, password, retentionPolicy) { if (serverBaseAddress == null) throw new ArgumentNullException(nameof(serverBaseAddress)); @@ -42,6 +43,8 @@ protected override async Task OnSendAsync( CancellationToken cancellationToken = default(CancellationToken)) { var endpoint = $"write?db={Uri.EscapeDataString(_database)}"; + if (!string.IsNullOrWhiteSpace(_retentionPolicy)) + endpoint += $"&rp={Uri.EscapeDataString(_retentionPolicy)}"; if (!string.IsNullOrEmpty(_username)) endpoint += $"&u={Uri.EscapeDataString(_username)}&p={Uri.EscapeDataString(_password)}"; diff --git a/src/InfluxDB.LineProtocol/Client/LineProtocolClientBase.cs b/src/InfluxDB.LineProtocol/Client/LineProtocolClientBase.cs index d7bb5ca..2784128 100644 --- a/src/InfluxDB.LineProtocol/Client/LineProtocolClientBase.cs +++ b/src/InfluxDB.LineProtocol/Client/LineProtocolClientBase.cs @@ -12,9 +12,9 @@ namespace InfluxDB.LineProtocol.Client { public abstract class LineProtocolClientBase : ILineProtocolClient { - protected readonly string _database, _username, _password; + protected readonly string _database, _username, _password, _retentionPolicy; - protected LineProtocolClientBase(Uri serverBaseAddress, string database, string username, string password) + protected LineProtocolClientBase(Uri serverBaseAddress, string database, string username, string password, string retentionPolicy) { if (serverBaseAddress == null) throw new ArgumentNullException(nameof(serverBaseAddress)); @@ -25,6 +25,7 @@ protected LineProtocolClientBase(Uri serverBaseAddress, string database, string _database = database; _username = username; _password = password; + _retentionPolicy = retentionPolicy; } public Task WriteAsync(LineProtocolPayload payload, CancellationToken cancellationToken = default(CancellationToken)) diff --git a/src/InfluxDB.LineProtocol/Client/LineProtocolUdpClient.cs b/src/InfluxDB.LineProtocol/Client/LineProtocolUdpClient.cs index e76be29..ae0a619 100644 --- a/src/InfluxDB.LineProtocol/Client/LineProtocolUdpClient.cs +++ b/src/InfluxDB.LineProtocol/Client/LineProtocolUdpClient.cs @@ -20,8 +20,9 @@ public LineProtocolUdpClient( Uri serverBaseAddress, string database, string username = null, - string password = null) - :base(serverBaseAddress, database, username, password) + string password = null, + string retentionPolicy = null) + :base(serverBaseAddress, database, username, password, retentionPolicy) { if (serverBaseAddress == null) throw new ArgumentNullException(nameof(serverBaseAddress)); diff --git a/test/InfluxDB.LineProtocol.Tests/Client/MockLineProtocolClient.cs b/test/InfluxDB.LineProtocol.Tests/Client/MockLineProtocolClient.cs index 47cab05..e28eab3 100644 --- a/test/InfluxDB.LineProtocol.Tests/Client/MockLineProtocolClient.cs +++ b/test/InfluxDB.LineProtocol.Tests/Client/MockLineProtocolClient.cs @@ -11,7 +11,7 @@ public class MockLineProtocolClient : LineProtocolClient { } - private MockLineProtocolClient(MockHttpMessageHandler handler, Uri serverBaseAddress, string database) : base(handler, serverBaseAddress, database, null, null) + private MockLineProtocolClient(MockHttpMessageHandler handler, Uri serverBaseAddress, string database) : base(handler, serverBaseAddress, database, null, null, null) { Handler = handler; BaseAddress = serverBaseAddress;