Skip to content

Commit

Permalink
Allow metrics to be bound to a type early. fixes #8
Browse files Browse the repository at this point in the history
  • Loading branch information
bretcope committed Dec 1, 2014
1 parent 958f7f4 commit f5c38f3
Show file tree
Hide file tree
Showing 2 changed files with 27 additions and 5 deletions.
28 changes: 24 additions & 4 deletions BosunReporter/BosunReporter.cs
Original file line number Diff line number Diff line change
Expand Up @@ -79,6 +79,24 @@ public BosunReporter(BosunReporterOptions options)
_metaDataTimer = new Timer(PostMetaData, null, TimeSpan.FromSeconds(30), TimeSpan.FromSeconds(options.MetaDataReportingInterval));
}

public void BindMetric(string name, Type type)
{
name = MetricsNamePrefix + name;
lock (_rootNameToType)
{
if (_rootNameToType.ContainsKey(name) && _rootNameToType[name] != type)
{
throw new Exception(
String.Format("Cannot bind metric name \"{0}\" to Type {1}. It has already been bound to {2}",
name, type.FullName, _rootNameToType[name].FullName));
}

// if (type)

_rootNameToType[name] = type;
}
}

public T GetMetric<T>(string name, T metric = null) where T : BosunMetric
{
var metricType = typeof (T);
Expand All @@ -95,16 +113,18 @@ public BosunReporter(BosunReporterOptions options)
name = MetricsNamePrefix + name;
lock (_rootNameToType)
{
if (_nameAndSuffixToRootName.ContainsKey(name) && (!_rootNameToType.ContainsKey(name) || _rootNameToType[name] != metricType))
if (_rootNameToType.ContainsKey(name))
{
if (_rootNameToType.ContainsKey(name))
if (_rootNameToType[name] != metricType)
{
throw new Exception(
String.Format(
"Attempted to create metric name \"{0}\" with Type {1}. This metric name has already been assigned to Type {2}.",
"Attempted to create metric name \"{0}\" with Type {1}. This metric name has already been bound to Type {2}.",
name, metricType.FullName, _rootNameToType[name].FullName));
}

}
else if (_nameAndSuffixToRootName.ContainsKey(name))
{
throw new Exception(
String.Format(
"Attempted to create metric name \"{0}\" with Type {1}. This metric name is already in use as a suffix of Type {2}.",
Expand Down
4 changes: 3 additions & 1 deletion Scratch/Program.cs
Original file line number Diff line number Diff line change
Expand Up @@ -23,10 +23,12 @@ static void Main(string[] args)
MetricsNamePrefix = "bret.",
GetBosunUrl = getUrl,
ThrowOnPostFail = true,
ReportingInterval = 30,
ReportingInterval = 5,
PropertyToTagName = NameTransformers.CamelToLowerSnakeCase
};
var reporter = new BosunReporter.BosunReporter(options);

reporter.BindMetric("my_counter", typeof(TestCounter));
var counter = reporter.GetMetric<TestCounter>("my_counter");
counter.Increment();
counter.Increment();
Expand Down

0 comments on commit f5c38f3

Please sign in to comment.