Skip to content

Commit

Permalink
Allow dots in context names when reporting to Graphite.
Browse files Browse the repository at this point in the history
  • Loading branch information
vchekan committed Apr 16, 2015
1 parent 868b93d commit 1bfb6bd
Show file tree
Hide file tree
Showing 2 changed files with 7 additions and 4 deletions.
9 changes: 6 additions & 3 deletions Src/Metrics/Graphite/GraphiteReport.cs
Original file line number Diff line number Diff line change
Expand Up @@ -12,6 +12,7 @@ namespace Metrics.Graphite
public class GraphiteReport : BaseReport, IDisposable
{
private static readonly Regex invalid = new Regex(@"[^a-zA-Z0-9\-%&]+", RegexOptions.CultureInvariant | RegexOptions.Compiled);
private static readonly Regex invalidAllowDots = new Regex(@"[^a-zA-Z0-9\-%&.]+", RegexOptions.CultureInvariant | RegexOptions.Compiled);
private static readonly Regex slash = new Regex(@"\s*/\s*", RegexOptions.CultureInvariant | RegexOptions.Compiled);

private readonly GraphiteSender sender;
Expand Down Expand Up @@ -133,7 +134,7 @@ protected virtual void Send(string name, string value)
protected override string FormatContextName(IEnumerable<string> contextStack, string contextName)
{
var parts = contextStack.Concat(new[] { contextName })
.Select(GraphiteName);
.Select(_ => GraphiteName(_, true));

return string.Join(".", parts);
}
Expand Down Expand Up @@ -196,10 +197,12 @@ protected virtual string FormatUnit(string unit, string name)
return string.Concat("-", clean);
}

protected virtual string GraphiteName(string name)
protected virtual string GraphiteName(string name, bool allowDots = false)
{
var noSlash = slash.Replace(name, "-per-");
return invalid.Replace(noSlash, "_").Trim('_');
return allowDots ?
invalidAllowDots.Replace(noSlash, "_").Trim('_') :
invalid.Replace(noSlash, "_").Trim('_');
}

public void Dispose()
Expand Down
2 changes: 1 addition & 1 deletion Src/Metrics/Metric.cs
Original file line number Diff line number Diff line change
Expand Up @@ -187,7 +187,7 @@ private static string GetGlobalContextName()
try
{
var configName = ConfigurationManager.AppSettings["Metrics.GlobalContextName"];
var name = string.IsNullOrEmpty(configName) ? Process.GetCurrentProcess().ProcessName : configName;
var name = string.IsNullOrEmpty(configName) ? Process.GetCurrentProcess().ProcessName.Replace('.', '_') : configName;
log.Debug(() => "Metrics: GlobalContext Name set to " + name);
return name;
}
Expand Down

0 comments on commit 1bfb6bd

Please sign in to comment.