diff --git a/InfluxDB.sln b/InfluxDB.sln index aa9806f..290c390 100644 --- a/InfluxDB.sln +++ b/InfluxDB.sln @@ -1,9 +1,12 @@  Microsoft Visual Studio Solution File, Format Version 12.00 # Visual Studio 15 -VisualStudioVersion = 15.0.26730.3 +VisualStudioVersion = 15.0.27130.2027 MinimumVisualStudioVersion = 10.0.40219.1 Project("{2150E333-8FDC-42A3-9474-1A3956D46DE8}") = "src", "src", "{2D805782-756E-4C98-B22E-F502BEE95318}" + ProjectSection(SolutionItems) = preProject + src\Directory.Build.props = src\Directory.Build.props + EndProjectSection EndProject Project("{2150E333-8FDC-42A3-9474-1A3956D46DE8}") = "Solution Items", "Solution Items", "{31A4DDB1-952E-4EED-96EF-29C669279A86}" ProjectSection(SolutionItems) = preProject diff --git a/build/dependencies.props b/build/dependencies.props index bab4ad6..f7772bc 100644 --- a/build/dependencies.props +++ b/build/dependencies.props @@ -2,18 +2,20 @@ 2.0.0-* 2.0.0-* - 2.0.0-* - 0.10.8 - 4.4.0-* - 2.0.0-* - 4.7.49 - 10.0.2 + 2.0.1 + 2.0.2 + 2.0.0 + 0.10.12 + 4.4.0 + 2.0.4 + 4.8.1 + 10.0.3 2.0.0-* 2.0.0-* - 15.3.0-* - 2.3.0-beta2-* - 4.19.2 - 4.19.0 + 15.5.0 + 2.3.1 + 5.0.0 + 5.0.0 1.0.0 diff --git a/sandbox/MetricsInfluxDBSandbox/MetricsInfluxDBSandbox.csproj b/sandbox/MetricsInfluxDBSandbox/MetricsInfluxDBSandbox.csproj index 7bb679b..1073db1 100644 --- a/sandbox/MetricsInfluxDBSandbox/MetricsInfluxDBSandbox.csproj +++ b/sandbox/MetricsInfluxDBSandbox/MetricsInfluxDBSandbox.csproj @@ -14,9 +14,9 @@ - + - + diff --git a/sandbox/MetricsInfluxDBSandboxMvc/MetricsInfluxDBSandboxMvc.csproj b/sandbox/MetricsInfluxDBSandboxMvc/MetricsInfluxDBSandboxMvc.csproj index e168de6..80ddc43 100644 --- a/sandbox/MetricsInfluxDBSandboxMvc/MetricsInfluxDBSandboxMvc.csproj +++ b/sandbox/MetricsInfluxDBSandboxMvc/MetricsInfluxDBSandboxMvc.csproj @@ -10,11 +10,11 @@ - - + + - - + + @@ -28,12 +28,7 @@ - - - - - - + diff --git a/sandbox/MetricsInfluxDBSandboxMvc/Startup.cs b/sandbox/MetricsInfluxDBSandboxMvc/Startup.cs index 0125f92..253b42e 100644 --- a/sandbox/MetricsInfluxDBSandboxMvc/Startup.cs +++ b/sandbox/MetricsInfluxDBSandboxMvc/Startup.cs @@ -29,7 +29,7 @@ public void ConfigureServices(IServiceCollection services) { services.AddTestStuff(); - services.AddMvc(options => options.AddMetricsResourceFilter()); + services.AddMvc().AddMetrics(); } } } \ No newline at end of file diff --git a/src/App.Metrics.Reporting.InfluxDB/Client/DefaultLineProtocolClient.cs b/src/App.Metrics.Reporting.InfluxDB/Client/DefaultLineProtocolClient.cs index dc8768f..f4df62e 100644 --- a/src/App.Metrics.Reporting.InfluxDB/Client/DefaultLineProtocolClient.cs +++ b/src/App.Metrics.Reporting.InfluxDB/Client/DefaultLineProtocolClient.cs @@ -3,6 +3,7 @@ // using System; +using System.Net; using System.Net.Http; using System.Text; using System.Threading; @@ -55,6 +56,13 @@ public async Task WriteAsync( var response = await _httpClient.PostAsync(_influxDbOptions.Endpoint, content, cancellationToken); + if (response.StatusCode == HttpStatusCode.NotFound && _influxDbOptions.CreateDataBaseIfNotExists) + { + await TryCreateDatabase(cancellationToken); + + response = await _httpClient.PostAsync(_influxDbOptions.Endpoint, content, cancellationToken); + } + if (!response.IsSuccessStatusCode) { Interlocked.Increment(ref _failureAttempts); @@ -77,6 +85,35 @@ public async Task WriteAsync( } } + private async Task TryCreateDatabase(CancellationToken cancellationToken = default) + { + try + { + Logger.Trace($"Attempting to create InfluxDB Database '{_influxDbOptions.Database}'"); + + var content = new StringContent(string.Empty, Encoding.UTF8); + + var response = await _httpClient.PostAsync($"query?q=CREATE DATABASE \"{Uri.EscapeDataString(_influxDbOptions.Database)}\"", content, cancellationToken); + + if (!response.IsSuccessStatusCode) + { + var errorMessage = $"Failed to create InfluxDB Database '{_influxDbOptions.Database}' - StatusCode: {response.StatusCode} Reason: {response.ReasonPhrase}"; + Logger.Error(errorMessage); + + return new LineProtocolWriteResult(false, errorMessage); + } + + Logger.Trace($"Successfully created InfluxDB Database '{_influxDbOptions.Database}'"); + + return new LineProtocolWriteResult(true); + } + catch (Exception ex) + { + Logger.Error(ex, $"Failed to create InfluxDB Database'{_influxDbOptions.Database}'"); + return new LineProtocolWriteResult(false, ex.ToString()); + } + } + private bool NeedToBackoff() { if (Interlocked.Read(ref _failureAttempts) < _failuresBeforeBackoff) diff --git a/src/App.Metrics.Reporting.InfluxDB/InfluxDBOptions.cs b/src/App.Metrics.Reporting.InfluxDB/InfluxDBOptions.cs index 4f11a82..6658f45 100644 --- a/src/App.Metrics.Reporting.InfluxDB/InfluxDBOptions.cs +++ b/src/App.Metrics.Reporting.InfluxDB/InfluxDBOptions.cs @@ -11,6 +11,8 @@ namespace App.Metrics.Reporting.InfluxDB /// public class InfluxDbOptions { + public InfluxDbOptions() { CreateDataBaseIfNotExists = true; } + /// /// Gets or sets the number of InfluxDB notes that must confirm the write /// @@ -89,5 +91,13 @@ public string Endpoint /// The InfluxDB database username. /// public string UserName { get; set; } + + /// + /// Gets or sets a value indicating whether or not to attempt to create the specified database if it does not exist + /// + /// + /// The flag indicating whether or not to create the specifried database if it does not exist + /// + public bool CreateDataBaseIfNotExists { get; set; } } } \ No newline at end of file diff --git a/src/Directory.Build.props b/src/Directory.Build.props index ad660fa..1a172c2 100644 --- a/src/Directory.Build.props +++ b/src/Directory.Build.props @@ -1,5 +1,5 @@ - + \ No newline at end of file diff --git a/test/App.Metrics.Reporting.InfluxDB.Facts/App.Metrics.Reporting.InfluxDB.Facts.csproj b/test/App.Metrics.Reporting.InfluxDB.Facts/App.Metrics.Reporting.InfluxDB.Facts.csproj index a0e3d9e..01ea781 100644 --- a/test/App.Metrics.Reporting.InfluxDB.Facts/App.Metrics.Reporting.InfluxDB.Facts.csproj +++ b/test/App.Metrics.Reporting.InfluxDB.Facts/App.Metrics.Reporting.InfluxDB.Facts.csproj @@ -13,7 +13,7 @@ - + diff --git a/test/App.Metrics.Reporting.InfluxDB.Facts/DefaultLineProtocolClientTests.cs b/test/App.Metrics.Reporting.InfluxDB.Facts/DefaultLineProtocolClientTests.cs index a5192f4..df49f97 100644 --- a/test/App.Metrics.Reporting.InfluxDB.Facts/DefaultLineProtocolClientTests.cs +++ b/test/App.Metrics.Reporting.InfluxDB.Facts/DefaultLineProtocolClientTests.cs @@ -90,7 +90,7 @@ public void Http_policy_is_required() }; // Assert - action.ShouldThrow(); + action.Should().Throw(); } [Fact] @@ -104,7 +104,7 @@ public void Influxdb_settings_are_required() }; // Assert - action.ShouldThrow(); + action.Should().Throw(); } [Fact] diff --git a/test/App.Metrics.Reporting.InfluxDB.Facts/LineProtocolPointTests.cs b/test/App.Metrics.Reporting.InfluxDB.Facts/LineProtocolPointTests.cs index 85bda5e..6e7eecb 100644 --- a/test/App.Metrics.Reporting.InfluxDB.Facts/LineProtocolPointTests.cs +++ b/test/App.Metrics.Reporting.InfluxDB.Facts/LineProtocolPointTests.cs @@ -22,7 +22,7 @@ public void At_least_one_field_is_required() var unused = new LineProtocolPoint("measurement", fields, MetricTags.Empty); }; - action.ShouldThrow(); + action.Should().Throw(); } [Fact] @@ -91,7 +91,7 @@ public void Field_key_cannot_be_empty() var unused = new LineProtocolPoint("measurement", fields, MetricTags.Empty); }; - action.ShouldThrow(); + action.Should().Throw(); } [Fact] @@ -103,7 +103,7 @@ public void Measurement_is_required() var unused = new LineProtocolPoint(string.Empty, fields, MetricTags.Empty); }; - action.ShouldThrow(); + action.Should().Throw(); } [Theory] @@ -122,11 +122,11 @@ public void Time_stamp_should_be_utc(DateTimeKind dateTimeKind, bool expected) if (!expected) { - action.ShouldThrow(); + action.Should().Throw(); } else { - action.ShouldNotThrow(); + action.Should().NotThrow(); } } }