Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

fix .NET app name collisions #133

Merged
merged 1 commit into from
Oct 15, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
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