Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
15 changes: 15 additions & 0 deletions readme.md
Original file line number Diff line number Diff line change
@@ -1,9 +1,24 @@
![](prometheus-net-logo.png)
# prometheus-net.AspNet
A plugin for the [prometheus-net](https://github.com/prometheus-net/prometheus-net) package, exposing ASP.NET full framework metrics. Basic Auth can also be enabled for the endpoint. Can also collect metrics on SQL database calls if using Entity Framework 6.

![](screenshot1.png)
![](screenshot2.png)

# Movidesk pack and publish
```ps1
# version <= 1.1.0
https://www.youtube.com/watch?v=xPm9FsptuE8
nuget pack
nuget push movidesk-prometheus-net.AspNet.x.x.x.nupkg personal_github_token -Source github

# version >= 1.1.1
cd .\src\Prometheus.AspNet\
dotnet build --configuration Release
dotnet pack --configuration Release
dotnet nuget push .\bin\Release\movidesk-prometheus-net.AspNet.x.x.x.nupkg --api-key ghp_xxxxxxxx --source github
```

# Installation

Add the package from [nuget](https://www.nuget.org/packages/prometheus-net.AspNet):
Expand Down
95 changes: 77 additions & 18 deletions src/Prometheus.AspNet/Classes/PrometheusHttpRequestModule.cs
Original file line number Diff line number Diff line change
Expand Up @@ -9,24 +9,83 @@ namespace Prometheus.AspNet
/// </summary>
public class PrometheusHttpRequestModule : IHttpModule
{
private static readonly Counter _globalExceptions = Metrics
.CreateCounter("global_exceptions", "Number of global exceptions.");
private static Counter _globalExceptions;
private static Gauge _httpRequestsInProgress;
private static Gauge _httpRequestsTotal;
private static Histogram _httpRequestsDuration;
private const string _timerKey = "PrometheusHttpRequestModule.Timer";

private static readonly Gauge _httpRequestsInProgress = Metrics
.CreateGauge("http_requests_in_progress", "The number of HTTP requests currently in progress");
/// <summary>
///
/// </summary>
public PrometheusHttpRequestModule()
{
if (_globalExceptions == null)
{
_globalExceptions = Metrics
.CreateCounter("global_exceptions", "Number of global exceptions.");
}

private static readonly Gauge _httpRequestsTotal = Metrics
.CreateGauge("http_requests_received_total", "Provides the count of HTTP requests that have been processed by this app",
new GaugeConfiguration { LabelNames = new[] { "code", "method", "controller", "action" } });
if (_httpRequestsInProgress == null)
{
_httpRequestsInProgress = Metrics
.CreateGauge("http_requests_in_progress", "The number of HTTP requests currently in progress");
}

private static readonly Histogram _httpRequestsDuration = Metrics
.CreateHistogram("http_request_duration_seconds", "The duration of HTTP requests processed by this app.",
new HistogramConfiguration { LabelNames = new[] { "code", "method", "controller", "action" } });
if (_httpRequestsTotal == null)
{
_httpRequestsTotal = Metrics
.CreateGauge("http_requests_received_total", "Provides the count of HTTP requests that have been processed by this app",
new GaugeConfiguration {LabelNames = new[] {"code", "method", "controller", "action"}});
}

private const string _timerKey = "PrometheusHttpRequestModule.Timer";
if (_httpRequestsDuration == null)
{
_httpRequestsDuration = Metrics
.CreateHistogram("http_request_duration_seconds", "The duration of HTTP requests processed by this app.",
new HistogramConfiguration {LabelNames = new[] {"code", "method", "controller", "action"}});
}
}

/// <summary>
///
/// </summary>
/// <param name="bucektSize"></param>
public PrometheusHttpRequestModule(double[] bucektSize)
Copy link
Owner

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

If you change this constructor so that it looks like this:

public PrometheusHttpRequestModule(double[] bucektSize = null)

Does that mean you don't need the other constructor at all?

Copy link
Owner

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

(Also, please fix the spelling mistake :)

{
if (_globalExceptions == null)
{
_globalExceptions = Metrics
.CreateCounter("global_exceptions", "Number of global exceptions.");
}

if (_httpRequestsInProgress == null)
{
_httpRequestsInProgress = Metrics
.CreateGauge("http_requests_in_progress", "The number of HTTP requests currently in progress");
}

if (_httpRequestsTotal == null)
{
_httpRequestsTotal = Metrics
.CreateGauge("http_requests_received_total", "Provides the count of HTTP requests that have been processed by this app",
new GaugeConfiguration {LabelNames = new[] {"code", "method", "controller", "action"}});
}

if (_httpRequestsDuration == null)
{
_httpRequestsDuration = Metrics
.CreateHistogram("http_request_duration_seconds", "The duration of HTTP requests processed by this app.",
new HistogramConfiguration
{
LabelNames = new[] {"code", "method", "controller", "action"},
Buckets = bucektSize
});
}
}

/// <summary>
///
///
/// </summary>
/// <param name="httpApp"></param>
public void Init(HttpApplication httpApp)
Expand All @@ -42,12 +101,12 @@ private void HttpApp_Error(object sender, EventArgs e)
}

/// <summary>
///
///
/// </summary>
/// <param name="sender"></param>
/// <param name="e"></param>
// Record the time of the begin request event.
public void OnBeginRequest(Object sender, EventArgs e)
private void OnBeginRequest(Object sender, EventArgs e)
Copy link
Owner

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Any reason to change this to private?

{
_httpRequestsInProgress.Inc();

Expand All @@ -58,11 +117,11 @@ public void OnBeginRequest(Object sender, EventArgs e)
}

/// <summary>
///
///
/// </summary>
/// <param name="sender"></param>
/// <param name="e"></param>
public void OnEndRequest(Object sender, EventArgs e)
private void OnEndRequest(Object sender, EventArgs e)
{
_httpRequestsInProgress.Dec();

Expand Down Expand Up @@ -98,9 +157,9 @@ public void OnEndRequest(Object sender, EventArgs e)
}

/// <summary>
///
///
/// </summary>
public void Dispose() { /* Not needed */ }
}

}
}
77 changes: 25 additions & 52 deletions src/Prometheus.AspNet/Prometheus.AspNet.csproj
Original file line number Diff line number Diff line change
@@ -1,70 +1,43 @@
<?xml version="1.0" encoding="utf-8"?>
<Project ToolsVersion="15.0" xmlns="http://schemas.microsoft.com/developer/msbuild/2003">
<Import Project="$(MSBuildExtensionsPath)\$(MSBuildToolsVersion)\Microsoft.Common.props" Condition="Exists('$(MSBuildExtensionsPath)\$(MSBuildToolsVersion)\Microsoft.Common.props')" />
<Project Sdk="Microsoft.NET.Sdk">
<PropertyGroup>
<Configuration Condition=" '$(Configuration)' == '' ">Debug</Configuration>
<Platform Condition=" '$(Platform)' == '' ">AnyCPU</Platform>
<ProjectGuid>{D47CE1F2-949B-452E-B446-6A977C64F105}</ProjectGuid>
<OutputType>Library</OutputType>
<AppDesignerFolder>Properties</AppDesignerFolder>
<RootNamespace>prometheus.AspNet</RootNamespace>
<AssemblyName>prometheus.AspNet</AssemblyName>
<TargetFrameworkVersion>v4.7.1</TargetFrameworkVersion>
<FileAlignment>512</FileAlignment>
<Deterministic>true</Deterministic>
<LangVersion>default</LangVersion>
<AssemblyTitle>Prometheus.AspNet</AssemblyTitle>
<Product>Prometheus.AspNet</Product>
<Description>Exposes prometheus metrics inside full framework asp.net applications using WebApi</Description>
<Copyright>Copyright 2022</Copyright>
<Version>4.2.0</Version>
<AssemblyVersion>4.2.0.0</AssemblyVersion>
<FileVersion>4.2.0.0</FileVersion>
<OutputPath>bin\$(Configuration)\</OutputPath>
<DocumentationFile>bin\$(Configuration)\prometheus.AspNet.xml</DocumentationFile>
<Authors>Lachlan Barclay</Authors>
<PackageIcon>prometheus-net-logo.png</PackageIcon>
<PackageId>movidesk-prometheus-net.AspNet</PackageId>
<RepositoryUrl>https://github.com/movidesk/prometheus-net.AspNet</RepositoryUrl>
<PackageTags>movidesk-prometheus-net.AspNet</PackageTags>
<TargetFrameworks>net471;net472;net48;net481</TargetFrameworks>
<PackageVersion>4.2.0</PackageVersion>
</PropertyGroup>
<PropertyGroup Condition=" '$(Configuration)|$(Platform)' == 'Debug|AnyCPU' ">
<DebugSymbols>true</DebugSymbols>
<DebugType>full</DebugType>
<Optimize>false</Optimize>
<OutputPath>bin\Debug\</OutputPath>
<DefineConstants>DEBUG;TRACE</DefineConstants>
<ErrorReport>prompt</ErrorReport>
<WarningLevel>4</WarningLevel>
<DocumentationFile>bin\Debug\prometheus.AspNet.xml</DocumentationFile>
</PropertyGroup>
<PropertyGroup Condition=" '$(Configuration)|$(Platform)' == 'Release|AnyCPU' ">
<DebugType>pdbonly</DebugType>
<Optimize>true</Optimize>
<OutputPath>bin\Release\</OutputPath>
<DefineConstants>TRACE</DefineConstants>
<ErrorReport>prompt</ErrorReport>
<WarningLevel>4</WarningLevel>
<DocumentationFile>bin\Release\prometheus.AspNet.xml</DocumentationFile>
<DebugType>portable</DebugType>
</PropertyGroup>
<ItemGroup>
<Reference Include="Newtonsoft.Json, Version=4.5.0.0, Culture=neutral, PublicKeyToken=30ad4fe6b2a6aeed, processorArchitecture=MSIL">
<HintPath>..\..\packages\Newtonsoft.Json.4.5.11\lib\net40\Newtonsoft.Json.dll</HintPath>
</Reference>
<Reference Include="Prometheus.NetStandard, Version=3.0.0.0, Culture=neutral, PublicKeyToken=a243e9817ba9d559, processorArchitecture=MSIL">
<HintPath>..\..\packages\prometheus-net.3.1.0\lib\netstandard2.0\Prometheus.NetStandard.dll</HintPath>
</Reference>
<Reference Include="System" />
<Reference Include="System.Core" />
<Reference Include="System.Net.Http.Formatting, Version=5.0.0.0, Culture=neutral, PublicKeyToken=31bf3856ad364e35, processorArchitecture=MSIL">
<HintPath>..\..\packages\Microsoft.AspNet.WebApi.Client.5.0.0\lib\net45\System.Net.Http.Formatting.dll</HintPath>
</Reference>
<Reference Include="mscorlib" />
<Reference Include="System.Web" />
<Reference Include="System.Web.Http, Version=5.0.0.0, Culture=neutral, PublicKeyToken=31bf3856ad364e35, processorArchitecture=MSIL">
<HintPath>..\..\packages\Microsoft.AspNet.WebApi.Core.5.0.0\lib\net45\System.Web.Http.dll</HintPath>
</Reference>
<Reference Include="System.Xml.Linq" />
<Reference Include="System.Data.DataSetExtensions" />
<Reference Include="Microsoft.CSharp" />
<Reference Include="System.Data" />
<Reference Include="System.Net.Http" />
<Reference Include="System.Xml" />
<None Include="../../prometheus-net-logo.png" Pack="true" PackagePath="\" />
</ItemGroup>
<ItemGroup>
<Compile Include="Classes\PrometheusHttpRequestModule.cs" />
<Compile Include="Classes\PerformanceMonitorModule.cs" />
<Compile Include="Classes\PrometheusConfig.cs" />
<Compile Include="Classes\PrometheusMetricsController.cs" />
<Compile Include="Properties\AssemblyInfo.cs" />
<PackageReference Include="Microsoft.AspNet.WebApi.Client" Version="5.2.9" />
<PackageReference Include="Microsoft.AspNet.WebApi.Core" Version="5.2.9" />
<PackageReference Include="Newtonsoft.Json" Version="13.0.1" />
<PackageReference Include="prometheus-net" Version="4.2.0" />
</ItemGroup>
<ItemGroup>
<None Include="packages.config" />
<None Include="prometheus.AspNet.nuspec" />
</ItemGroup>
<Import Project="$(MSBuildToolsPath)\Microsoft.CSharp.targets" />
</Project>
25 changes: 0 additions & 25 deletions src/Prometheus.AspNet/Properties/AssemblyInfo.cs
Original file line number Diff line number Diff line change
Expand Up @@ -2,35 +2,10 @@
using System.Runtime.CompilerServices;
using System.Runtime.InteropServices;

