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

Commit

Permalink
allow reporting only configured fields
Browse files Browse the repository at this point in the history
  • Loading branch information
alhardy committed Jun 25, 2018
1 parent fe13645 commit baa9b93
Show file tree
Hide file tree
Showing 4 changed files with 103 additions and 13 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -20,12 +20,14 @@ public static class MetricsGrafanaCloudHostedMetricsFormatterBuilder
/// options.
/// </param>
/// <param name="setupAction">The GrafanaCloud Hosted Metrics formatting options to use.</param>
/// <param name="fields">The metric fields to report as well as their names.</param>
/// <returns>
/// An <see cref="IMetricsBuilder" /> that can be used to further configure App Metrics.
/// </returns>
public static IMetricsBuilder AsGrafanaCloudHostedMetricsGraphiteSyntax(
this IMetricsOutputFormattingBuilder metricFormattingBuilder,
Action<MetricsHostedMetricsOptions> setupAction)
Action<MetricsHostedMetricsOptions> setupAction,
MetricFields fields = null)
{
if (metricFormattingBuilder == null)
{
Expand All @@ -41,7 +43,7 @@ public static IMetricsBuilder AsGrafanaCloudHostedMetricsGraphiteSyntax(

setupAction.Invoke(options);

var formatter = new MetricsHostedMetricsJsonOutputFormatter(options);
var formatter = new MetricsHostedMetricsJsonOutputFormatter(options, fields);

return metricFormattingBuilder.Using(formatter, false);
}
Expand All @@ -54,17 +56,48 @@ public static IMetricsBuilder AsGrafanaCloudHostedMetricsGraphiteSyntax(
/// The <see cref="IMetricsOutputFormattingBuilder" /> used to configure GrafanaCloud Hosted Metrics formatting
/// options.
/// </param>
/// <param name="options">The GrafanaCloud Hosted Metrics formatting options to use.</param>
/// <param name="fields">The metric fields to report as well as their names.</param>
/// <returns>
/// An <see cref="IMetricsBuilder" /> that can be used to further configure App Metrics.
/// </returns>
public static IMetricsBuilder AsGrafanaCloudHostedMetricsGraphiteSyntax(this IMetricsOutputFormattingBuilder metricFormattingBuilder)
public static IMetricsBuilder AsGrafanaCloudHostedMetricsGraphiteSyntax(
this IMetricsOutputFormattingBuilder metricFormattingBuilder,
MetricsHostedMetricsOptions options,
MetricFields fields = null)
{
if (metricFormattingBuilder == null)
{
throw new ArgumentNullException(nameof(metricFormattingBuilder));
}

var formatter = new MetricsHostedMetricsJsonOutputFormatter(options, fields);

return metricFormattingBuilder.Using(formatter, false);
}

/// <summary>
/// Add the <see cref="MetricsHostedMetricsJsonOutputFormatter" /> allowing metrics to optionally be reported to
/// GrafanaCloud Hosted Metrics Graphite syntax.
/// </summary>
/// <param name="metricFormattingBuilder">s
/// The <see cref="IMetricsOutputFormattingBuilder" /> used to configure GrafanaCloud Hosted Metrics formatting
/// options.
/// </param>
/// <param name="fields">The metric fields to report as well as their names.</param>
/// <returns>
/// An <see cref="IMetricsBuilder" /> that can be used to further configure App Metrics.
/// </returns>
public static IMetricsBuilder AsGrafanaCloudHostedMetricsGraphiteSyntax(
this IMetricsOutputFormattingBuilder metricFormattingBuilder,
MetricFields fields = null)
{
if (metricFormattingBuilder == null)
{
throw new ArgumentNullException(nameof(metricFormattingBuilder));
}

var formatter = new MetricsHostedMetricsJsonOutputFormatter();
var formatter = new MetricsHostedMetricsJsonOutputFormatter(fields);

return metricFormattingBuilder.Using(formatter, false);
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -45,7 +45,6 @@ public void Write(string context, string name, IEnumerable<string> columns, IEnu
public void Dispose()
{
Dispose(true);
GC.SuppressFinalize(this);
}

/// <summary>
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,7 @@
using System.Threading;
using System.Threading.Tasks;
using App.Metrics.Serialization;

#if !NETSTANDARD1_6
using App.Metrics.Internal;
#endif
Expand All @@ -17,16 +18,25 @@ public class MetricsHostedMetricsJsonOutputFormatter : IMetricsOutputFormatter
{
private readonly MetricsHostedMetricsOptions _options;

public MetricsHostedMetricsJsonOutputFormatter()
public MetricsHostedMetricsJsonOutputFormatter() { _options = new MetricsHostedMetricsOptions(); }

public MetricsHostedMetricsJsonOutputFormatter(MetricFields metricFields)
{
_options = new MetricsHostedMetricsOptions();
MetricFields = metricFields;
}

public MetricsHostedMetricsJsonOutputFormatter(MetricsHostedMetricsOptions options)
{
_options = options ?? throw new ArgumentNullException(nameof(options));
}

public MetricsHostedMetricsJsonOutputFormatter(MetricsHostedMetricsOptions options, MetricFields metricFields)
{
_options = options ?? throw new ArgumentNullException(nameof(options));
MetricFields = metricFields;
}

/// <inheritdoc />
public MetricsMediaTypeValue MediaType => new MetricsMediaTypeValue("text", "vnd.appmetrics.metrics.hostedmetrics", "v1", "plain");

Expand Down Expand Up @@ -63,4 +73,4 @@ public Task WriteAsync(
#endif
}
}
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,8 @@
using System.Net.Http;
using System.Net.Http.Headers;
using App.Metrics.Builder;
using App.Metrics.Formatters;
using App.Metrics.Formatters.GrafanaCloudHostedMetrics;
using App.Metrics.Reporting.GrafanaCloudHostedMetrics;
using App.Metrics.Reporting.GrafanaCloudHostedMetrics.Client;

Expand Down Expand Up @@ -81,13 +83,17 @@ public static IMetricsBuilder ToHostedMetrics(
/// </param>
/// <param name="url">The base url where metrics are written.</param>
/// <param name="apiKey">The api key used for authentication</param>
/// <param name="fieldsSetup">The metric fields to report as well as their names.</param>
/// <param name="hostedMetricsOptionsSetup">The setup action to configure the <see cref="MetricsHostedMetricsOptions"/> to use.</param>
/// <returns>
/// An <see cref="IMetricsBuilder" /> that can be used to further configure App Metrics.
/// </returns>
public static IMetricsBuilder ToHostedMetrics(
this IMetricsReportingBuilder metricReporterProviderBuilder,
string url,
string apiKey)
string apiKey,
Action<MetricFields> fieldsSetup = null,
Action<MetricsHostedMetricsOptions> hostedMetricsOptionsSetup = null)
{
if (metricReporterProviderBuilder == null)
{
Expand All @@ -104,20 +110,39 @@ public static IMetricsBuilder ToHostedMetrics(
throw new InvalidOperationException($"{nameof(url)} must be a valid absolute URI");
}

var hostedMetricsOptions = new MetricsHostedMetricsOptions();

hostedMetricsOptionsSetup?.Invoke(hostedMetricsOptions);

IMetricsOutputFormatter formatter;
MetricFields fields = null;

if (fieldsSetup == null)
{
formatter = new MetricsHostedMetricsJsonOutputFormatter(hostedMetricsOptions);
}
else
{
fields = new MetricFields();
fieldsSetup.Invoke(fields);
formatter = new MetricsHostedMetricsJsonOutputFormatter(hostedMetricsOptions, fields);
}

var options = new MetricsReportingHostedMetricsOptions
{
HostedMetrics =
{
BaseUri = uri,
ApiKey = apiKey
}
},
MetricsOutputFormatter = formatter
};

var httpClient = CreateClient(options, options.HttpPolicy);
var reporter = new HostedMetricsReporter(options, httpClient);

var builder = metricReporterProviderBuilder.Using(reporter);
builder.OutputMetrics.AsGrafanaCloudHostedMetricsGraphiteSyntax();
builder.OutputMetrics.AsGrafanaCloudHostedMetricsGraphiteSyntax(hostedMetricsOptions, fields);

return builder;
}
Expand All @@ -134,14 +159,18 @@ public static IMetricsBuilder ToHostedMetrics(
/// The <see cref="T:System.TimeSpan" /> interval used if intended to schedule metrics
/// reporting.
/// </param>
/// <param name="fieldsSetup">The metric fields to report as well as their names.</param>
/// <param name="hostedMetricsOptionsSetup">The setup action to configure the <see cref="MetricsHostedMetricsOptions"/> to use.</param>
/// <returns>
/// An <see cref="IMetricsBuilder" /> that can be used to further configure App Metrics.
/// </returns>
public static IMetricsBuilder ToHostedMetrics(
this IMetricsReportingBuilder metricReporterProviderBuilder,
string url,
string apiKey,
TimeSpan flushInterval)
TimeSpan flushInterval,
Action<MetricFields> fieldsSetup = null,
Action<MetricsHostedMetricsOptions> hostedMetricsOptionsSetup = null)
{
if (metricReporterProviderBuilder == null)
{
Expand All @@ -158,21 +187,40 @@ public static IMetricsBuilder ToHostedMetrics(
throw new InvalidOperationException($"{nameof(url)} must be a valid absolute URI");
}

var hostedMetricsOptions = new MetricsHostedMetricsOptions();

hostedMetricsOptionsSetup?.Invoke(hostedMetricsOptions);

IMetricsOutputFormatter formatter;
MetricFields fields = null;

if (fieldsSetup == null)
{
formatter = new MetricsHostedMetricsJsonOutputFormatter(hostedMetricsOptions);
}
else
{
fields = new MetricFields();
fieldsSetup.Invoke(fields);
formatter = new MetricsHostedMetricsJsonOutputFormatter(hostedMetricsOptions, fields);
}

var options = new MetricsReportingHostedMetricsOptions
{
FlushInterval = flushInterval,
HostedMetrics =
{
BaseUri = uri,
ApiKey = apiKey
}
},
MetricsOutputFormatter = formatter
};

var httpClient = CreateClient(options, options.HttpPolicy);
var reporter = new HostedMetricsReporter(options, httpClient);

var builder = metricReporterProviderBuilder.Using(reporter);
builder.OutputMetrics.AsGrafanaCloudHostedMetricsGraphiteSyntax();
builder.OutputMetrics.AsGrafanaCloudHostedMetricsGraphiteSyntax(hostedMetricsOptions, fields);

return builder;
}
Expand Down

0 comments on commit baa9b93

Please sign in to comment.