Skip to content
This repository has been archived by the owner on Mar 26, 2019. It is now read-only.

Commit

Permalink
Merge pull request #29 from AppMetrics/dev
Browse files Browse the repository at this point in the history
2.0.0
  • Loading branch information
alhardy authored Mar 29, 2018
2 parents 14163a4 + a0364f9 commit ad0232e
Show file tree
Hide file tree
Showing 11 changed files with 80 additions and 33 deletions.
5 changes: 4 additions & 1 deletion InfluxDB.sln
Original file line number Diff line number Diff line change
@@ -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
Expand Down
22 changes: 12 additions & 10 deletions build/dependencies.props
Original file line number Diff line number Diff line change
Expand Up @@ -2,18 +2,20 @@
<PropertyGroup>
<AppMetricsCoreVersion>2.0.0-*</AppMetricsCoreVersion>
<AppMetricsAspNetCoreVersion>2.0.0-*</AppMetricsAspNetCoreVersion>
<AspNetCoreVersion>2.0.0-*</AspNetCoreVersion>
<BenchmarkDotNetVersion>0.10.8</BenchmarkDotNetVersion>
<CoreFxVersion>4.4.0-*</CoreFxVersion>
<DependencyModelVersion>2.0.0-*</DependencyModelVersion>
<MoqVersion>4.7.49</MoqVersion>
<NewtonsoftVersion>10.0.2</NewtonsoftVersion>
<AspNetCoreVersion>2.0.1</AspNetCoreVersion>
<AspNetCoreMvcVersion>2.0.2</AspNetCoreMvcVersion>
<MicrosoftExtensionsVersion>2.0.0</MicrosoftExtensionsVersion>
<BenchmarkDotNetVersion>0.10.12</BenchmarkDotNetVersion>
<CoreFxVersion>4.4.0</CoreFxVersion>
<DependencyModelVersion>2.0.4</DependencyModelVersion>
<MoqVersion>4.8.1</MoqVersion>
<NewtonsoftVersion>10.0.3</NewtonsoftVersion>
<NETStandardLibraryNETFrameworkVersion>2.0.0-*</NETStandardLibraryNETFrameworkVersion>
<RuntimeFrameworkVersion Condition="'$(TargetFramework)'=='netcoreapp2.0'">2.0.0-*</RuntimeFrameworkVersion>
<TestSdkVersion>15.3.0-*</TestSdkVersion>
<XunitVersion>2.3.0-beta2-*</XunitVersion>
<FluentAssertionsVersion>4.19.2</FluentAssertionsVersion>
<FluentAssertionsJsonVersion>4.19.0</FluentAssertionsJsonVersion>
<TestSdkVersion>15.5.0</TestSdkVersion>
<XunitVersion>2.3.1</XunitVersion>
<FluentAssertionsVersion>5.0.0</FluentAssertionsVersion>
<FluentAssertionsJsonVersion>5.0.0</FluentAssertionsJsonVersion>
<StyleCopAnalyzersVersion>1.0.0</StyleCopAnalyzersVersion>
</PropertyGroup>
</Project>
4 changes: 2 additions & 2 deletions sandbox/MetricsInfluxDBSandbox/MetricsInfluxDBSandbox.csproj
Original file line number Diff line number Diff line change
Expand Up @@ -14,9 +14,9 @@

<ItemGroup>
<PackageReference Include="App.Metrics" Version="$(AppMetricsCoreVersion)" />
<PackageReference Include="Microsoft.Extensions.Configuration.Json" Version="$(AspNetCoreVersion)" />
<PackageReference Include="Microsoft.Extensions.Configuration.Json" Version="$(MicrosoftExtensionsVersion)" />
<PackageReference Include="Serilog.Sinks.Literate" Version="3.0.0" />
<PackageReference Include="Serilog.Sinks.Seq" Version="3.3.3" />
<PackageReference Include="Serilog.Sinks.Seq" Version="4.0.0" />
</ItemGroup>

<ItemGroup>
Expand Down
15 changes: 5 additions & 10 deletions sandbox/MetricsInfluxDBSandboxMvc/MetricsInfluxDBSandboxMvc.csproj
Original file line number Diff line number Diff line change
Expand Up @@ -10,11 +10,11 @@
<ItemGroup>
<PackageReference Include="App.Metrics.AspNetCore.Mvc" Version="$(AppMetricsAspNetCoreVersion)" />
<PackageReference Include="Microsoft.AspNetCore" Version="$(AspNetCoreVersion)" />
<PackageReference Include="Microsoft.AspNetCore.Mvc" Version="$(AspNetCoreVersion)" />
<PackageReference Include="Microsoft.Extensions.Logging.Debug" Version="$(AspNetCoreVersion)" />
<PackageReference Include="Microsoft.AspNetCore.Mvc" Version="$(AspNetCoreMvcVersion)" />
<PackageReference Include="Microsoft.Extensions.Logging.Debug" Version="$(MicrosoftExtensionsVersion)" />
<PackageReference Include="Serilog.Sinks.Literate" Version="3.0.0" />
<PackageReference Include="Serilog.Sinks.Seq" Version="3.3.3" />
<PackageReference Include="Serilog.AspNetCore " Version="2.0.0" />
<PackageReference Include="Serilog.Sinks.Seq" Version="4.0.0" />
<PackageReference Include="Serilog.AspNetCore " Version="2.1.0" />
</ItemGroup>