// General Information about an assembly is controlled through the following
// set of attributes. Change these attribute values to modify the information
// associated with an assembly.
[assembly: AssemblyTitle("Prometheus.AspNet")]
[assembly: AssemblyDescription("Exports prometheus web metrics")]
[assembly: AssemblyConfiguration("")]
[assembly: AssemblyCompany("")]
[assembly: AssemblyProduct("Prometheus.AspNet")]
[assembly: AssemblyCopyright("Copyright © 2020")]
[assembly: AssemblyTrademark("")]
[assembly: AssemblyCulture("")]

// Setting ComVisible to false makes the types in this assembly not visible
// to COM components. If you need to access a type in this assembly from
// COM, set the ComVisible attribute to true on that type.
[assembly: ComVisible(false)]

// The following GUID is for the ID of the typelib if this project is exposed to COM
[assembly: Guid("d47ce1f2-949b-452e-b446-6a977c64f105")]

// Version information for an assembly consists of the following four values:
//
// Major Version
// Minor Version
// Build Number
// Revision
//
// You can specify all the values or you can default the Build and Revision Numbers
// by using the '*' as shown below:
// [assembly: AssemblyVersion("1.0.*")]
[assembly: AssemblyVersion("1.0.0.0")]
[assembly: AssemblyFileVersion("1.0.0.0")]
7 changes: 0 additions & 7 deletions src/Prometheus.AspNet/packages.config

