Skip to content

Commit

Permalink
msmq: Use Performance Counter instead WMI (#1766)
Browse files Browse the repository at this point in the history
Signed-off-by: Jan-Otto Kröpke <[email protected]>
  • Loading branch information
jkroepke authored Nov 24, 2024
1 parent e812584 commit 86ace68
Show file tree
Hide file tree
Showing 4 changed files with 67 additions and 84 deletions.
26 changes: 11 additions & 15 deletions docs/collector.msmq.md
Original file line number Diff line number Diff line change
Expand Up @@ -2,26 +2,22 @@

The msmq collector exposes metrics about the queues on a MSMQ server

|||
-|-
Metric name prefix | `msmq`
Classes | `Win32_PerfRawData_MSMQ_MSMQQueue`
Enabled by default? | No
| | |
|---------------------|----------------------|
| Metric name prefix | `msmq` |
| Spource | Performance Counters |
| Enabled by default? | No |

## Flags

### `--collector.msmq.msmq-where`

A WMI filter on which queues to include. `%` is a wildcard, and can be used to match on substrings.

## Metrics

Name | Description | Type | Labels
-----|-------------|------|-------
`windows_msmq_bytes_in_journal_queue` | Size of queue journal in bytes | gauge | `name`
`windows_msmq_bytes_in_queue` | Size of queue in bytes | gauge | `name`
`windows_msmq_messages_in_journal_queue` | Count messages in queue journal | gauge | `name`
`windows_msmq_messages_in_queue` | Count messages in queue | gauge | `name`
| Name | Description | Type | Labels |
|------------------------------------------|---------------------------------|-------|--------|
| `windows_msmq_bytes_in_journal_queue` | Size of queue journal in bytes | gauge | `name` |
| `windows_msmq_bytes_in_queue` | Size of queue in bytes | gauge | `name` |
| `windows_msmq_messages_in_journal_queue` | Count messages in queue journal | gauge | `name` |
| `windows_msmq_messages_in_queue` | Count messages in queue | gauge | `name` |

### Example metric
_This collector does not yet have explained examples, we would appreciate your help adding them!_
Expand Down
21 changes: 21 additions & 0 deletions internal/collector/msmq/const.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,21 @@
// Copyright 2024 The Prometheus Authors
// Licensed under the Apache License, Version 2.0 (the "License");
// you may not use this file except in compliance with the License.
// You may obtain a copy of the License at
//
// http://www.apache.org/licenses/LICENSE-2.0
//
// Unless required by applicable law or agreed to in writing, software
// distributed under the License is distributed on an "AS IS" BASIS,
// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
// See the License for the specific language governing permissions and
// limitations under the License.

package msmq

const (
BytesInJournalQueue = "Bytes in Journal Queue"
BytesInQueue = "Bytes in Queue"
MessagesInJournalQueue = "Messages in Journal Queue"
MessagesInQueue = "Messages in Queue"
)
88 changes: 27 additions & 61 deletions internal/collector/msmq/msmq.go
Original file line number Diff line number Diff line change
Expand Up @@ -3,32 +3,26 @@
package msmq

import (
"errors"
"fmt"
"log/slog"
"strings"

"github.com/alecthomas/kingpin/v2"
"github.com/prometheus-community/windows_exporter/internal/mi"
"github.com/prometheus-community/windows_exporter/internal/perfdata"
"github.com/prometheus-community/windows_exporter/internal/types"
"github.com/prometheus-community/windows_exporter/internal/utils"
"github.com/prometheus/client_golang/prometheus"
)

const Name = "msmq"

type Config struct {
QueryWhereClause *string `yaml:"query_where_clause"`
}
type Config struct{}

var ConfigDefaults = Config{
QueryWhereClause: utils.ToPTR(""),
}
var ConfigDefaults = Config{}

// A Collector is a Prometheus Collector for WMI Win32_PerfRawData_MSMQ_MSMQQueue metrics.
type Collector struct {
config Config
miSession *mi.Session
config Config
perfDataCollector *perfdata.Collector

bytesInJournalQueue *prometheus.Desc
bytesInQueue *prometheus.Desc
Expand All @@ -41,26 +35,18 @@ func New(config *Config) *Collector {
config = &ConfigDefaults
}

if config.QueryWhereClause == nil {
config.QueryWhereClause = ConfigDefaults.QueryWhereClause
}

c := &Collector{
config: *config,
}

return c
}

func NewWithFlags(app *kingpin.Application) *Collector {
func NewWithFlags(_ *kingpin.Application) *Collector {
c := &Collector{
config: ConfigDefaults,
}

app.Flag("collector.msmq.msmq-where", "WQL 'where' clause to use in WMI metrics query. "+
"Limits the response to the msmqs you specify and reduces the size of the response.").
Default(*c.config.QueryWhereClause).StringVar(c.config.QueryWhereClause)

return c
}

Expand All @@ -72,17 +58,17 @@ func (c *Collector) Close() error {
return nil
}

func (c *Collector) Build(logger *slog.Logger, miSession *mi.Session) error {
logger = logger.With(slog.String("collector", Name))
func (c *Collector) Build(_ *slog.Logger, _ *mi.Session) error {
var err error

if miSession == nil {
return errors.New("miSession is nil")
}

c.miSession = miSession

if *c.config.QueryWhereClause == "" {
logger.Warn("No where-clause specified for msmq collector. This will generate a very large number of metrics!")
c.perfDataCollector, err = perfdata.NewCollector("MSMQ Queue", perfdata.InstancesAll, []string{
BytesInJournalQueue,
BytesInQueue,
MessagesInJournalQueue,
MessagesInQueue,
})
if err != nil {
return fmt.Errorf("failed to create MSMQ Queue collector: %w", err)
}

c.bytesInJournalQueue = prometheus.NewDesc(
Expand Down Expand Up @@ -113,61 +99,41 @@ func (c *Collector) Build(logger *slog.Logger, miSession *mi.Session) error {
return nil
}

type msmqQueue struct {
Name string `mi:"Name"`

BytesInJournalQueue uint64 `mi:"BytesInJournalQueue"`
BytesInQueue uint64 `mi:"BytesInQueue"`
MessagesInJournalQueue uint64 `mi:"MessagesInJournalQueue"`
MessagesInQueue uint64 `mi:"MessagesInQueue"`
}

// Collect sends the metric values for each metric
// to the provided prometheus Metric channel.
func (c *Collector) Collect(ch chan<- prometheus.Metric) error {
var dst []msmqQueue

query := "SELECT * FROM Win32_PerfRawData_MSMQ_MSMQQueue"
if *c.config.QueryWhereClause != "" {
query += " WHERE " + *c.config.QueryWhereClause
}

queryExpression, err := mi.NewQuery(query)
perfData, err := c.perfDataCollector.Collect()
if err != nil {
return fmt.Errorf("failed to create WMI query: %w", err)
}

if err := c.miSession.Query(&dst, mi.NamespaceRootCIMv2, queryExpression); err != nil {
return fmt.Errorf("WMI query failed: %w", err)
return fmt.Errorf("failed to collect MSMQ Queue metrics: %w", err)
}

for _, msmq := range dst {
for name, data := range perfData {
ch <- prometheus.MustNewConstMetric(
c.bytesInJournalQueue,
prometheus.GaugeValue,
float64(msmq.BytesInJournalQueue),
strings.ToLower(msmq.Name),
data[BytesInJournalQueue].FirstValue,
name,
)

ch <- prometheus.MustNewConstMetric(
c.bytesInQueue,
prometheus.GaugeValue,
float64(msmq.BytesInQueue),
strings.ToLower(msmq.Name),
data[BytesInQueue].FirstValue,
name,
)

ch <- prometheus.MustNewConstMetric(
c.messagesInJournalQueue,
prometheus.GaugeValue,
float64(msmq.MessagesInJournalQueue),
strings.ToLower(msmq.Name),
data[MessagesInJournalQueue].FirstValue,
name,
)

ch <- prometheus.MustNewConstMetric(
c.messagesInQueue,
prometheus.GaugeValue,
float64(msmq.MessagesInQueue),
strings.ToLower(msmq.Name),
data[MessagesInQueue].FirstValue,
name,
)
}

Expand Down
16 changes: 8 additions & 8 deletions internal/collector/smb/smb.go
Original file line number Diff line number Diff line change
Expand Up @@ -96,19 +96,19 @@ func (c *Collector) Build(_ *slog.Logger, _ *mi.Session) error {
nil,
)
c.writeRequests = prometheus.NewDesc(
prometheus.BuildFQName(types.Namespace, Name, "server_shares_write_requests_count"),
prometheus.BuildFQName(types.Namespace, Name, "server_shares_write_requests_count_total"),
"Writes requests on the SMB Server Share",
[]string{"share"},
nil,
)
c.readRequests = prometheus.NewDesc(
prometheus.BuildFQName(types.Namespace, Name, "server_shares_read_requests_count"),
prometheus.BuildFQName(types.Namespace, Name, "server_shares_read_requests_count_total"),
"Read requests on the SMB Server Share",
[]string{"share"},
nil,
)
c.metadataRequests = prometheus.NewDesc(
prometheus.BuildFQName(types.Namespace, Name, "server_shares_metadata_requests_count"),
prometheus.BuildFQName(types.Namespace, Name, "server_shares_metadata_requests_count_total"),
"Metadata requests on the SMB Server Share",
[]string{"share"},
nil,
Expand All @@ -120,7 +120,7 @@ func (c *Collector) Build(_ *slog.Logger, _ *mi.Session) error {
nil,
)
c.filesOpened = prometheus.NewDesc(
prometheus.BuildFQName(types.Namespace, Name, "server_shares_filed_opened_count"),
prometheus.BuildFQName(types.Namespace, Name, "server_shares_filed_opened_count_total"),
"Files opened on the SMB Server Share",
[]string{"share"},
nil,
Expand Down Expand Up @@ -160,21 +160,21 @@ func (c *Collector) Collect(ch chan<- prometheus.Metric) error {

ch <- prometheus.MustNewConstMetric(
c.writeRequests,
prometheus.GaugeValue,
prometheus.CounterValue,
data[writeRequests].FirstValue,
share,
)

ch <- prometheus.MustNewConstMetric(
c.readRequests,
prometheus.GaugeValue,
prometheus.CounterValue,
data[readRequests].FirstValue,
share,
)

ch <- prometheus.MustNewConstMetric(
c.metadataRequests,
prometheus.GaugeValue,
prometheus.CounterValue,
data[metadataRequests].FirstValue,
share,
)
Expand All @@ -188,7 +188,7 @@ func (c *Collector) Collect(ch chan<- prometheus.Metric) error {

ch <- prometheus.MustNewConstMetric(
c.filesOpened,
prometheus.GaugeValue,
prometheus.CounterValue,
data[filesOpened].FirstValue,
share,
)
Expand Down

0 comments on commit 86ace68

Please sign in to comment.