<ItemGroup>
Expand All @@ -28,12 +28,7 @@
</ItemGroup>

<ItemGroup>
<DotNetCliToolReference Include="Microsoft.DotNet.Watcher.Tools" Version="$(AspNetCoreVersion)" />
</ItemGroup>

<ItemGroup>
<Folder Include="Controllers\" />
<Folder Include="JustForTesting\" />
<DotNetCliToolReference Include="Microsoft.DotNet.Watcher.Tools" Version="2.0.0" />
</ItemGroup>

</Project>
2 changes: 1 addition & 1 deletion sandbox/MetricsInfluxDBSandboxMvc/Startup.cs
Original file line number Diff line number Diff line change
Expand Up @@ -29,7 +29,7 @@ public void ConfigureServices(IServiceCollection services)
{
services.AddTestStuff();

services.AddMvc(options => options.AddMetricsResourceFilter());
services.AddMvc().AddMetrics();
}
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@
// </copyright>

using System;
using System.Net;
using System.Net.Http;
using System.Text;
using System.Threading;
Expand Down Expand Up @@ -55,6 +56,13 @@ public async Task<LineProtocolWriteResult> 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);
Expand All @@ -77,6 +85,35 @@ public async Task<LineProtocolWriteResult> WriteAsync(
}
}

private async Task<LineProtocolWriteResult> 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)
Expand Down
10 changes: 10 additions & 0 deletions src/App.Metrics.Reporting.InfluxDB/InfluxDBOptions.cs
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,8 @@ namespace App.Metrics.Reporting.InfluxDB
/// </summary>
public class InfluxDbOptions
{
public InfluxDbOptions() { CreateDataBaseIfNotExists = true; }

/// <summary>
/// Gets or sets the number of InfluxDB notes that must confirm the write
/// </summary>
Expand Down Expand Up @@ -89,5 +91,13 @@ public string Endpoint
/// The InfluxDB database username.
/// </value>
public string UserName { get; set; }

/// <summary>
/// Gets or sets a value indicating whether or not to attempt to create the specified database if it does not exist
/// </summary>
/// <value>
/// The flag indicating whether or not to create the specifried database if it does not exist
/// </value>
public bool CreateDataBaseIfNotExists { get; set; }
}
}
2 changes: 1 addition & 1 deletion src/Directory.Build.props
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
<Project>
<ItemGroup>
<PackageReference Include="SourceLink.Create.CommandLine" Version="2.4.0" PrivateAssets="All" />
<PackageReference Include="SourceLink.Create.CommandLine" Version="2.8.0" PrivateAssets="All" />
</ItemGroup>
</Project>
Original file line number Diff line number Diff line change
Expand Up @@ -13,7 +13,7 @@

<ItemGroup>
<PackageReference Include="Microsoft.NET.Test.Sdk" Version="$(TestSdkVersion)" />
<PackageReference Include="Microsoft.Extensions.DependencyInjection" Version="$(AspNetCoreVersion)" />
<PackageReference Include="Microsoft.Extensions.DependencyInjection" Version="$(MicrosoftExtensionsVersion)" />
<PackageReference Include="FluentAssertions" Version="$(FluentAssertionsVersion)" />
<PackageReference Include="Moq" Version="$(MoqVersion)" />
<PackageReference Include="xunit" Version="$(XunitVersion)" />
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -90,7 +90,7 @@ public void Http_policy_is_required()
};

// Assert
action.ShouldThrow<ArgumentNullException>();
action.Should().Throw<ArgumentNullException>();
}

[Fact]
Expand All @@ -104,7 +104,7 @@ public void Influxdb_settings_are_required()
};

// Assert
action.ShouldThrow<ArgumentNullException>();
action.Should().Throw<ArgumentNullException>();
}

[Fact]
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -22,7 +22,7 @@ public void At_least_one_field_is_required()
var unused = new LineProtocolPoint("measurement", fields, MetricTags.Empty);
};

action.ShouldThrow<ArgumentException>();
action.Should().Throw<ArgumentException>();
}

[Fact]
Expand Down Expand Up @@ -91,7 +91,7 @@ public void Field_key_cannot_be_empty()
var unused = new LineProtocolPoint("measurement", fields, MetricTags.Empty);
};

action.ShouldThrow<ArgumentException>();
action.Should().Throw<ArgumentException>();
}

[Fact]
Expand All @@ -103,7 +103,7 @@ public void Measurement_is_required()
var unused = new LineProtocolPoint(string.Empty, fields, MetricTags.Empty);
};

action.ShouldThrow<ArgumentException>();
action.Should().Throw<ArgumentException>();
}

[Theory]
Expand All @@ -122,11 +122,11 @@ public void Time_stamp_should_be_utc(DateTimeKind dateTimeKind, bool expected)

if (!expected)
{
action.ShouldThrow<ArgumentException>();
action.Should().Throw<ArgumentException>();
}
else
{
action.ShouldNotThrow();
action.Should().NotThrow();
}
}
}
Expand Down

0 comments on commit ad0232e

Please sign in to comment.