Skip to content

Commit 06654ae

Browse files
committed
feature: add new server property to enable telemetry (#42)
1 parent 0bbc77d commit 06654ae

9 files changed

Lines changed: 79 additions & 34 deletions

File tree

documentation/docs/guide/observability.md

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -7,6 +7,7 @@ Observability is integrated through support for **OpenTelemetry**, the [open sta
77

88
Logs are very useful for developer when coding.
99
The following example shows how to :
10+
- [`EnableTelemetry `](../reference/simplewserver#telemetry)
1011
- subscribe to all SimpleW `Event` with `openTelemetryObserver()`
1112
- log each request to console with `LogProcessor` (do not use for production).
1213

documentation/docs/reference/simplewserver.md

Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -376,6 +376,15 @@ bool AutoIndex { get; set; } = false;
376376
This property enable or disable the index feature which list files of a static
377377
content directory when no file has been selected and `DefaultDocument` does not exists.
378378

379+
## Telemetry
380+
381+
```csharp
382+
/// <summary>
383+
/// True to enable telemetry
384+
/// </summary>
385+
public bool EnableTelemetry { get; set; } = false;
386+
```
387+
379388
## Statistics
380389

381390
```csharp

documentation/docs/snippets/observability-advanced.cs

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -16,6 +16,10 @@ static void Main() {
1616
openTelemetryObserver("SimpleW");
1717

1818
var server = new SimpleWServer(IPAddress.Any, 2015);
19+
20+
// enable telemetry
21+
server.EnableTelemetry = true;
22+
1923
server.AddDynamicContent("/api");
2024
server.Start();
2125
Console.WriteLine("server started at http://localhost:2015/");

documentation/docs/snippets/observability.cs

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -16,6 +16,10 @@ static void Main() {
1616
openTelemetryObserver("SimpleW");
1717

1818
var server = new SimpleWServer(IPAddress.Any, 2015);
19+
20+
// enable telemetry
21+
server.EnableTelemetry = true;
22+
1923
server.AddDynamicContent("/api");
2024
server.Start();
2125
Console.WriteLine("server started at http://localhost:2015/");

src/SimpleW/SimpleW/ISimpleWServer.cs

Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -111,6 +111,15 @@ public interface ISimpleWServer {
111111

112112
#endregion cors
113113

114+
#region telemetry
115+
116+
/// <summary>
117+
/// True to enable telemetry
118+
/// </summary>
119+
bool EnableTelemetry { get; set; }
120+
121+
#endregion telemetry
122+
114123
}
115124

116125
}

src/SimpleW/SimpleW/SimpleWSServer.cs

Lines changed: 16 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -44,15 +44,17 @@ public SimpleWSServer(SslContext context, IPAddress address, int port) : base(co
4444
/// </summary>
4545
/// <param name="error"></param>
4646
protected override void OnError(SocketError error) {
47-
Activity? activity = source.StartActivity();
48-
if (activity != null) {
49-
activity.DisplayName = "Server OnError()";
50-
activity.SetStatus(ActivityStatusCode.Error);
51-
ActivityTagsCollection tagsCollection = new() {
52-
{ "exception.type", nameof(SocketError) },
53-
};
54-
activity.AddEvent(new ActivityEvent("exception", default, tagsCollection));
55-
activity.Stop();
47+
if (EnableTelemetry) {
48+
Activity? activity = source.StartActivity();
49+
if (activity != null) {
50+
activity.DisplayName = "Server OnError()";
51+
activity.SetStatus(ActivityStatusCode.Error);
52+
ActivityTagsCollection tagsCollection = new() {
53+
{ "exception.type", nameof(SocketError) },
54+
};
55+
activity.AddEvent(new ActivityEvent("exception", default, tagsCollection));
56+
activity.Stop();
57+
}
5658
}
5759
}
5860

@@ -456,6 +458,11 @@ public void AddCORS(string origin="*", string headers = "*", string methods = "G
456458

457459
#region OpenTelemetry
458460

461+
/// <summary>
462+
/// True to enable telemetry
463+
/// </summary>
464+
public bool EnableTelemetry { get; set; } = false;
465+
459466
/// <summary>
460467
/// ActivitySource
461468
/// </summary>

src/SimpleW/SimpleW/SimpleWSSession.cs

Lines changed: 10 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -248,14 +248,16 @@ public override void OnWsDisconnected() {
248248
/// </summary>
249249
/// <param name="error"></param>
250250
protected override void OnError(SocketError error) {
251-
Activity? activity = CreateActivity();
252-
SetDefaultActivity(activity, "Session OnError()", Id);
253-
activity.SetStatus(ActivityStatusCode.Error);
254-
ActivityTagsCollection tagsCollection = new() {
251+
if (server.EnableTelemetry) {
252+
Activity? activity = CreateActivity();
253+
SetDefaultActivity(activity, "Session OnError()", Id);
254+
activity.SetStatus(ActivityStatusCode.Error);
255+
ActivityTagsCollection tagsCollection = new() {
255256
{ "exception.type", nameof(SocketError) },
256-
};
257-
activity.AddEvent(new ActivityEvent("exception", default, tagsCollection));
258-
activity.Stop();
257+
};
258+
activity.AddEvent(new ActivityEvent("exception", default, tagsCollection));
259+
activity.Stop();
260+
}
259261
}
260262

261263
/// <summary>
@@ -281,7 +283,7 @@ protected override void OnDisconnected() {
281283
/// </summary>
282284
/// <returns></returns>
283285
protected Activity? CreateActivity() {
284-
return ActivitySource.StartActivity();
286+
return server.EnableTelemetry ? ActivitySource.StartActivity() : null;
285287
}
286288

287289
/// <summary>

src/SimpleW/SimpleW/SimpleWServer.cs

Lines changed: 16 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -43,15 +43,17 @@ public SimpleWServer(IPAddress address, int port) : base(address, port) { }
4343
/// </summary>
4444
/// <param name="error"></param>
4545
protected override void OnError(SocketError error) {
46-
Activity? activity = source.StartActivity();
47-
if (activity != null) {
48-
activity.DisplayName = "Server OnError()";
49-
activity.SetStatus(ActivityStatusCode.Error);
50-
ActivityTagsCollection tagsCollection = new() {
51-
{ "exception.type", nameof(SocketError) },
52-
};
53-
activity.AddEvent(new ActivityEvent("exception", default, tagsCollection));
54-
activity.Stop();
46+
if (EnableTelemetry) {
47+
Activity? activity = source.StartActivity();
48+
if (activity != null) {
49+
activity.DisplayName = "Server OnError()";
50+
activity.SetStatus(ActivityStatusCode.Error);
51+
ActivityTagsCollection tagsCollection = new() {
52+
{ "exception.type", nameof(SocketError) },
53+
};
54+
activity.AddEvent(new ActivityEvent("exception", default, tagsCollection));
55+
activity.Stop();
56+
}
5557
}
5658
}
5759

@@ -455,6 +457,11 @@ public void AddCORS(string origin="*", string headers = "*", string methods = "G
455457

456458
#region OpenTelemetry
457459

460+
/// <summary>
461+
/// True to enable telemetry
462+
/// </summary>
463+
public bool EnableTelemetry { get; set; } = false;
464+
458465
/// <summary>
459466
/// ActivitySource
460467
/// </summary>

src/SimpleW/SimpleW/SimpleWSession.cs

Lines changed: 10 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -248,14 +248,16 @@ public override void OnWsDisconnected() {
248248
/// </summary>
249249
/// <param name="error"></param>
250250
protected override void OnError(SocketError error) {
251-
Activity? activity = CreateActivity();
252-
SetDefaultActivity(activity, "Session OnError()", Id);
253-
activity.SetStatus(ActivityStatusCode.Error);
254-
ActivityTagsCollection tagsCollection = new() {
251+
if (server.EnableTelemetry) {
252+
Activity? activity = CreateActivity();
253+
SetDefaultActivity(activity, "Session OnError()", Id);
254+
activity.SetStatus(ActivityStatusCode.Error);
255+
ActivityTagsCollection tagsCollection = new() {
255256
{ "exception.type", nameof(SocketError) },
256-
};
257-
activity.AddEvent(new ActivityEvent("exception", default, tagsCollection));
258-
activity.Stop();
257+
};
258+
activity.AddEvent(new ActivityEvent("exception", default, tagsCollection));
259+
activity.Stop();
260+
}
259261
}
260262

261263
/// <summary>
@@ -281,7 +283,7 @@ protected override void OnDisconnected() {
281283
/// </summary>
282284
/// <returns></returns>
283285
protected Activity? CreateActivity() {
284-
return ActivitySource.StartActivity();
286+
return server.EnableTelemetry ? ActivitySource.StartActivity() : null;
285287
}
286288

287289
/// <summary>

0 commit comments

Comments
 (0)