-
Notifications
You must be signed in to change notification settings - Fork 0
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 #44 from Particular/master-to-develop
Master to develop
- Loading branch information
Showing
9 changed files
with
339 additions
and
14 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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,88 @@ | ||
namespace NServiceBus.Metrics.PerformanceCounters | ||
{ | ||
using System.Collections.Generic; | ||
using System.IO; | ||
using System.Text; | ||
|
||
static class CSharpCounterWriter | ||
{ | ||
public static void WriteCode(string scriptPath, IEnumerable<TimerDefinition> timers, IEnumerable<MeterDefinition> meters, Dictionary<string, string> legacyInstanceNameMap) | ||
{ | ||
var outputPath = Path.Combine(scriptPath, "Counters.g.cs"); | ||
using (var streamWriter = File.CreateText(outputPath)) | ||
{ | ||
var stringBuilder = new StringBuilder(); | ||
|
||
var slaCounterDefinition = @"new CounterCreationData(""SLA violation countdown"", ""Seconds until the SLA for this endpoint is breached."", PerformanceCounterType.NumberOfItems32),"; | ||
stringBuilder.AppendLine(slaCounterDefinition.PadLeft(slaCounterDefinition.Length + 8)); | ||
|
||
foreach (var timer in timers) | ||
{ | ||
var timerDefinition = $@"new CounterCreationData(""{timer.Name}"", ""{timer.Description}"", PerformanceCounterType.NumberOfItems32),"; | ||
stringBuilder.AppendLine(timerDefinition.PadLeft(timerDefinition.Length + 8)); | ||
} | ||
|
||
foreach (var meter in meters) | ||
{ | ||
string instanceName; | ||
legacyInstanceNameMap.TryGetValue(meter.Name, out instanceName); | ||
|
||
var meterDefinition = $@"new CounterCreationData(""{instanceName ?? meter.Name}"", ""{meter.Description}"", PerformanceCounterType.RateOfCountsPerSecond32),"; | ||
stringBuilder.AppendLine(meterDefinition.PadLeft(meterDefinition.Length + 8)); | ||
} | ||
|
||
streamWriter.Write(Template, stringBuilder); | ||
} | ||
} | ||
|
||
const string Template = @"using System; | ||
using System.Diagnostics; | ||
using System.Security; | ||
using System.Runtime.CompilerServices; | ||
[CompilerGenerated] | ||
public static class CounterCreator | ||
{{ | ||
public static void Create() | ||
{{ | ||
var counterCreationCollection = new CounterCreationDataCollection(Counters); | ||
try | ||
{{ | ||
var categoryName = ""NServiceBus""; | ||
if (PerformanceCounterCategory.Exists(categoryName)) | ||
{{ | ||
foreach (CounterCreationData counter in counterCreationCollection) | ||
{{ | ||
if (!PerformanceCounterCategory.CounterExists(counter.CounterName, categoryName)) | ||
{{ | ||
PerformanceCounterCategory.Delete(categoryName); | ||
break; | ||
}} | ||
}} | ||
}} | ||
if (PerformanceCounterCategory.Exists(categoryName) == false) | ||
{{ | ||
PerformanceCounterCategory.Create( | ||
categoryName: categoryName, | ||
categoryHelp: ""NServiceBus statistics"", | ||
categoryType: PerformanceCounterCategoryType.MultiInstance, | ||
counterData: counterCreationCollection); | ||
}} | ||
PerformanceCounter.CloseSharedResources(); | ||
}} | ||
catch (Exception ex) when (ex is SecurityException || ex is UnauthorizedAccessException) | ||
{{ | ||
throw new Exception(""Execution requires elevated permissions"", ex); | ||
}} | ||
}} | ||
static CounterCreationData[] Counters = new CounterCreationData[] | ||
{{ | ||
{0} | ||
}}; | ||
}}"; | ||
} | ||
} |
Oops, something went wrong.