Program.cs demonstrates how to access WebApplication to a MySQL database by using MySQLConnector. You can create the corresponding MySQL database or purchase one directly.dotnet new web
Microsoft.Extensions.Logging.Abstractions is a necessary package for MySqlConnector.dotnet add package MySqlConnector;dotnet add package Microsoft.Extensions.Logging.Abstractions;
Properties/launchSettings.json file.{"$schema": "http://json.schemastore.org/launchsettings.json","profiles": {"http": {"commandName": "Project","dotnetRunMessages": true,"launchBrowser": true,"applicationUrl": "http://localhost:8080","environmentVariables": {"ASPNETCORE_ENVIRONMENT": "Development"}}}}
Program.cs.Program.cs demonstrates how to call the WebApplication API to simulate access to a MySQL database by using MySQLConnector.using MySqlConnector;var builder = WebApplication.CreateBuilder(args);var app = builder.Build();app.MapGet("/", () => "Hello World!");app.MapGet("/todo", () =>{string connectionString = "server=localhost;port=3306;User Id=root;password=;Database=MyDB";using (MySqlConnection myConnnect = new MySqlConnection(connectionString)){try{myConnnect.Open();using (MySqlCommand myCmd = new MySqlCommand("select * from MyTable", myConnnect)){using (MySqlDataReader reader = myCmd.ExecuteReader()){List<Int32> results = new List<Int32>();while (reader.Read()){// Assume we only process data IDs in the first column.results.Add(reader.GetInt32(0));}return "ids:" + string.Join(", ", results);}}}catch (Exception ex){Console.WriteLine($"Database operation error: {ex.Message}");return "Database operation error";}}});app.Run();
curl -L -O https://github.com/open-telemetry/opentelemetry-dotnet-instrumentation/releases/latest/download/otel-dotnet-auto-install.shchmod -x otel-dotnet-auto-install.shsh ./otel-dotnet-auto-install.sh
chmod -x $HOME/.otel-dotnet-auto/instrument.shexport OTEL_TRACES_EXPORTER=otlp \\OTEL_METRICS_EXPORTER=none \\OTEL_LOGS_EXPORTER=none \\OTEL_SERVICE_NAME=<service-name> \\ # Replace <service-name> with the actual service name.OTEL_EXPORTER_OTLP_PROTOCOL=grpc \\OTEL_EXPORTER_OTLP_ENDPOINT=<endpoint> \\ # Replace with the access point obtained in Step 1.OTEL_RESOURCE_ATTRIBUTES="token=<token>" # Replace with the token obtained in step 1.. $HOME/.otel-dotnet-auto/instrument.sh
brew install coreutils
dotnet builddotnet run
http://localhost:8080http://localhost:8080/todo
https://localhost:8080/todo. In case of normal traffic, the connected application will be displayed in APM > Application list. Click on the Application name/ID to enter the application details page, then select Instance Analysis to view the connected application instance. Due to some delay in processing observable data, if the application or instance is not found in the console after connecting, please wait about 30 seconds.dotnet add package System.Diagnostics.DiagnosticSource
private static readonly ActivitySource RegisteredActivity = new ActivitySource("Examples.ManualInstrumentations.Registered");
using (var activity = RegisteredActivity.StartActivity("Main")){// Add theActivity Tag.activity?.SetTag("key", "3.1415");// Add an Event to the Activity.activity?.AddEvent(new("something happened"));// Write the business code.Thread.Sleep(1000);}
OTEL_DOTNET_AUTO_TRACES_ADDITIONAL_SOURCES. ActivitySources not registered in this way will not be reported.exportOTEL_DOTNET_AUTO_TRACES_ADDITIONAL_SOURCES=Examples.ManualInstrumentations.Registered
dotnet add package OpenTelemetrydotnet add package OpenTelemetry.Exporter.OpenTelemetryProtocoldotnet add package OpenTelemetry.Extensions.Hosting
dotnet add package OpenTelemetry.Instrumentation.AspNetCore
tracerProvider and add the following code at the beginning of your program.var serviceName = "<service-name>"; // Replace <service-name> with the actual service name.using var tracerProvider = Sdk.CreateTracerProviderBuilder().AddSource(serviceName).SetResourceBuilder(ResourceBuilder.CreateDefault().AddService(serviceName).AddAttributes(new Dictionary<string, object>{["token"] = "<token>", // Replace <token> with the token obtained in the previous step.["host.name"] = "<host>" // Replace <host> with the actual hostname.})).AddOtlpExporter(opt =>{opt.Endpoint = new Uri("<endpoint>"); // Replace <endpoint> with the access point information obtained in the previous step.opt.Protocol = OtlpExportProtocol.Grpc;}).Build();
ActivitySource for creating an Activity at any place in the application that requires manual access.var MyActivitySource = new ActivitySource(serviceName);
ActivitySource is created, create an Activity.using var activity = MyActivitySource.StartActivity("SayHello");// Add theActivity Tag.activity?.SetTag("key", "3.1415");// Add an Event to the Activity.activity?.AddEvent(new("something happened"));
dotnet builddotnet run
Feedback