Master OpenTracing & SkyWalking with .NET6: From Basics to Advanced Integration
This guide walks you through OpenTracing concepts, SkyWalking architecture, and step‑by‑step .NET6 integration—including deployment, configuration files, code snippets, microservice gateway setup, and alarm rule customization—providing a comprehensive reference for building observable cloud‑native applications.
OpenTracing Specification
OpenTracing is a design principle, specification, and standard for distributed system tracing. Similar to JDBC, it provides a vendor‑agnostic API that lets developers easily add or replace tracing implementations.
Why OpenTracing is Needed
It offers a platform‑independent, vendor‑neutral API, simplifying the integration or replacement of tracing systems and providing auxiliary libraries for operational support.
What Is a Trace
A trace represents the execution of a transaction or workflow across a distributed system. In OpenTracing, a trace is a directed acyclic graph (DAG) of spans, where each span records a timed, named segment of the execution.
In a typical RPC call, OpenTracing recommends at least one span on the client side and one on the server side to capture the call details.
A parent span can launch multiple child spans in parallel or serially, and a child span may have multiple parents.
A Typical Trace Example
This diagram shows component relationships but lacks timing details. A more effective view adds execution time context, hierarchy, and serial/parallel relationships, helping teams identify critical paths for performance optimization.
SkyWalking
SkyWalking is an Application Performance Management (APM) system designed for microservices, cloud‑native, and container‑based architectures (Docker, Kubernetes, Mesos). It provides distributed tracing, service‑mesh telemetry, metric aggregation, and unified visualization.
Key Features
Multiple monitoring methods via language agents and service mesh.
Automatic agents for Java, .NET Core, and Node.js.
Lightweight without requiring a big‑data platform.
Modular UI, storage, and cluster management.
Built‑in alerting.
Rich visualization capabilities.
Overall Architecture
Probe – Collects data from various sources and formats it for SkyWalking.
Backend – Cluster‑capable service that aggregates, analyzes, and drives data flow from probes to the UI; supports pluggable data sources, storage, and custom analysis scripts.
Storage – Open‑ended; can use Elasticsearch, H2, MySQL (Sharding‑Sphere), or custom implementations.
User Interface – Powerful, customizable UI for end users.
Tracing, Logging, and Metrics
In microservices, tracing captures the end‑to‑end request flow, logging records discrete events, and metrics aggregate quantitative data such as QPS or latency.
.NET6 Integration with SkyWalking
Deploy SkyWalking Environment
version: '3.3'
services:
elasticsearch:
image: docker.elastic.co/elasticsearch/elasticsearch:7.5.0
container_name: elasticsearch
restart: always
ports:
- 9200:9200
environment:
- discovery.type=single-node
- bootstrap.memory_lock=true
- "ES_JAVA_OPTS=-Xms256m -Xmx256m"
ulimits:
memlock:
soft: -1
hard: -1
oap:
image: apache/skywalking-oap-server:6.6.0-es7
container_name: oap
depends_on:
- elasticsearch
restart: always
ports:
- 11800:11800
- 12800:12800
environment:
SW_STORAGE: elasticsearch
SW_STORAGE_ES_CLUSTER_NODES: elasticsearch:9200
ui:
image: apache/skywalking-ui:6.6.0
container_name: ui
depends_on:
- oap
restart: always
ports:
- 8080:8080
environment:
SW_OAP_ADDRESS: http://oap:12800After installation, the UI is accessible at http:// :8080
Add Dependencies
<ItemGroup>
<PackageReference Include="SkyAPM.Agent.AspNetCore" Version="1.3.0" />
</ItemGroup>Edit SkyWalking Configuration (skyapm.json)
{
"SkyWalking": {
"ServiceName": "MySkyWalkingDemoTest",
"Namespace": "",
"HeaderVersions": ["sw8"],
"Sampling": {
"SamplePer3Secs": -1,
"Percentage": -1.0
},
"Logging": {
"Level": "Information",
"FilePath": "logs\\skyapm-{Date}.log"
},
"Transport": {
"Interval": 3000,
"ProtocolVersion": "v8",
"QueueSize": 30000,
"BatchSize": 3000,
"gRPC": {
"Servers": "192.168.3.245:11800",
"Timeout": 10000,
"ConnectTimeout": 10000,
"ReportTimeout": 600000,
"Authentication": ""
}
}
}
}Generate skyapm.json via CLI
dotnet tool install -g SkyAPM.DotNet.CLI dotnet skyapm config MySkyWalkingDemoTest 192.168.3.245:11800Configure launchSettings.json
{
"profiles": {
"IIS Express": {
"commandName": "IISExpress",
"launchBrowser": true,
"launchUrl": "weatherforecast",
"environmentVariables": {
"ASPNETCORE_ENVIRONMENT": "Development",
"ASPNETCORE_HOSTINGSTARTUPASSEMBLIES": "SkyAPM.Agent.AspNetCore",
"SKYWALKING__SERVICENAME": "MySkyWalkingDemoTest"
}
},
"SkyWalkingDemo": {
"commandName": "Project",
"launchBrowser": true,
"launchUrl": "weatherforecast",
"applicationUrl": "http://localhost:5000",
"environmentVariables": {
"ASPNETCORE_ENVIRONMENT": "Development",
"ASPNETCORE_HOSTINGSTARTUPASSEMBLIES": "SkyAPM.Agent.AspNetCore",
"SKYWALKING__SERVICENAME": "MySkyWalkingDemoTest"
}
}
}
}Modify startup.cs
public void ConfigureServices(IServiceCollection services)
{
services.AddSkyApmExtensions(); // Add SkyWalking extensions
services.AddControllers();
services.AddHttpClient();
}Get TraceId
private readonly IEntrySegmentContextAccessor segContext;
public SkywalkingController(IEntrySegmentContextAccessor segContext)
{
this.segContext = segContext;
}
[HttpGet("traceId")]
public string GetSkywalkingTraceId()
{
return segContext.Context.TraceId;
}Custom Span Logging
[HttpGet]
public async Task<IActionResult> SkywalkingTest()
{
var TraceId = _segContext.Context.TraceId;
Console.WriteLine($"TraceId={TraceId}");
_segContext.Context.Span.AddLog(LogEvent.Message($"SkywalkingTest---Worker running at: {DateTime.Now}"));
System.Threading.Thread.Sleep(1000);
_segContext.Context.Span.AddLog(LogEvent.Message($"SkywalkingTest---Worker running at--end: {DateTime.Now}"));
return Ok($"Ok,SkywalkingTest-TraceId={TraceId} ");
}Microservice Gateway Integration
Add Dependencies
<ItemGroup>
<PackageReference Include="SkyAPM.Agent.AspNetCore" Version="1.3.0" />
</ItemGroup>Copy and Modify Configuration (skyapm.json)
{
"SkyWalking": {
"ServiceName": "MySkyWalking_Gateway",
"Namespace": "",
"HeaderVersions": ["sw8"],
"Sampling": {"SamplePer3Secs": -1, "Percentage": -1.0},
"Logging": {"Level": "Debug", "FilePath": "logs\\skyapm-{Date}.log"},
"Transport": {"Interval": 3000, "ProtocolVersion": "v8", "QueueSize": 30000, "BatchSize": 3000, "gRPC": {"Servers": "192.168.3.245:11800", "Timeout": 10000, "ConnectTimeout": 10000, "ReportTimeout": 600000, "Authentication": ""}}
}
}Update launchSettings.json for Gateway
{
"profiles": {
"Zhaoxi.MicroService.GatewayCenter": {
"commandName": "Project",
"dotnetRunMessages": true,
"launchBrowser": true,
"launchUrl": "swagger",
"environmentVariables": {
"ASPNETCORE_ENVIRONMENT": "Development",
"ASPNETCORE_HOSTINGSTARTUPASSEMBLIES": "SkyAPM.Agent.AspNetCore",
"SKYWALKING__SERVICENAME": "MySkyWalking_Gateway"
}
}
}
}Configure Route for Order Service
{
"DownstreamPathTemplate": "/api/{url}",
"DownstreamScheme": "http",
"UpstreamPathTemplate": "/microservice/{url}",
"UpstreamHttpMethod": ["Get", "Post"],
"UseServiceDiscovery": true,
"ServiceName": "OrderService",
"LoadBalancerOptions": {"Type": "RoundRobin"}
}Start Gateway
dotnet run --urls=http://*:6299Order Service Integration
Add Dependencies
<ItemGroup>
<PackageReference Include="SkyAPM.Agent.AspNetCore" Version="1.3.0" />
</ItemGroup>Copy skyapm.json (Order Service)
{
"SkyWalking": {
"ServiceName": "MySkyWalking_OrderService",
"Namespace": "",
"HeaderVersions": ["sw8"],
"Sampling": {"SamplePer3Secs": -1, "Percentage": -1.0},
"Logging": {"Level": "Debug", "FilePath": "logs\\skyapm-{Date}.log"},
"Transport": {"Interval": 3000, "ProtocolVersion": "v8", "QueueSize": 30000, "BatchSize": 3000, "gRPC": {"Servers": "192.168.3.245:11800", "Timeout": 10000, "ConnectTimeout": 10000, "ReportTimeout": 600000, "Authentication": ""}}
}
}Configure launchSettings.json for Order Service
{
"profiles": {
"Zhaoxi.MicroService.OrderServiceInstance": {
"commandName": "Project",
"dotnetRunMessages": true,
"launchBrowser": true,
"launchUrl": "swagger",
"applicationUrl": "http://192.168.3.105:7900",
"environmentVariables": {
"ASPNETCORE_ENVIRONMENT": "Development",
"ASPNETCORE_HOSTINGSTARTUPASSEMBLIES": "SkyAPM.Agent.AspNetCore",
"SKYWALKING__SERVICENAME": "MySkyWalking_OrderService"
}
}
}
}Start Order Service
dotnet runSkyWalking Alert Configuration
Enter Container to Edit Rules
docker exec -it 12f053748e85 /bin/sh ls -lView Alarm Settings
cat alarm-settings.yml rules:
service_resp_time_rule:
metrics-name: service_resp_time
op: ">"
threshold: 1000
period: 10
count: 3
silence-period: 5
message: Response time of service {name} is more than 1000ms in 3 minutes of last 10 minutes.
service_sla_rule:
metrics-name: service_sla
op: "<"
threshold: 8000
period: 10
count: 2
silence-period: 3
message: Successful rate of service {name} is lower than 80% in 2 minutes of last 10 minutes.
... (other rules omitted for brevity) ...
webhooks:
- http://192.168.3.105:7900/api/Skywalking/AlarmMsgModify an Alarm Rule Example
rules:
service_test_sal_rule:
metrics-name: service_test_sal
op: "<"
threshold: 8000
period: 2
count: 1
silence-period: 3
message: Successful rate of service {name} is lower than 80% in 2 minutes of last 10 minutes.Alarm API Implementation
public class AlarmMsg
{
public int scopeId { get; set; }
public string? scope { get; set; }
public string? name { get; set; }
public string? id0 { get; set; }
public string? id1 { get; set; }
public string? ruleName { get; set; }
public string? alarmMessage { get; set; }
}
[HttpPost("AlarmMsg")]
public void AlarmMsg(List<AlarmMsg> msgs)
{
string msg = "触发告警:" + msgs.FirstOrDefault()?.alarmMessage;
Console.WriteLine(msg);
SendMail(msg);
}Configure WebHook Endpoint
http://192.168.3.105:7900/api/Skywalking/AlarmMsgMaGe Linux Operations
Founded in 2009, MaGe Education is a top Chinese high‑end IT training brand. Its graduates earn 12K+ RMB salaries, and the school has trained tens of thousands of students. It offers high‑pay courses in Linux cloud operations, Python full‑stack, automation, data analysis, AI, and Go high‑concurrency architecture. Thanks to quality courses and a solid reputation, it has talent partnerships with numerous internet firms.
How this landed with the community
Was this worth your time?
0 Comments
Thoughtful readers leave field notes, pushback, and hard-won operational detail here.
