Skip to content

Commit 55c0dfb

Browse files
[Instrumentation.Process, Resources.Process] Properly disposes of System.Diagnostics.Process class instances insta… (open-telemetry#2101)
1 parent 3dc3d78 commit 55c0dfb

File tree

4 files changed

+21
-6
lines changed

4 files changed

+21
-6
lines changed

src/OpenTelemetry.Instrumentation.Process/CHANGELOG.md

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -2,6 +2,9 @@
22

33
## Unreleased
44

5+
* Properly dispose of System.Diagnostics.Process class instances
6+
([#2101](https://github.com/open-telemetry/opentelemetry-dotnet-contrib/pull/2101))
7+
58
## 0.5.0-beta.6
69

710
Released 2024-Jun-18

src/OpenTelemetry.Instrumentation.Process/ProcessMetrics.cs

Lines changed: 7 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -24,7 +24,8 @@ static ProcessMetrics()
2424
"process.memory.usage",
2525
() =>
2626
{
27-
return Diagnostics.Process.GetCurrentProcess().WorkingSet64;
27+
using var process = Diagnostics.Process.GetCurrentProcess();
28+
return process.WorkingSet64;
2829
},
2930
unit: "By",
3031
description: "The amount of physical memory in use.");
@@ -33,7 +34,8 @@ static ProcessMetrics()
3334
"process.memory.virtual",
3435
() =>
3536
{
36-
return Diagnostics.Process.GetCurrentProcess().VirtualMemorySize64;
37+
using var process = Diagnostics.Process.GetCurrentProcess();
38+
return process.VirtualMemorySize64;
3739
},
3840
unit: "By",
3941
description: "The amount of committed virtual memory.");
@@ -42,7 +44,7 @@ static ProcessMetrics()
4244
"process.cpu.time",
4345
() =>
4446
{
45-
var process = Diagnostics.Process.GetCurrentProcess();
47+
using var process = Diagnostics.Process.GetCurrentProcess();
4648
return new[]
4749
{
4850
new Measurement<double>(process.UserProcessorTime.TotalSeconds, new KeyValuePair<string, object?>("process.cpu.state", "user")),
@@ -65,7 +67,8 @@ static ProcessMetrics()
6567
"process.thread.count",
6668
() =>
6769
{
68-
return Diagnostics.Process.GetCurrentProcess().Threads.Count;
70+
using var process = Diagnostics.Process.GetCurrentProcess();
71+
return process.Threads.Count;
6972
},
7073
unit: "{thread}",
7174
description: "Process threads count.");

src/OpenTelemetry.Resources.Process/CHANGELOG.md

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -2,6 +2,9 @@
22

33
## Unreleased
44

5+
* Properly dispose of System.Diagnostics.Process class instances
6+
([#2101](https://github.com/open-telemetry/opentelemetry-dotnet-contrib/pull/2101))
7+
58
## 0.1.0-beta.2
69

710
Released 2024-Jun-18

src/OpenTelemetry.Resources.Process/ProcessDetector.cs

Lines changed: 8 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -19,9 +19,15 @@ public Resource Detect()
1919
new(ProcessSemanticConventions.AttributeProcessOwner, Environment.UserName),
2020
#if NET
2121
new(ProcessSemanticConventions.AttributeProcessPid, Environment.ProcessId),
22+
});
2223
#else
23-
new(ProcessSemanticConventions.AttributeProcessPid, System.Diagnostics.Process.GetCurrentProcess().Id),
24-
#endif
24+
new(ProcessSemanticConventions.AttributeProcessPid, GetProcessPid()),
2525
});
26+
static int GetProcessPid()
27+
{
28+
using var process = System.Diagnostics.Process.GetCurrentProcess();
29+
return process.Id;
30+
}
31+
#endif
2632
}
2733
}

0 commit comments

Comments
 (0)