Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

support setting a prefix for graphite metric names #105

Open
wants to merge 1 commit into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
22 changes: 12 additions & 10 deletions Src/Metrics/Graphite/GraphiteExtensions.cs
Original file line number Diff line number Diff line change
Expand Up @@ -6,39 +6,41 @@ namespace Metrics
{
public static class GraphiteExtensions
{
public static MetricsReports WithGraphite(this MetricsReports reports, Uri graphiteUri, TimeSpan interval)
public static MetricsReports WithGraphite(this MetricsReports reports, Uri graphiteUri, TimeSpan interval,
string metricNamePrefix = null)
{
if (graphiteUri.Scheme.ToLowerInvariant() == "net.tcp")
{
return reports.WithTCPGraphite(graphiteUri.Host, graphiteUri.Port, interval);
return reports.WithTCPGraphite(graphiteUri.Host, graphiteUri.Port, interval, metricNamePrefix);
}

if (graphiteUri.Scheme.ToLowerInvariant() == "net.udp")
{
return reports.WithUDPGraphite(graphiteUri.Host, graphiteUri.Port, interval);
return reports.WithUDPGraphite(graphiteUri.Host, graphiteUri.Port, interval, metricNamePrefix);
}

if (graphiteUri.Scheme.ToLowerInvariant() == "net.pickled")
{
return reports.WithPickledGraphite(graphiteUri.Host, graphiteUri.Port, interval);
return reports.WithPickledGraphite(graphiteUri.Host, graphiteUri.Port, interval, PickleGraphiteSender.DefaultPickleJarSize, metricNamePrefix);
}

throw new ArgumentException("Graphite uri scheme must be either net.tcp or net.udp or net.pickled (ex: net.udp://graphite.myhost.com:2003 )", "graphiteUri");
}

public static MetricsReports WithPickledGraphite(this MetricsReports reports, string host, int port, TimeSpan interval, int batchSize = PickleGraphiteSender.DefaultPickleJarSize)
public static MetricsReports WithPickledGraphite(this MetricsReports reports, string host, int port, TimeSpan interval, int batchSize = PickleGraphiteSender.DefaultPickleJarSize,
string metricNamePrefix = null)
{
return reports.WithGraphite(new PickleGraphiteSender(host, port, batchSize), interval);
return reports.WithGraphite(new PickleGraphiteSender(host, port, batchSize, metricNamePrefix), interval);
}

public static MetricsReports WithTCPGraphite(this MetricsReports reports, string host, int port, TimeSpan interval)
public static MetricsReports WithTCPGraphite(this MetricsReports reports, string host, int port, TimeSpan interval, string metricNamePrefix = null)
{
return reports.WithGraphite(new TcpGraphiteSender(host, port), interval);
return reports.WithGraphite(new TcpGraphiteSender(host, port, metricNamePrefix), interval);
}

public static MetricsReports WithUDPGraphite(this MetricsReports reports, string host, int port, TimeSpan interval)
public static MetricsReports WithUDPGraphite(this MetricsReports reports, string host, int port, TimeSpan interval, string metricNamePrefix = null)
{
return reports.WithGraphite(new UdpGraphiteSender(host, port), interval);
return reports.WithGraphite(new UdpGraphiteSender(host, port, metricNamePrefix), interval);
}

public static MetricsReports WithGraphite(this MetricsReports reports, GraphiteSender graphiteLink, TimeSpan interval)
Expand Down
4 changes: 3 additions & 1 deletion Src/Metrics/Graphite/GraphiteSender.cs
Original file line number Diff line number Diff line change
Expand Up @@ -4,9 +4,11 @@ namespace Metrics.Graphite
{
public abstract class GraphiteSender : IDisposable
{
protected string MetricNamePrefix;
public virtual void Send(string name, string value, string timestamp)
{
var data = string.Concat(name, " ", value, " ", timestamp, "\n");
var metricName = string.IsNullOrEmpty(MetricNamePrefix) ? name : string.Format("{0}.{1}", MetricNamePrefix, name);
var data = string.Concat(metricName, " ", value, " ", timestamp, "\n");
SendData(data);
}

Expand Down
3 changes: 2 additions & 1 deletion Src/Metrics/Graphite/PickleGraphiteSender.cs
Original file line number Diff line number Diff line change
Expand Up @@ -21,11 +21,12 @@ public sealed class PickleGraphiteSender : GraphiteSender
private PickleJar jar = new PickleJar();


public PickleGraphiteSender(string host, int port, int batchSize = DefaultPickleJarSize)
public PickleGraphiteSender(string host, int port, int batchSize = DefaultPickleJarSize, string metricNamePrefix = null)
{
this.host = host;
this.port = port;
this.pickleJarSize = batchSize;
this.MetricNamePrefix = metricNamePrefix;
}

public override void Send(string name, string value, string timestamp)
Expand Down
3 changes: 2 additions & 1 deletion Src/Metrics/Graphite/TcpGraphiteSender.cs
Original file line number Diff line number Diff line change
Expand Up @@ -16,10 +16,11 @@ public sealed class TcpGraphiteSender : GraphiteSender

private TcpClient client;

public TcpGraphiteSender(string host, int port)
public TcpGraphiteSender(string host, int port, string metricNamePrefix = null)
{
this.host = host;
this.port = port;
this.MetricNamePrefix = metricNamePrefix;
}

protected override void SendData(string data)
Expand Down
5 changes: 3 additions & 2 deletions Src/Metrics/Graphite/UdpGraphiteSender.cs
Original file line number Diff line number Diff line change
Expand Up @@ -16,10 +16,11 @@ public sealed class UdpGraphiteSender : GraphiteSender

private UdpClient client;

public UdpGraphiteSender(string host, int port)
public UdpGraphiteSender(string host, int port, string metricNamePrefix = null)
{
this.host = host;
this.port = port;
this.MetricNamePrefix = metricNamePrefix;
}

protected override void SendData(string data)
Expand All @@ -43,7 +44,7 @@ protected override void SendData(string data)
}

public override void Flush()
{
{
}

private static UdpClient InitClient(string host, int port)
Expand Down