Merge pull request #1133 from breed808/iis_duplicates

fix: Ignore duplicate IIS entries from Perflib
This commit is contained in:
Ben Reedy
2023-05-07 20:17:25 +10:00
committed by GitHub
2 changed files with 166 additions and 64 deletions

View File

@@ -7,6 +7,8 @@ import (
"errors" "errors"
"fmt" "fmt"
"regexp" "regexp"
"sort"
"strings"
"github.com/alecthomas/kingpin/v2" "github.com/alecthomas/kingpin/v2"
"github.com/prometheus-community/windows_exporter/log" "github.com/prometheus-community/windows_exporter/log"
@@ -977,14 +979,63 @@ type perflibWebService struct {
TotalUnlockRequests float64 `perflib:"Total Unlock Requests"` TotalUnlockRequests float64 `perflib:"Total Unlock Requests"`
} }
// Fulfill the hasGetIISName interface
func (p perflibWebService) getIISName() string {
return p.Name
}
// Fulfill the hasGetIISName interface
func (p perflibAPP_POOL_WAS) getIISName() string {
return p.Name
}
// Fulfill the hasGetIISName interface
func (p perflibW3SVC_W3WP) getIISName() string {
return p.Name
}
// Fulfill the hasGetIISName interface
func (p perflibW3SVC_W3WP_IIS8) getIISName() string {
return p.Name
}
// Required as Golang doesn't allow access to struct fields in generic functions. That restriction may be removed in a future release.
type hasGetIISName interface {
getIISName() string
}
// Deduplicate IIS site names from various IIS perflib objects.
//
// E.G. Given the following list of site names, "Site_B" would be
// discarded, and "Site_B#2" would be kept and presented as "Site_B" in the
// collector metrics.
// [ "Site_A", "Site_B", "Site_C", "Site_B#2" ]
func dedupIISNames[V hasGetIISName](services []V) map[string]V {
// Ensure IIS entry with the highest suffix occurs last
sort.SliceStable(services, func(i, j int) bool {
return services[i].getIISName() < services[j].getIISName()
})
var webServiceDeDuplicated = make(map[string]V)
// Use map to deduplicate IIS entries
for _, entry := range services {
name := strings.Split(entry.getIISName(), "#")[0]
webServiceDeDuplicated[name] = entry
}
return webServiceDeDuplicated
}
func (c *IISCollector) collectWebService(ctx *ScrapeContext, ch chan<- prometheus.Metric) (*prometheus.Desc, error) { func (c *IISCollector) collectWebService(ctx *ScrapeContext, ch chan<- prometheus.Metric) (*prometheus.Desc, error) {
var WebService []perflibWebService var webService []perflibWebService
if err := unmarshalObject(ctx.perfObjects["Web Service"], &WebService); err != nil { if err := unmarshalObject(ctx.perfObjects["Web Service"], &webService); err != nil {
return nil, err return nil, err
} }
for _, app := range WebService { webServiceDeDuplicated := dedupIISNames(webService)
if app.Name == "_Total" || c.siteExcludePattern.MatchString(app.Name) || !c.siteIncludePattern.MatchString(app.Name) {
for name, app := range webServiceDeDuplicated {
if name == "_Total" || c.siteExcludePattern.MatchString(name) || !c.siteIncludePattern.MatchString(name) {
continue continue
} }
@@ -992,238 +1043,238 @@ func (c *IISCollector) collectWebService(ctx *ScrapeContext, ch chan<- prometheu
c.CurrentAnonymousUsers, c.CurrentAnonymousUsers,
prometheus.GaugeValue, prometheus.GaugeValue,
app.CurrentAnonymousUsers, app.CurrentAnonymousUsers,
app.Name, name,
) )
ch <- prometheus.MustNewConstMetric( ch <- prometheus.MustNewConstMetric(
c.CurrentBlockedAsyncIORequests, c.CurrentBlockedAsyncIORequests,
prometheus.GaugeValue, prometheus.GaugeValue,
app.CurrentBlockedAsyncIORequests, app.CurrentBlockedAsyncIORequests,
app.Name, name,
) )
ch <- prometheus.MustNewConstMetric( ch <- prometheus.MustNewConstMetric(
c.CurrentCGIRequests, c.CurrentCGIRequests,
prometheus.GaugeValue, prometheus.GaugeValue,
app.CurrentCGIRequests, app.CurrentCGIRequests,
app.Name, name,
) )
ch <- prometheus.MustNewConstMetric( ch <- prometheus.MustNewConstMetric(
c.CurrentConnections, c.CurrentConnections,
prometheus.GaugeValue, prometheus.GaugeValue,
app.CurrentConnections, app.CurrentConnections,
app.Name, name,
) )
ch <- prometheus.MustNewConstMetric( ch <- prometheus.MustNewConstMetric(
c.CurrentISAPIExtensionRequests, c.CurrentISAPIExtensionRequests,
prometheus.GaugeValue, prometheus.GaugeValue,
app.CurrentISAPIExtensionRequests, app.CurrentISAPIExtensionRequests,
app.Name, name,
) )
ch <- prometheus.MustNewConstMetric( ch <- prometheus.MustNewConstMetric(
c.CurrentNonAnonymousUsers, c.CurrentNonAnonymousUsers,
prometheus.GaugeValue, prometheus.GaugeValue,
app.CurrentNonAnonymousUsers, app.CurrentNonAnonymousUsers,
app.Name, name,
) )
ch <- prometheus.MustNewConstMetric( ch <- prometheus.MustNewConstMetric(
c.ServiceUptime, c.ServiceUptime,
prometheus.GaugeValue, prometheus.GaugeValue,
app.ServiceUptime, app.ServiceUptime,
app.Name, name,
) )
ch <- prometheus.MustNewConstMetric( ch <- prometheus.MustNewConstMetric(
c.TotalBytesReceived, c.TotalBytesReceived,
prometheus.CounterValue, prometheus.CounterValue,
app.TotalBytesReceived, app.TotalBytesReceived,
app.Name, name,
) )
ch <- prometheus.MustNewConstMetric( ch <- prometheus.MustNewConstMetric(
c.TotalBytesSent, c.TotalBytesSent,
prometheus.CounterValue, prometheus.CounterValue,
app.TotalBytesSent, app.TotalBytesSent,
app.Name, name,
) )
ch <- prometheus.MustNewConstMetric( ch <- prometheus.MustNewConstMetric(
c.TotalAnonymousUsers, c.TotalAnonymousUsers,
prometheus.CounterValue, prometheus.CounterValue,
app.TotalAnonymousUsers, app.TotalAnonymousUsers,
app.Name, name,
) )
ch <- prometheus.MustNewConstMetric( ch <- prometheus.MustNewConstMetric(
c.TotalBlockedAsyncIORequests, c.TotalBlockedAsyncIORequests,
prometheus.CounterValue, prometheus.CounterValue,
app.TotalBlockedAsyncIORequests, app.TotalBlockedAsyncIORequests,
app.Name, name,
) )
ch <- prometheus.MustNewConstMetric( ch <- prometheus.MustNewConstMetric(
c.TotalCGIRequests, c.TotalCGIRequests,
prometheus.CounterValue, prometheus.CounterValue,
app.TotalCGIRequests, app.TotalCGIRequests,
app.Name, name,
) )
ch <- prometheus.MustNewConstMetric( ch <- prometheus.MustNewConstMetric(
c.TotalConnectionAttemptsAllInstances, c.TotalConnectionAttemptsAllInstances,
prometheus.CounterValue, prometheus.CounterValue,
app.TotalConnectionAttemptsAllInstances, app.TotalConnectionAttemptsAllInstances,
app.Name, name,
) )
ch <- prometheus.MustNewConstMetric( ch <- prometheus.MustNewConstMetric(
c.TotalFilesReceived, c.TotalFilesReceived,
prometheus.CounterValue, prometheus.CounterValue,
app.TotalFilesReceived, app.TotalFilesReceived,
app.Name, name,
) )
ch <- prometheus.MustNewConstMetric( ch <- prometheus.MustNewConstMetric(
c.TotalFilesSent, c.TotalFilesSent,
prometheus.CounterValue, prometheus.CounterValue,
app.TotalFilesSent, app.TotalFilesSent,
app.Name, name,
) )
ch <- prometheus.MustNewConstMetric( ch <- prometheus.MustNewConstMetric(
c.TotalISAPIExtensionRequests, c.TotalISAPIExtensionRequests,
prometheus.CounterValue, prometheus.CounterValue,
app.TotalISAPIExtensionRequests, app.TotalISAPIExtensionRequests,
app.Name, name,
) )
ch <- prometheus.MustNewConstMetric( ch <- prometheus.MustNewConstMetric(
c.TotalLockedErrors, c.TotalLockedErrors,
prometheus.CounterValue, prometheus.CounterValue,
app.TotalLockedErrors, app.TotalLockedErrors,
app.Name, name,
) )
ch <- prometheus.MustNewConstMetric( ch <- prometheus.MustNewConstMetric(
c.TotalLogonAttempts, c.TotalLogonAttempts,
prometheus.CounterValue, prometheus.CounterValue,
app.TotalLogonAttempts, app.TotalLogonAttempts,
app.Name, name,
) )
ch <- prometheus.MustNewConstMetric( ch <- prometheus.MustNewConstMetric(
c.TotalNonAnonymousUsers, c.TotalNonAnonymousUsers,
prometheus.CounterValue, prometheus.CounterValue,
app.TotalNonAnonymousUsers, app.TotalNonAnonymousUsers,
app.Name, name,
) )
ch <- prometheus.MustNewConstMetric( ch <- prometheus.MustNewConstMetric(
c.TotalNotFoundErrors, c.TotalNotFoundErrors,
prometheus.CounterValue, prometheus.CounterValue,
app.TotalNotFoundErrors, app.TotalNotFoundErrors,
app.Name, name,
) )
ch <- prometheus.MustNewConstMetric( ch <- prometheus.MustNewConstMetric(
c.TotalRejectedAsyncIORequests, c.TotalRejectedAsyncIORequests,
prometheus.CounterValue, prometheus.CounterValue,
app.TotalRejectedAsyncIORequests, app.TotalRejectedAsyncIORequests,
app.Name, name,
) )
ch <- prometheus.MustNewConstMetric( ch <- prometheus.MustNewConstMetric(
c.TotalRequests, c.TotalRequests,
prometheus.CounterValue, prometheus.CounterValue,
app.TotalOtherRequests, app.TotalOtherRequests,
app.Name, name,
"other", "other",
) )
ch <- prometheus.MustNewConstMetric( ch <- prometheus.MustNewConstMetric(
c.TotalRequests, c.TotalRequests,
prometheus.CounterValue, prometheus.CounterValue,
app.TotalCopyRequests, app.TotalCopyRequests,
app.Name, name,
"COPY", "COPY",
) )
ch <- prometheus.MustNewConstMetric( ch <- prometheus.MustNewConstMetric(
c.TotalRequests, c.TotalRequests,
prometheus.CounterValue, prometheus.CounterValue,
app.TotalDeleteRequests, app.TotalDeleteRequests,
app.Name, name,
"DELETE", "DELETE",
) )
ch <- prometheus.MustNewConstMetric( ch <- prometheus.MustNewConstMetric(
c.TotalRequests, c.TotalRequests,
prometheus.CounterValue, prometheus.CounterValue,
app.TotalGetRequests, app.TotalGetRequests,
app.Name, name,
"GET", "GET",
) )
ch <- prometheus.MustNewConstMetric( ch <- prometheus.MustNewConstMetric(
c.TotalRequests, c.TotalRequests,
prometheus.CounterValue, prometheus.CounterValue,
app.TotalHeadRequests, app.TotalHeadRequests,
app.Name, name,
"HEAD", "HEAD",
) )
ch <- prometheus.MustNewConstMetric( ch <- prometheus.MustNewConstMetric(
c.TotalRequests, c.TotalRequests,
prometheus.CounterValue, prometheus.CounterValue,
app.TotalLockRequests, app.TotalLockRequests,
app.Name, name,
"LOCK", "LOCK",
) )
ch <- prometheus.MustNewConstMetric( ch <- prometheus.MustNewConstMetric(
c.TotalRequests, c.TotalRequests,
prometheus.CounterValue, prometheus.CounterValue,
app.TotalMkcolRequests, app.TotalMkcolRequests,
app.Name, name,
"MKCOL", "MKCOL",
) )
ch <- prometheus.MustNewConstMetric( ch <- prometheus.MustNewConstMetric(
c.TotalRequests, c.TotalRequests,
prometheus.CounterValue, prometheus.CounterValue,
app.TotalMoveRequests, app.TotalMoveRequests,
app.Name, name,
"MOVE", "MOVE",
) )
ch <- prometheus.MustNewConstMetric( ch <- prometheus.MustNewConstMetric(
c.TotalRequests, c.TotalRequests,
prometheus.CounterValue, prometheus.CounterValue,
app.TotalOptionsRequests, app.TotalOptionsRequests,
app.Name, name,
"OPTIONS", "OPTIONS",
) )
ch <- prometheus.MustNewConstMetric( ch <- prometheus.MustNewConstMetric(
c.TotalRequests, c.TotalRequests,
prometheus.CounterValue, prometheus.CounterValue,
app.TotalPostRequests, app.TotalPostRequests,
app.Name, name,
"POST", "POST",
) )
ch <- prometheus.MustNewConstMetric( ch <- prometheus.MustNewConstMetric(
c.TotalRequests, c.TotalRequests,
prometheus.CounterValue, prometheus.CounterValue,
app.TotalPropfindRequests, app.TotalPropfindRequests,
app.Name, name,
"PROPFIND", "PROPFIND",
) )
ch <- prometheus.MustNewConstMetric( ch <- prometheus.MustNewConstMetric(
c.TotalRequests, c.TotalRequests,
prometheus.CounterValue, prometheus.CounterValue,
app.TotalProppatchRequests, app.TotalProppatchRequests,
app.Name, name,
"PROPPATCH", "PROPPATCH",
) )
ch <- prometheus.MustNewConstMetric( ch <- prometheus.MustNewConstMetric(
c.TotalRequests, c.TotalRequests,
prometheus.CounterValue, prometheus.CounterValue,
app.TotalPutRequests, app.TotalPutRequests,
app.Name, name,
"PUT", "PUT",
) )
ch <- prometheus.MustNewConstMetric( ch <- prometheus.MustNewConstMetric(
c.TotalRequests, c.TotalRequests,
prometheus.CounterValue, prometheus.CounterValue,
app.TotalSearchRequests, app.TotalSearchRequests,
app.Name, name,
"SEARCH", "SEARCH",
) )
ch <- prometheus.MustNewConstMetric( ch <- prometheus.MustNewConstMetric(
c.TotalRequests, c.TotalRequests,
prometheus.CounterValue, prometheus.CounterValue,
app.TotalTraceRequests, app.TotalTraceRequests,
app.Name, name,
"TRACE", "TRACE",
) )
ch <- prometheus.MustNewConstMetric( ch <- prometheus.MustNewConstMetric(
c.TotalRequests, c.TotalRequests,
prometheus.CounterValue, prometheus.CounterValue,
app.TotalUnlockRequests, app.TotalUnlockRequests,
app.Name, name,
"UNLOCK", "UNLOCK",
) )
} }
@@ -1267,10 +1318,12 @@ func (c *IISCollector) collectAPP_POOL_WAS(ctx *ScrapeContext, ch chan<- prometh
return nil, err return nil, err
} }
for _, app := range APP_POOL_WAS { appPoolDeDuplicated := dedupIISNames(APP_POOL_WAS)
if app.Name == "_Total" ||
c.appExcludePattern.MatchString(app.Name) || for name, app := range appPoolDeDuplicated {
!c.appIncludePattern.MatchString(app.Name) { if name == "_Total" ||
c.appExcludePattern.MatchString(name) ||
!c.appIncludePattern.MatchString(name) {
continue continue
} }
@@ -1283,7 +1336,7 @@ func (c *IISCollector) collectAPP_POOL_WAS(ctx *ScrapeContext, ch chan<- prometh
c.CurrentApplicationPoolState, c.CurrentApplicationPoolState,
prometheus.GaugeValue, prometheus.GaugeValue,
isCurrentState, isCurrentState,
app.Name, name,
label, label,
) )
} }
@@ -1292,73 +1345,73 @@ func (c *IISCollector) collectAPP_POOL_WAS(ctx *ScrapeContext, ch chan<- prometh
c.CurrentApplicationPoolUptime, c.CurrentApplicationPoolUptime,
prometheus.GaugeValue, prometheus.GaugeValue,
app.CurrentApplicationPoolUptime, app.CurrentApplicationPoolUptime,
app.Name, name,
) )
ch <- prometheus.MustNewConstMetric( ch <- prometheus.MustNewConstMetric(
c.CurrentWorkerProcesses, c.CurrentWorkerProcesses,
prometheus.GaugeValue, prometheus.GaugeValue,
app.CurrentWorkerProcesses, app.CurrentWorkerProcesses,
app.Name, name,
) )
ch <- prometheus.MustNewConstMetric( ch <- prometheus.MustNewConstMetric(
c.MaximumWorkerProcesses, c.MaximumWorkerProcesses,
prometheus.GaugeValue, prometheus.GaugeValue,
app.MaximumWorkerProcesses, app.MaximumWorkerProcesses,
app.Name, name,
) )
ch <- prometheus.MustNewConstMetric( ch <- prometheus.MustNewConstMetric(
c.RecentWorkerProcessFailures, c.RecentWorkerProcessFailures,
prometheus.GaugeValue, prometheus.GaugeValue,
app.RecentWorkerProcessFailures, app.RecentWorkerProcessFailures,
app.Name, name,
) )
ch <- prometheus.MustNewConstMetric( ch <- prometheus.MustNewConstMetric(
c.TimeSinceLastWorkerProcessFailure, c.TimeSinceLastWorkerProcessFailure,
prometheus.GaugeValue, prometheus.GaugeValue,
app.TimeSinceLastWorkerProcessFailure, app.TimeSinceLastWorkerProcessFailure,
app.Name, name,
) )
ch <- prometheus.MustNewConstMetric( ch <- prometheus.MustNewConstMetric(
c.TotalApplicationPoolRecycles, c.TotalApplicationPoolRecycles,
prometheus.CounterValue, prometheus.CounterValue,
app.TotalApplicationPoolRecycles, app.TotalApplicationPoolRecycles,
app.Name, name,
) )
ch <- prometheus.MustNewConstMetric( ch <- prometheus.MustNewConstMetric(
c.TotalApplicationPoolUptime, c.TotalApplicationPoolUptime,
prometheus.CounterValue, prometheus.CounterValue,
app.TotalApplicationPoolUptime, app.TotalApplicationPoolUptime,
app.Name, name,
) )
ch <- prometheus.MustNewConstMetric( ch <- prometheus.MustNewConstMetric(
c.TotalWorkerProcessesCreated, c.TotalWorkerProcessesCreated,
prometheus.CounterValue, prometheus.CounterValue,
app.TotalWorkerProcessesCreated, app.TotalWorkerProcessesCreated,
app.Name, name,
) )
ch <- prometheus.MustNewConstMetric( ch <- prometheus.MustNewConstMetric(
c.TotalWorkerProcessFailures, c.TotalWorkerProcessFailures,
prometheus.CounterValue, prometheus.CounterValue,
app.TotalWorkerProcessFailures, app.TotalWorkerProcessFailures,
app.Name, name,
) )
ch <- prometheus.MustNewConstMetric( ch <- prometheus.MustNewConstMetric(
c.TotalWorkerProcessPingFailures, c.TotalWorkerProcessPingFailures,
prometheus.CounterValue, prometheus.CounterValue,
app.TotalWorkerProcessPingFailures, app.TotalWorkerProcessPingFailures,
app.Name, name,
) )
ch <- prometheus.MustNewConstMetric( ch <- prometheus.MustNewConstMetric(
c.TotalWorkerProcessShutdownFailures, c.TotalWorkerProcessShutdownFailures,
prometheus.CounterValue, prometheus.CounterValue,
app.TotalWorkerProcessShutdownFailures, app.TotalWorkerProcessShutdownFailures,
app.Name, name,
) )
ch <- prometheus.MustNewConstMetric( ch <- prometheus.MustNewConstMetric(
c.TotalWorkerProcessStartupFailures, c.TotalWorkerProcessStartupFailures,
prometheus.CounterValue, prometheus.CounterValue,
app.TotalWorkerProcessStartupFailures, app.TotalWorkerProcessStartupFailures,
app.Name, name,
) )
} }
@@ -1442,16 +1495,23 @@ func (c *IISCollector) collectW3SVC_W3WP(ctx *ScrapeContext, ch chan<- prometheu
return nil, err return nil, err
} }
for _, app := range W3SVC_W3WP { w3svcW3WPDeduplicated := dedupIISNames(W3SVC_W3WP)
for w3Name, app := range w3svcW3WPDeduplicated {
// Extract the apppool name from the format <PID>_<NAME> // Extract the apppool name from the format <PID>_<NAME>
pid := workerProcessNameExtractor.ReplaceAllString(app.Name, "$1") pid := workerProcessNameExtractor.ReplaceAllString(w3Name, "$1")
name := workerProcessNameExtractor.ReplaceAllString(app.Name, "$2") name := workerProcessNameExtractor.ReplaceAllString(w3Name, "$2")
if name == "" || name == "_Total" || if name == "" || name == "_Total" ||
c.appExcludePattern.MatchString(name) || c.appExcludePattern.MatchString(name) ||
!c.appIncludePattern.MatchString(name) { !c.appIncludePattern.MatchString(name) {
continue continue
} }
// Duplicate instances are suffixed # with an index number. These should be ignored
if strings.Contains(app.Name, "#") {
continue
}
ch <- prometheus.MustNewConstMetric( ch <- prometheus.MustNewConstMetric(
c.Threads, c.Threads,
prometheus.GaugeValue, prometheus.GaugeValue,
@@ -1694,10 +1754,12 @@ func (c *IISCollector) collectW3SVC_W3WP(ctx *ScrapeContext, ch chan<- prometheu
return nil, err return nil, err
} }
for _, app := range W3SVC_W3WP_IIS8 { w3svcW3WPIIS8Deduplicated := dedupIISNames(W3SVC_W3WP_IIS8)
for w3Name, app := range w3svcW3WPIIS8Deduplicated {
// Extract the apppool name from the format <PID>_<NAME> // Extract the apppool name from the format <PID>_<NAME>
pid := workerProcessNameExtractor.ReplaceAllString(app.Name, "$1") pid := workerProcessNameExtractor.ReplaceAllString(w3Name, "$1")
name := workerProcessNameExtractor.ReplaceAllString(app.Name, "$2") name := workerProcessNameExtractor.ReplaceAllString(w3Name, "$2")
if name == "" || name == "_Total" || if name == "" || name == "_Total" ||
c.appExcludePattern.MatchString(name) || c.appExcludePattern.MatchString(name) ||
!c.appIncludePattern.MatchString(name) { !c.appIncludePattern.MatchString(name) {

View File

@@ -1,9 +1,49 @@
package collector package collector
import ( import (
"reflect"
"testing" "testing"
) )
func BenchmarkIISCollector(b *testing.B) { func BenchmarkIISCollector(b *testing.B) {
benchmarkCollector(b, "iis", newIISCollector) benchmarkCollector(b, "iis", newIISCollector)
} }
func TestIISDeduplication(t *testing.T) {
start := []perflibAPP_POOL_WAS{
{
Name: "foo",
Frequency_Object: 1,
},
{
Name: "foo1#999",
Frequency_Object: 2,
},
{
Name: "foo#2",
Frequency_Object: 3,
},
{
Name: "bar$2",
Frequency_Object: 4,
},
{
Name: "bar_2",
Frequency_Object: 5,
},
}
var expected = make(map[string]perflibAPP_POOL_WAS)
// Should be deduplicated from "foo#2"
expected["foo"] = perflibAPP_POOL_WAS{Name: "foo#2", Frequency_Object: 3}
// Map key should have suffix stripped, but struct name field should be unchanged
expected["foo1"] = perflibAPP_POOL_WAS{Name: "foo1#999", Frequency_Object: 2}
// Map key and Name should be identical, as there is no suffix starting with "#"
expected["bar$2"] = perflibAPP_POOL_WAS{Name: "bar$2", Frequency_Object: 4}
// Map key and Name should be identical, as there is no suffix starting with "#"
expected["bar_2"] = perflibAPP_POOL_WAS{Name: "bar_2", Frequency_Object: 5}
deduplicated := dedupIISNames(start)
if !reflect.DeepEqual(expected, deduplicated) {
t.Errorf("Flattened values do not match!\nExpected result: %+v\nActual result: %+v", expected, deduplicated)
}
}