Skip to content

Commit

Permalink
Merge pull request #133 from coroot/fix_dotnet_apps_collisions
Browse files Browse the repository at this point in the history
fix .NET app name collisions
  • Loading branch information
def authored Oct 15, 2024
2 parents dd275c6 + 1d3c74b commit ad6ad6a
Show file tree
Hide file tree
Showing 2 changed files with 22 additions and 4 deletions.
17 changes: 15 additions & 2 deletions containers/container.go
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@ package containers

import (
"os"
"sort"
"strings"
"sync"
"time"
Expand All @@ -19,6 +20,7 @@ import (
"github.com/coroot/logparser"
"github.com/prometheus/client_golang/prometheus"
"github.com/vishvananda/netns"
"golang.org/x/exp/maps"
"inet.af/netaddr"
"k8s.io/klog/v2"
)
Expand Down Expand Up @@ -335,7 +337,14 @@ func (c *Container) Collect(ch chan<- prometheus.Metric) {

appTypes := map[string]struct{}{}
seenJvms := map[string]bool{}
for pid, process := range c.processes {
seenDotNetApps := map[string]bool{}
pids := maps.Keys(c.processes)
sort.Slice(pids, func(i, j int) bool {
return pids[i] < pids[j]
})

for _, pid := range pids {
process := c.processes[pid]
cmdline := proc.GetCmdline(pid)
if len(cmdline) == 0 {
continue
Expand All @@ -358,7 +367,11 @@ func (c *Container) Collect(ch chan<- prometheus.Metric) {
}
case process.dotNetMonitor != nil:
appTypes["dotnet"] = struct{}{}
process.dotNetMonitor.Collect(ch)
appName := process.dotNetMonitor.AppName()
if !seenDotNetApps[appName] {
seenDotNetApps[appName] = true
process.dotNetMonitor.Collect(ch)
}
}
}
for appType := range appTypes {
Expand Down
9 changes: 7 additions & 2 deletions containers/dotnet.go
Original file line number Diff line number Diff line change
Expand Up @@ -53,6 +53,7 @@ func (m *dotNetMetric) units() string {

type DotNetMonitor struct {
pid uint32
appName string
cancel context.CancelFunc
lastUpdate time.Time
runtimeVersion string
Expand All @@ -74,8 +75,8 @@ func NewDotNetMonitor(ctx context.Context, pid uint32, appName string) *DotNetMo
constLabels := prometheus.Labels{"application": appName}

m := &DotNetMonitor{
pid: pid,

pid: pid,
appName: appName,
info: newGaugeVec("container_dotnet_info", "Meta information about the Common Language Runtime (CLR)", constLabels, "runtime_version"),
memoryAllocatedBytes: newCounter("container_dotnet_memory_allocated_bytes_total", "The number of bytes allocated", constLabels),
exceptionCount: newGauge("container_dotnet_exceptions_total", "The number of exceptions that have occurred", constLabels),
Expand All @@ -91,6 +92,10 @@ func NewDotNetMonitor(ctx context.Context, pid uint32, appName string) *DotNetMo
return m
}

func (m *DotNetMonitor) AppName() string {
return m.appName
}

func (m *DotNetMonitor) Collect(ch chan<- prometheus.Metric) {
if m.lastUpdate.Before(time.Now().Add(-2 * dotNetEventInterval)) {
return
Expand Down

0 comments on commit ad6ad6a

Please sign in to comment.