Skip to content

Commit 2b05659

Browse files
committed
init
0 parents  commit 2b05659

File tree

10 files changed

+295
-0
lines changed

10 files changed

+295
-0
lines changed

.gitattributes

Lines changed: 22 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,22 @@
1+
# Auto detect text files and perform LF normalization
2+
* text=auto
3+
4+
# Custom for Visual Studio
5+
*.cs diff=csharp
6+
*.sln merge=union
7+
*.csproj merge=union
8+
*.vbproj merge=union
9+
*.fsproj merge=union
10+
*.dbproj merge=union
11+
12+
# Standard to msysgit
13+
*.doc diff=astextplain
14+
*.DOC diff=astextplain
15+
*.docx diff=astextplain
16+
*.DOCX diff=astextplain
17+
*.dot diff=astextplain
18+
*.DOT diff=astextplain
19+
*.pdf diff=astextplain
20+
*.PDF diff=astextplain
21+
*.rtf diff=astextplain
22+
*.RTF diff=astextplain

.gitignore

Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,10 @@
1+
*.nupkg
2+
*.user
3+
*.suo
4+
*.exe
5+
6+
.vs
7+
bin
8+
obj
9+
packages
10+
.idea

LICENSE

Lines changed: 22 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,22 @@
1+
The MIT License (MIT)
2+
3+
Copyright (c) 2018 Sergey Kuznetsov
4+
5+
Permission is hereby granted, free of charge, to any person obtaining a copy
6+
of this software and associated documentation files (the "Software"), to deal
7+
in the Software without restriction, including without limitation the rights
8+
to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
9+
copies of the Software, and to permit persons to whom the Software is
10+
furnished to do so, subject to the following conditions:
11+
12+
The above copyright notice and this permission notice shall be included in all
13+
copies or substantial portions of the Software.
14+
15+
THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
16+
IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
17+
FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
18+
AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
19+
LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
20+
OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
21+
SOFTWARE.
22+

Prometheus.Client.AspNetCore.sln

Lines changed: 36 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,36 @@
1+
2+
Microsoft Visual Studio Solution File, Format Version 12.00
3+
# Visual Studio 15
4+
VisualStudioVersion = 15.0.26228.4
5+
MinimumVisualStudioVersion = 10.0.40219.1
6+
Project("{2150E333-8FDC-42A3-9474-1A3956D46DE8}") = "src", "src", "{D994EEE1-3786-4FDF-8A49-76D8B9819A29}"
7+
EndProject
8+
Project("{FAE04EC0-301F-11D3-BF4B-00C04F79EFBC}") = "Prometheus.Client.AspNetCore", "src\Prometheus.Client.AspNetCore\Prometheus.Client.AspNetCore.csproj", "{B70120FA-53E2-4290-8DF2-BD3AF7139DCB}"
9+
EndProject
10+
Project("{2150E333-8FDC-42A3-9474-1A3956D46DE8}") = "Solution Items", "Solution Items", "{FA0CD8D2-E9AE-4518-B989-17901C5928B0}"
11+
ProjectSection(SolutionItems) = preProject
12+
.gitattributes = .gitattributes
13+
.gitignore = .gitignore
14+
appveyor.yml = appveyor.yml
15+
LICENSE = LICENSE
16+
README.md = README.md
17+
EndProjectSection
18+
EndProject
19+
Global
20+
GlobalSection(SolutionConfigurationPlatforms) = preSolution
21+
Debug|Any CPU = Debug|Any CPU
22+
Release|Any CPU = Release|Any CPU
23+
EndGlobalSection
24+
GlobalSection(ProjectConfigurationPlatforms) = postSolution
25+
{B70120FA-53E2-4290-8DF2-BD3AF7139DCB}.Debug|Any CPU.ActiveCfg = Debug|Any CPU
26+
{B70120FA-53E2-4290-8DF2-BD3AF7139DCB}.Debug|Any CPU.Build.0 = Debug|Any CPU
27+
{B70120FA-53E2-4290-8DF2-BD3AF7139DCB}.Release|Any CPU.ActiveCfg = Release|Any CPU
28+
{B70120FA-53E2-4290-8DF2-BD3AF7139DCB}.Release|Any CPU.Build.0 = Release|Any CPU
29+
EndGlobalSection
30+
GlobalSection(SolutionProperties) = preSolution
31+
HideSolutionNode = FALSE
32+
EndGlobalSection
33+
GlobalSection(NestedProjects) = preSolution
34+
{B70120FA-53E2-4290-8DF2-BD3AF7139DCB} = {D994EEE1-3786-4FDF-8A49-76D8B9819A29}
35+
EndGlobalSection
36+
EndGlobal

