This repository has been archived by the owner on Mar 26, 2019. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 5
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge pull request #12 from nataly87s/master
#8 added IGraphiteNameFormatter
- Loading branch information
Showing
22 changed files
with
278 additions
and
345 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
99 changes: 99 additions & 0 deletions
99
src/App.Metrics.Formatting.Graphite/DefaultGraphiteNameFormatter.cs
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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(); | ||
} | ||
} | ||
} | ||
} |
30 changes: 30 additions & 0 deletions
30
src/App.Metrics.Formatting.Graphite/Extensions/GraphitePayloadExtentions.cs
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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(); | ||
} | ||
} | ||
} |
File renamed without changes.
Oops, something went wrong.