This file was deleted.

16 changes: 0 additions & 16 deletions src/Prometheus.AspNet/prometheus.AspNet.nuspec

This file was deleted.

12 changes: 6 additions & 6 deletions src/Prometheus.EntityFramework/App.config
Original file line number Diff line number Diff line change
@@ -1,17 +1,17 @@
<?xml version="1.0" encoding="utf-8"?>
<?xml version="1.0" encoding="utf-8"?>
<configuration>
<configSections>
<!-- For more information on Entity Framework configuration, visit http://go.microsoft.com/fwlink/?LinkID=237468 -->
<section name="entityFramework" type="System.Data.Entity.Internal.ConfigFile.EntityFrameworkSection, EntityFramework, Version=6.0.0.0, Culture=neutral, PublicKeyToken=b77a5c561934e089" requirePermission="false"/>

<section name="entityFramework" type="System.Data.Entity.Internal.ConfigFile.EntityFrameworkSection, EntityFramework, Version=6.0.0.0, Culture=neutral, PublicKeyToken=b77a5c561934e089" requirePermission="false" />
</configSections>
<entityFramework>
<defaultConnectionFactory type="System.Data.Entity.Infrastructure.LocalDbConnectionFactory, EntityFramework">
<parameters>
<parameter value="v13.0"/>
<parameter value="v13.0" />
</parameters>
</defaultConnectionFactory>
<providers>
<provider invariantName="System.Data.SqlClient" type="System.Data.Entity.SqlServer.SqlProviderServices, EntityFramework.SqlServer"/>
<provider invariantName="System.Data.SqlClient" type="System.Data.Entity.SqlServer.SqlProviderServices, EntityFramework.SqlServer" />
</providers>
</entityFramework>
<startup><supportedRuntime version="v4.0" sku=".NETFramework,Version=v4.6.1"/></startup></configuration>
<startup><supportedRuntime version="v4.0" sku=".NETFramework,Version=v4.6.1" /></startup></configuration>
Loading