README.md

Lines changed: 23 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,23 @@
1+
# Prometheus.Client.AspNetCore
2+
3+
[![NuGet Badge](https://buildstats.info/nuget/Prometheus.Client.AspNetCore)](https://www.nuget.org/packages/Prometheus.Client.AspNetCore/)
4+
[![License MIT](https://img.shields.io/badge/license-MIT-green.svg)](https://opensource.org/licenses/MIT)
5+
6+
7+
Extension for [Prometheus.Client](https://github.com/PrometheusClientNet/Prometheus.Client)
8+
9+
10+
#### Installation:
11+
12+
dotnet add package Prometheus.Client.AspNetCore
13+
14+
#### Quik start:
15+
16+
```csharp
17+
18+
public void Configure(IApplicationBuilder app, IHostingEnvironment env, ILoggerFactory loggerFactory, IApplicationLifetime appLifetime)
19+
{
20+
app.UsePrometheusServer();
21+
}
22+
23+
```

appveyor.yml

Lines changed: 13 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,13 @@
1+
version: '{build}-{branch}'
2+
image: Visual Studio 2017
3+
build_script:
4+
- cmd: dotnet restore src\Prometheus.Client.AspNetCore && dotnet pack src\Prometheus.Client.AspNetCore -c Release --include-symbols
5+
artifacts:
6+
- path: '**\*.nupkg'
7+
name: All Nupkg
8+
deploy:
9+
- provider: NuGet
10+
api_key:
11+
secure: vukCl5k+S4fJtqeYNiB9ufy/NUCw+60cPcqnPoqvykBjSo8Ub8QtEfZNN9qvj5Jh
12+
on:
13+
appveyor_repo_tag: true
Lines changed: 25 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,25 @@
1+
using System.Collections.Generic;
2+
using Prometheus.Client.Collectors;
3+
using System.Runtime.InteropServices;
4+
5+
namespace Prometheus.Client.AspNetCore
6+
{
7+
/// <summary>
8+
/// All default Collector
9+
/// </summary>
10+
public static class DefaultCollectors
11+
{
12+
/// <summary>
13+
/// Get default Collector
14+
/// </summary>
15+
public static IEnumerable<IOnDemandCollector> Get(MetricFactory metricFactory)
16+
{
17+
yield return new DotNetStatsCollector(metricFactory);
18+
19+
var isWindows = RuntimeInformation.IsOSPlatform(OSPlatform.Windows);
20+
if (isWindows)
21+
yield return new WindowsDotNetStatsCollector(metricFactory);
22+
23+
}
24+
}
25+
}
Lines changed: 33 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,33 @@
1+
<Project Sdk="Microsoft.NET.Sdk">
2+
<PropertyGroup>
3+
<Description>An Owin handler for the Prometheus.Client</Description>
4+
<Copyright>2018 © Sergey Kuznetsov</Copyright>
5+
<AssemblyTitle>Prometheus.Client.Owin</AssemblyTitle>
6+
<VersionPrefix>1.5.0</VersionPrefix>
7+
<Authors>Sergey Kuznetsov</Authors>
8+
<TargetFrameworks>netstandard1.3;netstandard2.0</TargetFrameworks>
9+
<AssemblyName>Prometheus.Client.AspNetCore</AssemblyName>
10+
<PackageId>Prometheus.Client.AspNetCore</PackageId>
11+
<PackageTags>prometheus;metrics</PackageTags>
12+
<PackageIconUrl>https://image.ibb.co/k4Sc0k/prometheus.png</PackageIconUrl>
13+
<PackageProjectUrl>https://github.com/PrometheusClientNet/Prometheus.Client.AspNetCore</PackageProjectUrl>
14+
<PackageLicenseUrl>https://opensource.org/licenses/MIT</PackageLicenseUrl>
15+
<RepositoryType>git</RepositoryType>
16+
<RepositoryUrl>https://github.com/PrometheusClientNet/Prometheus.Client.AspNetCore</RepositoryUrl>
17+
<TreatWarningsAsErrors>true</TreatWarningsAsErrors>
18+
<GenerateDocumentationFile>true</GenerateDocumentationFile>
19+
</PropertyGroup>
20+
<ItemGroup>
21+
<PackageReference Include="Prometheus.Client" Version="1.5.0" />
22+
</ItemGroup>
23+
<ItemGroup Condition=" '$(TargetFramework)' == 'netstandard1.3'">
24+
<PackageReference Include="Microsoft.AspNetCore.Http.Abstractions">
25+
<Version>1.1.0</Version>
26+
</PackageReference>
27+
</ItemGroup>
28+
<ItemGroup Condition="'$(TargetFramework)' == 'netstandard2.0'">
29+
<PackageReference Include="Microsoft.AspNetCore.Http.Abstractions">
30+
<Version>2.0.0</Version>
31+
</PackageReference>
32+
</ItemGroup>
33+
</Project>
Lines changed: 80 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,80 @@
1+
using System;
2+
using System.Threading.Tasks;
3+
using Prometheus.Client.Collectors;
4+
using Microsoft.AspNetCore.Builder;
5+
6+
namespace Prometheus.Client.AspNetCore
7+
{
8+
/// <summary>
9+
/// PrometheusExtensions
10+
/// </summary>
11+
public static class PrometheusExtensions
12+
{
13+
/// <summary>
14+
/// Add PrometheusServer request execution pipeline.
15+
/// </summary>
16+
public static IApplicationBuilder UsePrometheusServer(this IApplicationBuilder app)
17+
{
18+
return UsePrometheusServer(app, null);
19+
}
20+
21+
/// <summary>
22+
/// Add PrometheusServer request execution pipeline.
23+
/// </summary>
24+
public static IApplicationBuilder UsePrometheusServer(this IApplicationBuilder app, Action<PrometheusOptions> setupOptions)
25+
{
26+
var options = new PrometheusOptions();
27+
setupOptions?.Invoke(options);
28+
29+
if (app == null)
30+
throw new ArgumentNullException(nameof(app));
31+
32+
if (options == null)
33+
throw new ArgumentNullException(nameof(options));
34+
35+
if (!options.MapPath.StartsWith("/"))
36+
throw new ArgumentException($"MapPath '{options.MapPath}' should start with '/'");
37+
38+
RegisterCollectors(options);
39+
40+
app.Map(options.MapPath, coreapp =>
41+
{
42+
coreapp.Run(async context =>
43+
{
44+
var req = context.Request;
45+
var response = context.Response;
46+
47+
req.Headers.TryGetValue("Accept", out var acceptHeaders);
48+
var contentType = ScrapeHandler.GetContentType(acceptHeaders);
49+
50+
response.ContentType = contentType;
51+
52+
using (var outputStream = response.Body)
53+
{
54+
var collected = options.CollectorRegistryInstance.CollectAll();
55+
ScrapeHandler.ProcessScrapeRequest(collected, contentType, outputStream);
56+
}
57+
58+
await Task.FromResult(0).ConfigureAwait(false);
59+
});
60+
});
61+
62+
return app;
63+
}
64+
65+
66+
private static void RegisterCollectors(PrometheusOptions options)
67+
{
68+
if (options.UseDefaultCollectors)
69+
{
70+
var metricFactory = Metrics.DefaultFactory;
71+
if (options.CollectorRegistryInstance != CollectorRegistry.Instance)
72+
metricFactory = new MetricFactory(options.CollectorRegistryInstance);
73+
74+
options.Collectors.AddRange(DefaultCollectors.Get(metricFactory));
75+
}
76+
77+
options.CollectorRegistryInstance.RegisterOnDemandCollectors(options.Collectors);
78+
}
79+
}
80+
}
Lines changed: 31 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,31 @@
1+
using System.Collections.Generic;
2+
using Prometheus.Client.Collectors;
3+
4+
namespace Prometheus.Client.AspNetCore
5+
{
6+
/// <summary>
7+
/// Options for Prometheus
8+
/// </summary>
9+
public class PrometheusOptions
10+
{
11+
/// <summary>
12+
/// Url, default = "/metrics"
13+
/// </summary>
14+
public string MapPath { get; set; } = "/metrics";
15+
16+
/// <summary>
17+
/// CollectorRegistry intance
18+
/// </summary>
19+
public ICollectorRegistry CollectorRegistryInstance { get; set; } = CollectorRegistry.Instance;
20+
21+
/// <summary>
22+
/// IOnDemandCollectors
23+
/// </summary>
24+
public List<IOnDemandCollector> Collectors { get; set; } = new List<IOnDemandCollector>();
25+
26+
/// <summary>
27+
/// Use default collectors
28+
/// </summary>
29+
public bool UseDefaultCollectors { get; set; } = true;
30+
}
31+
}

0 commit comments

Comments
 (0)