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 #12 from nataly87s/master
Browse files Browse the repository at this point in the history
#8 added IGraphiteNameFormatter
  • Loading branch information
alhardy committed Jun 30, 2017
2 parents 6fee242 + 6488525 commit 7bec39b
Show file tree
Hide file tree
Showing 22 changed files with 278 additions and 345 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,7 @@
using App.Metrics.Extensions.Reporting.Graphite;
using App.Metrics.Extensions.Reporting.Graphite.Client;
using App.Metrics.Formatting.Graphite;
using App.Metrics.Formatting.Graphite.Extensions;
using Microsoft.Extensions.Logging;

// ReSharper disable CheckNamespace
Expand All @@ -34,9 +35,7 @@ public static class TcpGraphitePayloadListExtensions

foreach (var batch in batches)
{
var payloadText = new StringWriter();
batch.Format(payloadText);
var text = payloadText.ToString();
var text = batch.Format(graphiteSettings.MetricNameFormatter);

logger.LogDebug(text);

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,7 @@
using App.Metrics.Extensions.Reporting.Graphite;
using App.Metrics.Extensions.Reporting.Graphite.Client;
using App.Metrics.Formatting.Graphite;
using App.Metrics.Formatting.Graphite.Extensions;
using Microsoft.Extensions.Logging;

// ReSharper disable CheckNamespace
Expand All @@ -33,9 +34,7 @@ public static class UdpGraphitePayloadListExtensions

foreach (var batch in batches)
{
var payloadText = new StringWriter();
batch.Format(payloadText);
var text = payloadText.ToString();
var text = batch.Format(graphiteSettings.MetricNameFormatter);

logger.LogDebug(text);

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -62,30 +62,25 @@ public GraphiteClient(ILoggerFactory loggerFactory, GraphiteSettings graphiteSet
return new GraphiteWriteResult(false, "Too many failures in writing to Graphite, Circuit Opened");
}

var batchedPayloads = new GraphteBatchedPayload(payload, _graphiteSettings.BatchSize);

try
{
var batches = batchedPayloads.GetBatches().ToList();

if (_graphiteSettings.Protocol == Protocol.Tcp)
{
await batches.TcpWriteAsync(_graphiteSettings, _httpPolicy, _logger, cancellationToken);
return new GraphiteWriteResult(true);
}

if (_graphiteSettings.Protocol == Protocol.Udp)
{
await batches.UdpWriteAsync(_graphiteSettings, _httpPolicy, _logger, cancellationToken);
return new GraphiteWriteResult(true);
}
var batches = payload.ToBatches(_graphiteSettings.BatchSize).ToList();

if (_graphiteSettings.Protocol == Protocol.Pickled)
switch (_graphiteSettings.Protocol)
{
throw new NotImplementedException("Picked protocol not implemented, use UDP or TCP");
case Protocol.Tcp:
await batches.TcpWriteAsync(_graphiteSettings, _httpPolicy, _logger, cancellationToken);
break;
case Protocol.Udp:
await batches.UdpWriteAsync(_graphiteSettings, _httpPolicy, _logger, cancellationToken);
break;
case Protocol.Pickled:
throw new NotImplementedException("Picked protocol not implemented, use UDP or TCP");
default:
throw new InvalidOperationException("Unsupported protocol, UDP, TCP and Pickled protocols are accepted");
}

throw new InvalidOperationException("Unsupported protocol, UDP, TCP and Pickled protocols are accepted");
return GraphiteWriteResult.SuccessResult;
}
catch (Exception ex)
{
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -3,16 +3,21 @@
// </copyright>

using System;
using App.Metrics.Formatting.Graphite;

namespace App.Metrics.Extensions.Reporting.Graphite.Client
{
// ReSharper disable InconsistentNaming
public class GraphiteSettings
// ReSharper restore InconsistentNaming
{
public GraphiteSettings(Uri baseAddress) { BaseAddress = baseAddress ?? throw new ArgumentNullException(nameof(baseAddress)); }
public GraphiteSettings(Uri baseAddress)
: this()
{
BaseAddress = baseAddress ?? throw new ArgumentNullException(nameof(baseAddress));
}

internal GraphiteSettings() { }
internal GraphiteSettings(IGraphiteNameFormatter nameFormatter = null) { MetricNameFormatter = nameFormatter ?? new DefaultGraphiteNameFormatter(); }

/// <summary>
/// Gets the Graphite host.
Expand Down Expand Up @@ -41,23 +46,31 @@ public Protocol Protocol
{
get
{
if (BaseAddress.Scheme.ToLowerInvariant() == "net.tcp")
{
return Protocol.Tcp;
}

if (BaseAddress.Scheme.ToLowerInvariant() == "net.udp")
{
return Protocol.Udp;
}

if (BaseAddress.Scheme.ToLowerInvariant() == "net.pickled")
switch (BaseAddress.Scheme.ToLowerInvariant())
{
return Protocol.Pickled;
case "net.tcp":
return Protocol.Tcp;
case "net.udp":
return Protocol.Udp;
case "net.pickled":
return Protocol.Pickled;
default:
throw new ArgumentException("Graphite URI scheme must be either net.tcp or net.udp or net.pickled", nameof(BaseAddress));
}

throw new ArgumentException("Graphite URI scheme must be either net.tcp or net.udp or net.pickled", nameof(BaseAddress));
}
}

/// <summary>
/// Gets or sets the metric name formatter func which takes the metric context and name and returns a formatted string
/// which will be reported to influx as the measurement
/// </summary>
/// <value>
/// The metric name formatter.
/// </value>
// ReSharper disable AutoPropertyCanBeMadeGetOnly.Global
// ReSharper disable MemberCanBePrivate.Global
public IGraphiteNameFormatter MetricNameFormatter { get; set; }
// ReSharper restore MemberCanBePrivate.Global
// ReSharper restore AutoPropertyCanBeMadeGetOnly.Global
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -21,5 +21,7 @@ public GraphiteWriteResult(bool success, string errorMessage)
public string ErrorMessage { get; }

public bool Success { get; }

public static readonly GraphiteWriteResult SuccessResult = new GraphiteWriteResult(true);
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -37,7 +37,7 @@ public IMetricReporter CreateMetricReporter(string name, ILoggerFactory loggerFa
loggerFactory,
_settings.GraphiteSettings,
_settings.HttpPolicy);
var payloadBuilder = new GraphitePayloadBuilder(_settings.MetricNameFormatter, _settings.DataKeys);
var payloadBuilder = new GraphitePayloadBuilder(_settings.GraphiteSettings.MetricNameFormatter, _settings.DataKeys);

return new ReportRunner<GraphitePayload>(
async p =>
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -21,9 +21,6 @@ public GraphiteReporterSettings()
GraphiteSettings = new GraphiteSettings();
HttpPolicy = new HttpPolicy();
ReportInterval = TimeSpan.FromSeconds(5);
MetricNameFormatter = (metricContext, metricName) => metricContext.IsMissing()
? $"{metricName}".Replace(' ', '_').Replace('.', '_')
: $"{metricContext}.{metricName.Replace(' ', '_').Replace('.', '_')}".Replace(' ', '_');
}

/// <inheritdoc />
Expand All @@ -47,19 +44,6 @@ public GraphiteReporterSettings()
/// </value>
public HttpPolicy HttpPolicy { get; set; }

/// <summary>
/// Gets or sets the metric name formatter func which takes the metric context and name and returns a formatted string
/// which will be reported to influx as the measurement
/// </summary>
/// <value>
/// The metric name formatter.
/// </value>
// ReSharper disable AutoPropertyCanBeMadeGetOnly.Global
// ReSharper disable MemberCanBePrivate.Global
public Func<string, string, string> MetricNameFormatter { get; set; }
// ReSharper restore MemberCanBePrivate.Global
// ReSharper restore AutoPropertyCanBeMadeGetOnly.Global

/// <summary>
/// Gets or sets the report interval for which to flush metrics to Graphite.
/// </summary>
Expand Down
5 changes: 1 addition & 4 deletions src/App.Metrics.Formatting.Graphite/Constants.cs
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,6 @@
// Copyright (c) Allan Hardy. All rights reserved.
// </copyright>

using System;
using System.Collections.Generic;
using App.Metrics.Reporting;

Expand Down Expand Up @@ -105,9 +104,7 @@ public class GraphiteDefaults
{ MeterValueDataKeys.Rate15M, "Rate-15-Min" }
};

public static readonly Func<string, string, string> MetricNameFormatter = (metricContext, metricName) => string.IsNullOrWhiteSpace(metricContext)
? $"{metricName}".Replace(' ', '_').Replace('.', '_')
: $"{metricContext}.{metricName.Replace(' ', '_').Replace('.', '_')}".Replace(' ', '_');
public static readonly IGraphiteNameFormatter MetricNameFormatter = new DefaultGraphiteNameFormatter();
}
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,99 @@
// <copyright file="DefaultGraphiteNameFormatter.cs" company="Allan Hardy">
// Copyright (c) Allan Hardy. All rights reserved.
// </copyright>

using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using App.Metrics.Tagging;

namespace App.Metrics.Formatting.Graphite
{
public class DefaultGraphiteNameFormatter : IGraphiteNameFormatter
{
private static readonly HashSet<string> ExcludeTags = new HashSet<string> { "app", "env", "server", "mtype", "unit", "unit_rate", "unit_dur" };

/// <inheritdoc />
public IEnumerable<string> Format(GraphitePoint point)
{
var sb = new StringBuilder();

var tagsDictionary = point.Tags.ToDictionary(GraphiteSyntax.EscapeName);

if (tagsDictionary.TryGetValue("app", out var appValue))
{
sb.Append("app.");
sb.Append(appValue);
}

if (tagsDictionary.TryGetValue("env", out var envValue))
{
if (sb.Length > 0)
{
sb.Append('.');
}

sb.Append("env.");
sb.Append(envValue);
}

if (tagsDictionary.TryGetValue("server", out var serverValue))
{
if (sb.Length > 0)
{
sb.Append('.');
}

sb.Append("server.");
sb.Append(serverValue);
}

if (tagsDictionary.TryGetValue("mtype", out var metricType) && !string.IsNullOrWhiteSpace(metricType))
{
if (sb.Length > 0)
{
sb.Append('.');
}

sb.Append(metricType);
sb.Append(".");
}

if (!point.Context.IsMissing())
{
sb.Append(GraphiteSyntax.EscapeName(point.Context, true));
sb.Append(".");
}

sb.Append(GraphiteSyntax.EscapeName(point.Name, true));

var tags = tagsDictionary.Where(tag => !ExcludeTags.Contains(tag.Key));

foreach (var tag in tags)
{
sb.Append('.');
sb.Append(GraphiteSyntax.EscapeName(tag.Key));
sb.Append('.');
sb.Append(tag.Value);
}

sb.Append('.');
var prefix = sb.ToString();

foreach (var f in point.Fields)
{
var fieldStringBuilder = new StringBuilder(prefix);
fieldStringBuilder.Append(GraphiteSyntax.EscapeName(f.Key));

fieldStringBuilder.Append(' ');
fieldStringBuilder.Append(GraphiteSyntax.FormatValue(f.Value));

fieldStringBuilder.Append(' ');
fieldStringBuilder.Append(GraphiteSyntax.FormatTimestamp(point.UtcTimestamp));

yield return fieldStringBuilder.ToString();
}
}
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,30 @@
// <copyright file="GraphitePayloadExtentions.cs" company="Allan Hardy">
// Copyright (c) Allan Hardy. All rights reserved.
// </copyright>

using System.Linq;
using System.Text;

namespace App.Metrics.Formatting.Graphite.Extensions
{
public static class GraphitePayloadExtentions
{
public static string Format(this GraphitePayload payload, IGraphiteNameFormatter formatter)
{
if (formatter == null)
{
return null;
}

var sb = new StringBuilder();

foreach (var formatted in payload.SelectMany(formatter.Format))
{
sb.Append(formatted);
sb.Append("\n");
}

return sb.ToString();
}
}
}
Loading

0 comments on commit 7bec39b

Please sign in to comment.