Skip to content
This repository has been archived by the owner on Jan 24, 2022. It is now read-only.

Commit

Permalink
Merge pull request #5 from MindFlavor/pc_mapping/pr
Browse files Browse the repository at this point in the history
Implemented performance counter filter
  • Loading branch information
MindFlavor authored Mar 18, 2019
2 parents fd58f48 + 9ed599d commit 42beb55
Show file tree
Hide file tree
Showing 9 changed files with 685 additions and 16 deletions.
25 changes: 18 additions & 7 deletions CommandLineOptions.cs
Original file line number Diff line number Diff line change
Expand Up @@ -4,24 +4,35 @@
public class CommandLineOptions
{
[JsonProperty(PropertyName = "instances")]
public List<SQLServerInstanceOptions> Instances { get; set; }
public List<SQLServerInstance> Instances { get; set; }

[JsonProperty(PropertyName = "port")]
public int Port { get; set; }

[JsonProperty(PropertyName = "waitStatsOptions")]
public WaitStatsOptions WaitStatsOptions { get; set; }
[JsonProperty(PropertyName = "waitStats")]
public WaitStats WaitStats { get; set; }

[JsonProperty(PropertyName = "performanceCounters")]
public PerformanceCounters PerformanceCounters { get; set; }
}

public class PerformanceCounters
{
[JsonProperty(PropertyName = "templateFiles")]
public string[] TemplateFiles { get; set; }


}

public class SQLServerInstanceOptions
public class SQLServerInstance
{
[JsonProperty(PropertyName = "connectionString")]
public string ConnectionString { get; set; }
}

public class WaitStatsOptions
public class WaitStats
{
[JsonProperty(PropertyName = "waitStatsFiles")]
public string[] WaitStatsFiles { get; set; }
[JsonProperty(PropertyName = "templateFiles")]
public string[] TemplateFiles { get; set; }

}
53 changes: 48 additions & 5 deletions Counters/PerformanceCounters.cs
Original file line number Diff line number Diff line change
Expand Up @@ -20,10 +20,16 @@ public struct GrafanaPerformanceCounter
{
public string type;
public string name;

public override string ToString()
{
return $"{this.GetType().Name}[type == {type}, name == {name}]";
}
}

public class PerformanceCounters
{
private static HashSet<string> EnabledCounters { get; set; }
static Dictionary<string, GrafanaPerformanceCounter> _dGraf;

public SQLServerInfo SQLServerInfo;
Expand Down Expand Up @@ -51,15 +57,44 @@ static PerformanceCounters()
}
}

private static string KeyFromObjectNameAndCounterName(string objectName, string counterName)
{
return objectName + "_" + counterName;
}

public PerformanceCounters(HttpContext context, SQLServerInfo sqlServerInfo)
{
this.SQLServerInfo = sqlServerInfo;
logger = context.RequestServices.GetRequiredService<ILogger<PerformanceCounters>>();

// in theory we should guard this code against concurrent access. It is possibile that
// two or more threads will work on the same HashSet concurrently because the all
// tested Waits == null to be true. In practice this will never happen as long as it's only
// one Prometheus calling our method. For now we will avoid the cost of a mutex, if bugs
// arise it will be included here.
if (EnabledCounters == null)
{
EnabledCounters = new HashSet<string>();

foreach (var file in Program.CommandLineOptions.PerformanceCounters.TemplateFiles)
{
System.IO.FileInfo fi = new System.IO.FileInfo(file);
logger.LogInformation($"Loading performance counter template to include from {fi.FullName}...");

using (System.IO.StreamReader sr = new System.IO.StreamReader(new System.IO.FileStream(fi.FullName,
System.IO.FileMode.Open, System.IO.FileAccess.Read, System.IO.FileShare.Read)))
{
string str;
while ((str = sr.ReadLine()) != null)
{
if (str.StartsWith("#"))
continue;

EnabledCounters.Add(str);
}
}
}
}
}

private static string KeyFromObjectNameAndCounterName(string objectName, string counterName)
{
return objectName + "_" + counterName;
}

public async Task<string> QueryAndSerializeData()
Expand Down Expand Up @@ -98,6 +133,14 @@ public async Task<string> QueryAndSerializeData()
GrafanaPerformanceCounter gpc;
if (_dGraf.TryGetValue(key, out gpc))
{
// skip this is it's not in the enabled performance counters
// as specified by the template files configured.
if (!EnabledCounters.Contains(gpc.name))
{
logger.LogDebug($"PerformanceCounter {gpc} will be skipped because it's not in any configured template file");
continue;
}

string gpcName = $"sql_pc_{gpc.name}";

sb.Append($"# TYPE {gpcName} {gpc.type}\n");
Expand Down
4 changes: 2 additions & 2 deletions Counters/WaitStats.cs
Original file line number Diff line number Diff line change
Expand Up @@ -38,10 +38,10 @@ public WaitStats(HttpContext context, SQLServerInfo sqlServerInfo)
{
Waits = new HashSet<string>();

foreach (var file in Program.CommandLineOptions.WaitStatsOptions.WaitStatsFiles)
foreach (var file in Program.CommandLineOptions.WaitStats.TemplateFiles)
{
System.IO.FileInfo fi = new System.IO.FileInfo(file);
logger.LogInformation($"Loading wait stats to include from {fi.FullName}...");
logger.LogInformation($"Loading wait stats template to include from {fi.FullName}...");

using (System.IO.StreamReader sr = new System.IO.StreamReader(new System.IO.FileStream(fi.FullName,
System.IO.FileMode.Open, System.IO.FileAccess.Read, System.IO.FileShare.Read)))
Expand Down
6 changes: 6 additions & 0 deletions MindFlavor.SQLServerExporter.csproj
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,12 @@
<PropertyGroup>
<TargetFramework>netcoreapp2.2</TargetFramework>
<AspNetCoreHostingModel>InProcess</AspNetCoreHostingModel>
<Version>0.2.0.0</Version>
<Authors>Francesco Cogno</Authors>
<Copyright>Apache License Version 2.0</Copyright>
<PackageLicenseUrl>https://raw.githubusercontent.com/MindFlavor/prometheus_sql_server_exporter/master/LICENSE</PackageLicenseUrl>
<PackageProjectUrl>https://mindflavor.github.io/prometheus_sql_server_exporter</PackageProjectUrl>
<RepositoryUrl>https://github.com/MindFlavor/prometheus_sql_server_exporter</RepositoryUrl>
</PropertyGroup>

<ItemGroup>
Expand Down
4 changes: 4 additions & 0 deletions Program.cs
Original file line number Diff line number Diff line change
Expand Up @@ -17,6 +17,10 @@ public class Program
public static CommandLineOptions CommandLineOptions { get; private set; }
public static void Main(string[] args)
{
var assemblyVersion = System.Reflection.Assembly.GetExecutingAssembly().GetName().Version;
System.Console.WriteLine($"Prometheus SQL Server Exporter v{assemblyVersion.ToString()}");
System.Console.WriteLine($"Licensed under Apache License 2.0\n");

string configFile = null;
for (int i = 0; (i < args.Length - 1 && configFile == null); i++)
if (args[i].ToLower() == "-c")
Expand Down
Loading

0 comments on commit 42beb55

Please sign in to comment.