{"id":19101024,"url":"https://github.com/Azure-Samples/application-insights-aspnet-sample-opentelemetry","last_synced_at":"2025-04-18T18:31:25.584Z","repository":{"id":52496859,"uuid":"240560104","full_name":"Azure-Samples/application-insights-aspnet-sample-opentelemetry","owner":"Azure-Samples","description":"Sample ASP.NET application demonstrating the use of Application Insights and OpenTelemetry","archived":false,"fork":false,"pushed_at":"2024-01-16T20:03:51.000Z","size":587,"stargazers_count":89,"open_issues_count":3,"forks_count":26,"subscribers_count":19,"default_branch":"master","last_synced_at":"2024-04-11T17:02:13.409Z","etag":null,"topics":[],"latest_commit_sha":null,"homepage":null,"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/Azure-Samples.png","metadata":{"files":{"readme":"README.md","changelog":null,"contributing":null,"funding":null,"license":"LICENSE","code_of_conduct":"CODE_OF_CONDUCT.md","threat_model":null,"audit":null,"citation":null,"codeowners":null,"security":"SECURITY.md","support":null}},"created_at":"2020-02-14T17:10:26.000Z","updated_at":"2024-04-04T11:13:30.000Z","dependencies_parsed_at":"2022-09-06T15:11:16.175Z","dependency_job_id":null,"html_url":"https://github.com/Azure-Samples/application-insights-aspnet-sample-opentelemetry","commit_stats":null,"previous_names":[],"tags_count":1,"template":false,"template_full_name":null,"repository_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/Azure-Samples%2Fapplication-insights-aspnet-sample-opentelemetry","tags_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/Azure-Samples%2Fapplication-insights-aspnet-sample-opentelemetry/tags","releases_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/Azure-Samples%2Fapplication-insights-aspnet-sample-opentelemetry/releases","manifests_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/Azure-Samples%2Fapplication-insights-aspnet-sample-opentelemetry/manifests","owner_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/owners/Azure-Samples","download_url":"https://codeload.github.com/Azure-Samples/application-insights-aspnet-sample-opentelemetry/tar.gz/refs/heads/master","host":{"name":"GitHub","url":"https://github.com","kind":"github","repositories_count":223783093,"owners_count":17201905,"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":[],"created_at":"2024-11-09T03:53:08.061Z","updated_at":"2025-04-18T18:31:25.558Z","avatar_url":"https://github.com/Azure-Samples.png","language":"C#","funding_links":[],"categories":["others"],"sub_categories":[],"readme":"---\r\npage_type: sample\r\nlanguages:\r\n- csharp\r\nproducts:\r\n- dotnet\r\ndescription: \"Adding observability to ASP.NET Core application using OpenTelemetry and Application Insights\"\r\nurlFragment: \"update-this-to-unique-url-stub\"\r\n---\r\n\r\n# Official Microsoft Sample\r\n\r\n\u003c!-- \r\nGuidelines on README format: https://review.docs.microsoft.com/help/onboard/admin/samples/concepts/readme-template?branch=master\r\n\r\nGuidance on onboarding samples to docs.microsoft.com/samples: https://review.docs.microsoft.com/help/onboard/admin/samples/process/onboarding?branch=master\r\n\r\nTaxonomies for products and languages: https://review.docs.microsoft.com/new-hope/information-architecture/metadata/taxonomies?branch=master\r\n--\u003e\r\n\r\nThis sample application takes a look at current options to implement observability in a ASP.NET Core. It uses OpenTelemetry and Application Insights SDKs to illustrate how logging, tracing and metrics to monitor an application. It contains a distributed transaction example trace including REST, dependencies and RabbitMQ processing.\r\n\r\n![Sample application](./media/sample-app-overview.png)\r\n\r\n## Contents\r\n\r\n| File/folder       | Description                                |\r\n|-------------------|--------------------------------------------|\r\n| `src`             | Sample source code.                        |\r\n| `quickstart`      | Quick start using docker-compose and pre-built images.                        |\r\n| `.gitignore`      | Define what to ignore at commit time.      |\r\n| `CHANGELOG.md`    | List of changes to the sample.             |\r\n| `CONTRIBUTING.md` | Guidelines for contributing to the sample. |\r\n| `README.md`       | This README file.                          |\r\n| `LICENSE`         | The license for the sample.                |\r\n\r\n## Prerequisites\r\n\r\nSample application can be executed in two ways:\r\n\r\n- Using docker-compose. It is a great way to get started.\r\n- Downloading source code and running it locally. Using Visual Studio or another IDE. In this case the .NET Core 3.1 SDK is required. To use Jaeger and Prometheues Docker is recommended.\r\n\r\n## Setup - Quickstart with docker-compose\r\n\r\nTo run the application using pre-built images and docker-compose following the guideline below:\r\n\r\n### Using OpenTelemetry\r\n\r\n1. Clone this repository\r\n1. Open terminal under `quickstart/sample`\r\n1. Execute `docker-compose up` (-d if you don't wish to see console logs)\r\n1. View traces in [Jaeger](http://localhost:16686/)\r\n1. View metrics by searching for \"Enqueued_Item\" in [Prometheus](http://localhost:9090)\r\n1. Build dashboards in [Grafana](http://localhost:3000/) (admin/password1)\r\n\r\n### Using Application Insights SDK\r\n\r\n1. Clone this repository\r\n1. Open terminal under `quickstart/sample`\r\n1. Create file `quickstart/sample/.env` with following content:\r\n\r\n```env\r\nUSE_APPLICATIONINSIGHTS=true\r\nUSE_OPENTELEMETRY=false\r\nAI_INSTRUMENTATIONKEY=\u003cENTER-APPLICATION-INSIGHTS-INSTRUMENTATION-KEY\u003e\r\n```\r\n\r\n4. Execute `docker-compose up` (-d if you don't wish to see console logs)\r\n5. View logs, traces and metrics in Azure Portal Application Insights\r\n\r\n## Setup - Compile/debug locally\r\n\r\nClone or download the sample from this repository, then open the solution found in root folder using your favorite IDE.\r\n\r\nBefore running ensure the following dependencies are available:\r\n\r\n- SQL Server is available at `server=localhost;user id=sa;password=Pass@Word1;`\u003cbr/\u003e\r\nA way to accomplish it is to run as a linux docker container:\r\n\r\n```bash\r\ndocker run --name sqlserver -e \"ACCEPT_EULA=Y\" -e \"SA_PASSWORD=Pass@Word1\" -p 1433:1433 -d mcr.microsoft.com/mssql/server:2019-GA-ubuntu-16.04\r\n```\r\n\r\n- When using OpenTelemetry, ensure Jaeger is running locally\r\n\r\n```bash\r\ndocker run -d --name jaeger \\\r\n        -e COLLECTOR_ZIPKIN_HTTP_PORT=9411 \\\r\n        -p 5775:5775/udp \\\r\n        -p 6831:6831/udp \\\r\n        -p 6832:6832/udp \\\r\n        -p 5778:5778 \\\r\n        -p 16686:16686 \\\r\n        -p 14268:14268 \\\r\n        -p 9411:9411 \\\r\n        jaegertracing/all-in-one\r\n```\r\n\r\n- When using OpenTelemetry, in order to visualize metrics ensure Grafana and Prometheus are running locally. A docker-compose file is ready to run under `quickstart/prometheus-grafana`. Open terminal in the mentioned folder and execute `docker-compose up -d`.\u003cbr/\u003eTo visualize it, open Grafana on your browser at [http://localhost:3000](http://localhost:3000) (credentials are admin/password1). Next, add Prometheus as data source (URL is http://prometheus:9090).\r\n\r\n- When using Application Insights, ensure the instrumentation key is set (a simpler way to provide settings to all applications is to create file appsettings.Development.json in folder ./shared):\r\n\r\n```json\r\n{\r\n    \"SampleApp\": {\r\n        \"UseApplicationInsights\": \"true\",\r\n        \"UseOpenTelemetry\": \"false\",\r\n        \"ApplicationInsightsInstrumentationKey\": \"\u003cInstrumentation key\u003e\"\r\n    }\r\n}\r\n```\r\n\r\n#### Generating load\r\n\r\nThe application will only collect data once it starts to receive load. To generate load use the following scripts:\r\n\r\nEnqueuing from \"WebSiteA\" every 2 seconds\r\n\r\n```cmd\r\nwatch -n 2 curl --request GET http://localhost:5001/api/enqueue/WebSiteA\r\n```\r\n\r\n```powershell\r\nwhile (1) {Invoke-WebRequest -Uri http://localhost:5001/api/enqueue/WebSiteA; sleep 2}\r\n```\r\n\r\nEnqueuing from \"WebSiteB\" every 10 seconds\r\n\r\n```cmd\r\nwatch -n 10 curl --request GET http://localhost:5001/api/enqueue/WebSiteB\r\n```\r\n\r\n```powershell\r\nwhile (1) {Invoke-WebRequest -Uri http://localhost:5001/api/enqueue/WebSiteB; sleep 10}\r\n```\r\n\r\nEnqueuing from \"WebSiteC\" every 30 seconds\r\n\r\n```cmd\r\nwatch -n 30 curl --request GET http://localhost:5001/api/enqueue/WebSiteC\r\n```\r\n\r\n```powershell\r\nwhile (1) {Invoke-WebRequest -Uri http://localhost:5001/api/enqueue/WebSiteC; sleep 30}\r\n```\r\n\r\n## Key concepts\r\n\r\nGoal of the sample application is to demonstrate ways you can add the 3 observability pillars to your ASP.NET Core application:\r\n\r\n### Logging\r\n\r\nCollects information about events happening in the system, helping the team analyze unexpected application behavior. Searching through the logs of suspect services can provide the necessary hint to identify the problem root cause: service is throwing out of memory exceptions, app configuration does not reflect expected values, calls to external service have incorrect address, calls to external service returns unexpected results, incoming requests have unexpected input, etc.\r\n\r\nLogging with Application Insights:\r\n\r\n![Logging](./media/ai-search-logs.png)\r\n\r\n### Traces\r\n\r\nCollects information in order to create an end-to-end view of how transactions are executed in a distributed system. A trace is like a stack trace spanning multiple applications. Once a problem has been recognized, traces are a good starting point in identifying the source in distributed operations: calls from service A to B are taking longer than normal, service payment calls are failing, etc.\r\n\r\nTraces with Jaeger:\r\n\r\n![Jaeger Tracing](./media/jaeger-tracing.png)\r\n\r\nTraces with Application Insights:\r\n\r\n![Application Insights Tracing](./media/ai-tracing.png)\r\n\r\n### Metrics\r\n\r\nProvide a near real-time indication of how the system is running. Can be leveraged to build alerts, allowing proactive reactance to unexpected values. As opposed to logs and traces, the amount of data collected using metrics remains constant as the system load increases. Application problems are often first detected through abnormal metric values: CPU usage is higher than before, payment error count is spiking, queued item count keeps growing.\r\n\r\nMetrics with \r\n![Grafane/Prometheus Metrics](./media/grafana-metrics.png)\r\n\r\n## Contributing\r\n\r\nThis project welcomes contributions and suggestions.  Most contributions require you to agree to a\r\nContributor License Agreement (CLA) declaring that you have the right to, and actually do, grant us\r\nthe rights to use your contribution. For details, visit https://cla.opensource.microsoft.com.\r\n\r\nWhen you submit a pull request, a CLA bot will automatically determine whether you need to provide\r\na CLA and decorate the PR appropriately (e.g., status check, comment). Simply follow the instructions\r\nprovided by the bot. You will only need to do this once across all repos using our CLA.\r\n\r\nThis project has adopted the [Microsoft Open Source Code of Conduct](https://opensource.microsoft.com/codeofconduct/).\r\nFor more information see the [Code of Conduct FAQ](https://opensource.microsoft.com/codeofconduct/faq/) or\r\ncontact [opencode@microsoft.com](mailto:opencode@microsoft.com) with any additional questions or comments.\r\n","project_url":"https://awesome.ecosyste.ms/api/v1/projects/github.com%2FAzure-Samples%2Fapplication-insights-aspnet-sample-opentelemetry","html_url":"https://awesome.ecosyste.ms/projects/github.com%2FAzure-Samples%2Fapplication-insights-aspnet-sample-opentelemetry","lists_url":"https://awesome.ecosyste.ms/api/v1/projects/github.com%2FAzure-Samples%2Fapplication-insights-aspnet-sample-opentelemetry/lists"}