{"id":13430172,"url":"https://github.com/nexogen-international/Nexogen.Libraries.Metrics","last_synced_at":"2025-03-16T05:30:35.392Z","repository":{"id":66577328,"uuid":"100702987","full_name":"nexogen-international/Nexogen.Libraries.Metrics","owner":"nexogen-international","description":"Library for collecting application metrics in .NET and exporting them to Prometheus","archived":false,"fork":false,"pushed_at":"2025-02-12T15:11:15.000Z","size":147,"stargazers_count":61,"open_issues_count":11,"forks_count":9,"subscribers_count":6,"default_branch":"master","last_synced_at":"2025-02-12T15:29:00.936Z","etag":null,"topics":["histogram","metrics","nuget","prometheus"],"latest_commit_sha":null,"homepage":"","language":"C#","has_issues":true,"has_wiki":null,"has_pages":null,"mirror_url":null,"source_name":null,"license":"mit","status":null,"scm":"git","pull_requests_enabled":true,"icon_url":"https://github.com/nexogen-international.png","metadata":{"files":{"readme":"README.md","changelog":null,"contributing":null,"funding":null,"license":"LICENSE","code_of_conduct":null,"threat_model":null,"audit":null,"citation":null,"codeowners":null,"security":null,"support":null,"governance":null,"roadmap":null,"authors":null}},"created_at":"2017-08-18T10:48:18.000Z","updated_at":"2025-02-12T14:06:04.000Z","dependencies_parsed_at":"2023-03-11T00:07:35.301Z","dependency_job_id":null,"html_url":"https://github.com/nexogen-international/Nexogen.Libraries.Metrics","commit_stats":null,"previous_names":[],"tags_count":22,"template":false,"template_full_name":null,"repository_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/nexogen-international%2FNexogen.Libraries.Metrics","tags_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/nexogen-international%2FNexogen.Libraries.Metrics/tags","releases_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/nexogen-international%2FNexogen.Libraries.Metrics/releases","manifests_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/nexogen-international%2FNexogen.Libraries.Metrics/manifests","owner_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/owners/nexogen-international","download_url":"https://codeload.github.com/nexogen-international/Nexogen.Libraries.Metrics/tar.gz/refs/heads/master","host":{"name":"GitHub","url":"https://github.com","kind":"github","repositories_count":243830912,"owners_count":20354848,"icon_url":"https://github.com/github.png","version":null,"created_at":"2022-05-30T11:31:42.601Z","updated_at":"2022-07-04T15:15:14.044Z","host_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub","repositories_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories","repository_names_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repository_names","owners_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/owners"}},"keywords":["histogram","metrics","nuget","prometheus"],"created_at":"2024-07-31T02:00:50.777Z","updated_at":"2025-03-16T05:30:34.952Z","avatar_url":"https://github.com/nexogen-international.png","language":"C#","funding_links":[],"categories":["Frameworks, Libraries and Tools","框架, 库和工具","Code Analysis and Metrics"],"sub_categories":["Code Analysis and Metrics","代码分析和指标"],"readme":"# Nexogen.Libraries.Metrics\n\nLibrary for collecting application metrics in .Net and exporting them to [Prometheus](https://prometheus.io/)\n\n[![Build Status](https://travis-ci.org/nexogen-international/Nexogen.Libraries.Metrics.svg?branch=master)](https://travis-ci.org/nexogen-international/Nexogen.Libraries.Metrics)\n[![Build status](https://ci.appveyor.com/api/projects/status/cijwwapl9lva1qko?svg=true)](https://ci.appveyor.com/project/ahoka/nexogen-libraries-metrics)\n[![GitHub license](https://img.shields.io/badge/license-MIT-blue.svg)](https://raw.githubusercontent.com/nexogen-international/Nexogen.Libraries.Metrics/master/LICENSE)\n[![NuGet](https://img.shields.io/nuget/v/Nexogen.Libraries.Metrics.svg)](https://www.nuget.org/packages/Nexogen.Libraries.Metrics.Prometheus/)\n[![NuGet](https://img.shields.io/nuget/dt/Nexogen.Libraries.Metrics.svg)](https://github.com/nexogen-international/Nexogen.Libraries.Metrics)\n[![GitHub issues](https://img.shields.io/github/issues/nexogen-international/Nexogen.Libraries.Metrics.svg)](https://github.com/nexogen-international/Nexogen.Libraries.Metrics/issues)\n[![GitHub stars](https://img.shields.io/github/stars/nexogen-international/Nexogen.Libraries.Metrics.svg)](https://github.com/nexogen-international/Nexogen.Libraries.Metrics/stargazers)\n\n# API Reference\n\n[Nexogen.Libraries.Metrics API Reference](https://github.com/nexogen-international/Nexogen.Libraries.Metrics/wiki)\n\n# Updating from version 2.6.0 or earlier\n\nThe default Prometheus metrics registration behavior in ASP.NET Core applications has changed.\nThe request metrics collection is no longer enabled by default. This means that you need to add code to enable it. See the relevant section below for code samples.\nExplicitly defined metrics are not affected in any way.\n\n# Installation\n\n```sh\ndotnet add package Nexogen.Libraries.Metrics.Prometheus\ndotnet add package Nexogen.Libraries.Metrics.Extensions\n```\n\nYou can use an interface only nuget when writing libraries or when you want to use Metrics through dependency injection.\n\n```sh\ndotnet add package Nexogen.Libraries.Metrics\n```\n\nFor exporting metrics you currently have to use ASP.NET Core.\n\n```sh\ndotnet add package Nexogen.Libraries.Metrics.Prometheus.AspCore\n```\n\nOr you can use a push gateway when measuring batch processes.\n\n```sh\ndotnet add package Nexogen.Libraries.Metrics.Prometheus.PushGateway\n```\n\n# Example usage - Measurements\n\n## Counters\n\nCounters can only increment, so they are most useful for counting things, like calls to API endpoints or backend services.\n\n```cs\nIMetrics metrics = new PrometheusMetrics();\n\nICounter counter = metrics.Counter()\n    .Name(\"nexogen_sort_calls_total\")\n    .Help(\"Total calls to sort routine.\")\n    .Register();\n\ncounter.Increment();\n```\n\n## Gauges\n\nGauges can take any value, so they are the most versatile metric type available. You can even measure durations or dates with them!\n\n```cs\nIGauge gauge = metrics.Gauge()\n    .Name(\"nexogen_sorted_items_count_last\")\n    .Help(\"The last count of the sorted items.\")\n    .Register();\n\ngauge.Value = items.Length;\n\ngauge.Increment();\ngauge.Decrement(10.1);           \n```\n## Histograms\n\nHistograms are a trade off between measuring resolution and precision. With histograms you can avoid aliasing errors from Prometheus's scrape interval, but lose granularity. Histograms also need to have their buckets defined before use and we provide sevaral bucket generators to make it easy.\n\n```cs\n\nIHistogram histogram = metrics.Histogram()\n    .LinearBuckets(0.01, 0.01, 100)\n    .Name(\"nexogen_sort_time_seconds\")\n    .Help(\"Time taken for sort in seconds.\")\n    .Register();\n\nvar sw = Stopwatch.StartNew();\nArray.Sort(items);\nhistogram.Observe(sw.Elapsed.TotalSeconds);\n\n```\n\n## Extensions\n\nWe provide an [Extensions](https://www.nuget.org/packages/Nexogen.Libraries.Metrics.Extensions) library for making common measurements easy.\n\n```cs\nusing (histogram.Timer())\n{\n    Array.Sort(items);\n}\n\ngauge.SetToCurrentTime();\n\ngauge.TrackInProgress(() =\u003e Array.Sort(items));\n```\n\n## ASP.NET Core metrics collection\n\nThere is a way to automatically collect metrics about the ASP.NET Core requests. This needs to be enabled when registering to the application. \n\n```cs\npublic void Configure(IApplicationBuilder app, IHostingEnvironment env)\n{\n    app.UsePrometheus(options =\u003e options.CollectHttpMetrics());\n}\n```\n\nThe `options.CollectHttpMetrics()` registers automatic request metric collection, which was implicitly registered in versions prior to version 3.0.0.\n\n# Example usage - Exposing metrics\n\n## Asp.Net Core\n\nThe collected metrics can be exposed via ASP.NET Core middleware. To do this you need to register the feature during configuration.\n\n```cs\npublic void Configure(IApplicationBuilder app, IHostingEnvironment env)\n{\n    app.UsePrometheus();\n}\n```\n\n## Standalone server\n\nThere is a standalone server if you don't want to use ASP.NET Core just to expose your metrics.\n\n```sh\ndotnet add package Nexogen.Libraries.Metrics.Prometheus.Standalone\n```\n\n```cs\npublic void ConfigureServices(IServiceCollection services)\n{\n    services.AddPrometheusStandalone(Configuration.GetSection(\"Prometheus\"));\n}\n```\n\nor\n\n```cs \nvar metrics = new PrometheusMetrics();\nawait new PrometheusServer(new PrometheusServerOptions {Port = 9100}, metrics, loggerFactory).StartAsync();\n```\n\n## gRPC\n\nWe provide gRPC interceptors for capturing metrics.\n\n```sh\ndotnet add package Nexogen.Libraries.Metrics.Prometheus.Grpc\n```\n\nFor gRPC servers:\n\n```csharp\nservices.AddSingleton\u003cIGrpcServerMetrics, GrpcServerMetrics\u003e()\n        .AddGrpc(options =\u003e options.Interceptors.Add\u003cServerMetricsInterceptor\u003e());\n```\n\nFor gRPC clients:\n\n```csharp\nservices.AddSingleton\u003cIGrpcClientMetrics, GrpcClientMetrics\u003e()\n        .AddGrpcClient\u003cT\u003e((provider, options) =\u003e options.Interceptors.Add(new ClientMetricsInterceptor(provider.GetRequiredService\u003cIGrpcClientMetrics\u003e())));\n```\n","project_url":"https://awesome.ecosyste.ms/api/v1/projects/github.com%2Fnexogen-international%2FNexogen.Libraries.Metrics","html_url":"https://awesome.ecosyste.ms/projects/github.com%2Fnexogen-international%2FNexogen.Libraries.Metrics","lists_url":"https://awesome.ecosyste.ms/api/v1/projects/github.com%2Fnexogen-international%2FNexogen.Libraries.Metrics/lists"}