Skip to content

Commit

Permalink
Merge pull request #372 from callvirtual/features/sql-transactions-co…
Browse files Browse the repository at this point in the history
…llector

Add support for MSSQL Transactions counters
  • Loading branch information
carlpett authored Aug 4, 2019
2 parents e880889 + 823ffb7 commit d01c669
Show file tree
Hide file tree
Showing 3 changed files with 247 additions and 9 deletions.
3 changes: 2 additions & 1 deletion .gitignore
Original file line number Diff line number Diff line change
Expand Up @@ -3,4 +3,5 @@ VERSION
*.swp
*.un~
output/
.idea
.vscode
.idea
236 changes: 230 additions & 6 deletions collector/mssql.go
Original file line number Diff line number Diff line change
Expand Up @@ -91,7 +91,7 @@ func mssqlBuildWMIInstanceClass(suffix string, instance string) string {
type mssqlCollectorsMap map[string]mssqlCollectorFunc

func mssqlAvailableClassCollectors() string {
return "accessmethods,availreplica,bufman,databases,dbreplica,genstats,locks,memmgr,sqlstats,sqlerrors"
return "accessmethods,availreplica,bufman,databases,dbreplica,genstats,locks,memmgr,sqlstats,sqlerrors,transactions"
}

func (c *MSSQLCollector) getMSSQLCollectors() mssqlCollectorsMap {
Expand All @@ -106,6 +106,7 @@ func (c *MSSQLCollector) getMSSQLCollectors() mssqlCollectorsMap {
mssqlCollectors["memmgr"] = c.collectMemoryManager
mssqlCollectors["sqlstats"] = c.collectSQLStats
mssqlCollectors["sqlerrors"] = c.collectSQLErrors
mssqlCollectors["transactions"] = c.collectTransactions

return mssqlCollectors
}
Expand Down Expand Up @@ -360,7 +361,22 @@ type MSSQLCollector struct {
SQLStatsUnsafeAutoParams *prometheus.Desc

// Win32_PerfRawData_{instance}_SQLServerSQLErrors
SQLErrorsTotal *prometheus.Desc
SQLErrorsTotal *prometheus.Desc

// Win32_PerfRawData_{instance}_SQLServerTransactions
TransactionsTempDbFreeSpaceBytes *prometheus.Desc
TransactionsLongestTransactionRunningSeconds *prometheus.Desc
TransactionsNonSnapshotVersionActiveTotal *prometheus.Desc
TransactionsSnapshotActiveTotal *prometheus.Desc
TransactionsActiveTotal *prometheus.Desc
TransactionsUpdateConflictsTotal *prometheus.Desc
TransactionsUpdateSnapshotActiveTotal *prometheus.Desc
TransactionsVersionCleanupRateBytes *prometheus.Desc
TransactionsVersionGenerationRateBytes *prometheus.Desc
TransactionsVersionStoreSizeBytes *prometheus.Desc
TransactionsVersionStoreUnits *prometheus.Desc
TransactionsVersionStoreCreationUnits *prometheus.Desc
TransactionsVersionStoreTruncationUnits *prometheus.Desc

mssqlInstances mssqlInstancesType
mssqlCollectors mssqlCollectorsMap
Expand Down Expand Up @@ -1649,6 +1665,86 @@ func NewMSSQLCollector() (Collector, error) {
nil,
),

// Win32_PerfRawData_{instance}_SQLServerTransactions
TransactionsTempDbFreeSpaceBytes: prometheus.NewDesc(
prometheus.BuildFQName(Namespace, subsystem, "transactions_tempdb_free_space_bytes"),
"(Transactions.FreeSpaceInTempDbKB)",
[]string{"instance"},
nil,
),
TransactionsLongestTransactionRunningSeconds: prometheus.NewDesc(
prometheus.BuildFQName(Namespace, subsystem, "transactions_longest_transaction_running_seconds"),
"(Transactions.LongestTransactionRunningTime)",
[]string{"instance"},
nil,
),
TransactionsNonSnapshotVersionActiveTotal: prometheus.NewDesc(
prometheus.BuildFQName(Namespace, subsystem, "transactions_nonsnapshot_version_active_total"),
"(Transactions.NonSnapshotVersionTransactions)",
[]string{"instance"},
nil,
),
TransactionsSnapshotActiveTotal: prometheus.NewDesc(
prometheus.BuildFQName(Namespace, subsystem, "transactions_snapshot_active_total"),
"(Transactions.SnapshotTransactions)",
[]string{"instance"},
nil,
),
TransactionsActiveTotal: prometheus.NewDesc(
prometheus.BuildFQName(Namespace, subsystem, "transactions_active_total"),
"(Transactions.Transactions)",
[]string{"instance"},
nil,
),
TransactionsUpdateConflictsTotal: prometheus.NewDesc(
prometheus.BuildFQName(Namespace, subsystem, "transactions_update_conflicts_total"),
"(Transactions.UpdateConflictRatio)",
[]string{"instance"},
nil,
),
TransactionsUpdateSnapshotActiveTotal: prometheus.NewDesc(
prometheus.BuildFQName(Namespace, subsystem, "transactions_update_snapshot_active_total"),
"(Transactions.UpdateSnapshotTransactions)",
[]string{"instance"},
nil,
),
TransactionsVersionCleanupRateBytes: prometheus.NewDesc(
prometheus.BuildFQName(Namespace, subsystem, "transactions_version_cleanup_rate_bytes"),
"(Transactions.VersionCleanupRateKBs)",
[]string{"instance"},
nil,
),
TransactionsVersionGenerationRateBytes: prometheus.NewDesc(
prometheus.BuildFQName(Namespace, subsystem, "transactions_version_generation_rate_bytes"),
"(Transactions.VersionGenerationRateKBs)",
[]string{"instance"},
nil,
),
TransactionsVersionStoreSizeBytes: prometheus.NewDesc(
prometheus.BuildFQName(Namespace, subsystem, "transactions_version_store_size_bytes"),
"(Transactions.VersionStoreSizeKB)",
[]string{"instance"},
nil,
),
TransactionsVersionStoreUnits: prometheus.NewDesc(
prometheus.BuildFQName(Namespace, subsystem, "transactions_version_store_units"),
"(Transactions.VersionStoreUnitCount)",
[]string{"instance"},
nil,
),
TransactionsVersionStoreCreationUnits: prometheus.NewDesc(
prometheus.BuildFQName(Namespace, subsystem, "transactions_version_store_creation_units"),
"(Transactions.VersionStoreUnitCreation)",
[]string{"instance"},
nil,
),
TransactionsVersionStoreTruncationUnits: prometheus.NewDesc(
prometheus.BuildFQName(Namespace, subsystem, "transactions_version_store_truncation_units"),
"(Transactions.VersionStoreUnitTruncation)",
[]string{"instance"},
nil,
),

mssqlInstances: getMSSQLInstances(),
}

Expand Down Expand Up @@ -3572,8 +3668,8 @@ func (c *MSSQLCollector) collectSQLStats(ch chan<- prometheus.Metric, sqlInstanc
}

type win32PerfRawDataSQLServerSQLErrors struct {
Name string
Errorssec uint64
Name string
ErrorsPersec uint64
}

// Win32_PerfRawData_MSSQLSERVER_SQLServerErrors docs:
Expand All @@ -3594,10 +3690,138 @@ func (c *MSSQLCollector) collectSQLErrors(ch chan<- prometheus.Metric, sqlInstan
ch <- prometheus.MustNewConstMetric(
c.SQLErrorsTotal,
prometheus.CounterValue,
float64(v.Errorssec),
float64(v.ErrorsPersec),
sqlInstance, resource,
)
}

return nil, nil
}
}

type win32PerfRawDataSqlServerTransactions struct {
FreeSpaceintempdbKB uint64
LongestTransactionRunningTime uint64
NonSnapshotVersionTransactions uint64
SnapshotTransactions uint64
Transactions uint64
Updateconflictratio uint64
UpdateSnapshotTransactions uint64
VersionCleanuprateKBPers uint64
VersionGenerationrateKBPers uint64
VersionStoreSizeKB uint64
VersionStoreunitcount uint64
VersionStoreunitcreation uint64
VersionStoreunittruncation uint64
}

// Win32_PerfRawData_MSSQLSERVER_Transactions docs:
// - https://docs.microsoft.com/en-us/sql/relational-databases/performance-monitor/sql-server-transactions-object
func (c *MSSQLCollector) collectTransactions(ch chan<- prometheus.Metric, sqlInstance string) (*prometheus.Desc, error) {
var dst []win32PerfRawDataSqlServerTransactions
log.Debugf("mssql_transactions collector iterating sql instance %s.", sqlInstance)

class := mssqlBuildWMIInstanceClass("Transactions", sqlInstance)
q := queryAllForClass(&dst, class)
if err := wmi.Query(q, &dst); err != nil {
return nil, err
}

if len(dst) == 0 {
return nil, errors.New("WMI query returned empty result set")
}

v := dst[0]

ch <- prometheus.MustNewConstMetric(
c.TransactionsTempDbFreeSpaceBytes,
prometheus.GaugeValue,
float64(v.FreeSpaceintempdbKB*1024),
sqlInstance,
)

ch <- prometheus.MustNewConstMetric(
c.TransactionsLongestTransactionRunningSeconds,
prometheus.GaugeValue,
float64(v.LongestTransactionRunningTime),
sqlInstance,
)

ch <- prometheus.MustNewConstMetric(
c.TransactionsNonSnapshotVersionActiveTotal,
prometheus.CounterValue,
float64(v.NonSnapshotVersionTransactions),
sqlInstance,
)

ch <- prometheus.MustNewConstMetric(
c.TransactionsSnapshotActiveTotal,
prometheus.CounterValue,
float64(v.SnapshotTransactions),
sqlInstance,
)

ch <- prometheus.MustNewConstMetric(
c.TransactionsActiveTotal,
prometheus.CounterValue,
float64(v.Transactions),
sqlInstance,
)

ch <- prometheus.MustNewConstMetric(
c.TransactionsUpdateConflictsTotal,
prometheus.CounterValue,
float64(v.Updateconflictratio),
sqlInstance,
)

ch <- prometheus.MustNewConstMetric(
c.TransactionsUpdateSnapshotActiveTotal,
prometheus.CounterValue,
float64(v.UpdateSnapshotTransactions),
sqlInstance,
)

ch <- prometheus.MustNewConstMetric(
c.TransactionsVersionCleanupRateBytes,
prometheus.GaugeValue,
float64(v.VersionCleanuprateKBPers*1024),
sqlInstance,
)

ch <- prometheus.MustNewConstMetric(
c.TransactionsVersionGenerationRateBytes,
prometheus.GaugeValue,
float64(v.VersionGenerationrateKBPers*1024),
sqlInstance,
)

ch <- prometheus.MustNewConstMetric(
c.TransactionsVersionStoreSizeBytes,
prometheus.GaugeValue,
float64(v.VersionStoreSizeKB*1024),
sqlInstance,
)

ch <- prometheus.MustNewConstMetric(
c.TransactionsVersionStoreUnits,
prometheus.CounterValue,
float64(v.VersionStoreunitcount),
sqlInstance,
)

ch <- prometheus.MustNewConstMetric(
c.TransactionsVersionStoreCreationUnits,
prometheus.CounterValue,
float64(v.VersionStoreunitcreation),
sqlInstance,
)

ch <- prometheus.MustNewConstMetric(
c.TransactionsVersionStoreTruncationUnits,
prometheus.CounterValue,
float64(v.VersionStoreunittruncation),
sqlInstance,
)

return nil, nil
}
17 changes: 15 additions & 2 deletions docs/collector.mssql.md
Original file line number Diff line number Diff line change
Expand Up @@ -5,14 +5,14 @@ The mssql collector exposes metrics about the MSSQL server
|||
-|-
Metric name prefix | `mssql`
Classes | [`Win32_PerfRawData_MSSQLSERVER_SQLServerAccessMethods`](https://docs.microsoft.com/en-us/sql/relational-databases/performance-monitor/sql-server-access-methods-object)<br/>[`Win32_PerfRawData_MSSQLSERVER_SQLServerAvailabilityReplica`](https://docs.microsoft.com/en-us/sql/relational-databases/performance-monitor/sql-server-availability-replica)<br/>[`Win32_PerfRawData_MSSQLSERVER_SQLServerBufferManager`](https://docs.microsoft.com/en-us/sql/relational-databases/performance-monitor/sql-server-buffer-manager-object)<br/>[`Win32_PerfRawData_MSSQLSERVER_SQLServerDatabaseReplica`](https://docs.microsoft.com/en-us/sql/relational-databases/performance-monitor/sql-server-database-replica)<br/>[`Win32_PerfRawData_MSSQLSERVER_SQLServerDatabases`](https://docs.microsoft.com/en-us/sql/relational-databases/performance-monitor/sql-server-databases-object?view=sql-server-2017)<br/>[`Win32_PerfRawData_MSSQLSERVER_SQLServerGeneralStatistics`](https://docs.microsoft.com/en-us/sql/relational-databases/performance-monitor/sql-server-general-statistics-object)<br/>[`Win32_PerfRawData_MSSQLSERVER_SQLServerLocks`](https://docs.microsoft.com/en-us/sql/relational-databases/performance-monitor/sql-server-locks-object)<br/>[`Win32_PerfRawData_MSSQLSERVER_SQLServerMemoryManager`](https://docs.microsoft.com/en-us/sql/relational-databases/performance-monitor/sql-server-memory-manager-object)<br/>[`Win32_PerfRawData_MSSQLSERVER_SQLServerSQLStatistics`](https://docs.microsoft.com/en-us/sql/relational-databases/performance-monitor/sql-server-sql-statistics-object)<br/>[`Win32_PerfRawData_MSSQLSERVER_SQLServerSQLErrors`](https://docs.microsoft.com/en-us/sql/relational-databases/performance-monitor/sql-server-sql-errors-object)
Classes | [`Win32_PerfRawData_MSSQLSERVER_SQLServerAccessMethods`](https://docs.microsoft.com/en-us/sql/relational-databases/performance-monitor/sql-server-access-methods-object)<br/>[`Win32_PerfRawData_MSSQLSERVER_SQLServerAvailabilityReplica`](https://docs.microsoft.com/en-us/sql/relational-databases/performance-monitor/sql-server-availability-replica)<br/>[`Win32_PerfRawData_MSSQLSERVER_SQLServerBufferManager`](https://docs.microsoft.com/en-us/sql/relational-databases/performance-monitor/sql-server-buffer-manager-object)<br/>[`Win32_PerfRawData_MSSQLSERVER_SQLServerDatabaseReplica`](https://docs.microsoft.com/en-us/sql/relational-databases/performance-monitor/sql-server-database-replica)<br/>[`Win32_PerfRawData_MSSQLSERVER_SQLServerDatabases`](https://docs.microsoft.com/en-us/sql/relational-databases/performance-monitor/sql-server-databases-object?view=sql-server-2017)<br/>[`Win32_PerfRawData_MSSQLSERVER_SQLServerGeneralStatistics`](https://docs.microsoft.com/en-us/sql/relational-databases/performance-monitor/sql-server-general-statistics-object)<br/>[`Win32_PerfRawData_MSSQLSERVER_SQLServerLocks`](https://docs.microsoft.com/en-us/sql/relational-databases/performance-monitor/sql-server-locks-object)<br/>[`Win32_PerfRawData_MSSQLSERVER_SQLServerMemoryManager`](https://docs.microsoft.com/en-us/sql/relational-databases/performance-monitor/sql-server-memory-manager-object)<br/>[`Win32_PerfRawData_MSSQLSERVER_SQLServerSQLStatistics`](https://docs.microsoft.com/en-us/sql/relational-databases/performance-monitor/sql-server-sql-statistics-object)<br/>[`Win32_PerfRawData_MSSQLSERVER_SQLServerSQLErrors`](https://docs.microsoft.com/en-us/sql/relational-databases/performance-monitor/sql-server-sql-errors-object)<br/>[`Win32_PerfRawData_MSSQLSERVER_SQLServerTransactions`](https://docs.microsoft.com/en-us/sql/relational-databases/performance-monitor/sql-server-transactions-object)
Enabled by default? | No

## Flags

### `--collectors.mssql.classes-enabled`

Comma-separated list of MSSQL WMI classes to use. Supported values are `accessmethods`, `availreplica`, `bufman`, `databases`, `dbreplica`, `genstats`, `locks`, `memmgr`, `sqlstats` and `sqlerrors`.
Comma-separated list of MSSQL WMI classes to use. Supported values are `accessmethods`, `availreplica`, `bufman`, `databases`, `dbreplica`, `genstats`, `locks`, `memmgr`, `sqlstats`, `sqlerrors` and `transactions`.

### `--collectors.mssql.class-print`

Expand Down Expand Up @@ -231,6 +231,19 @@ Name | Description | Type | Labels
`wmi_mssql_sqlstats_sql_recompilations` | _Not yet documented_ | counter | `instance`
`wmi_mssql_sqlstats_unsafe_auto_parameterization_attempts` | _Not yet documented_ | counter | `instance`
`wmi_mssql_sql_errors_total` | _Not yet documented_ | counter | `instance`, `resource`
`wmi_mssql_transactions_tempdb_free_space_bytes` | _Not yet documented_ | gauge | `instance`
`wmi_mssql_transactions_longest_transaction_running_seconds` | _Not yet documented_ | gauge | `instance`
`wmi_mssql_transactions_nonsnapshot_version_active_total` | _Not yet documented_ | counter | `instance`
`wmi_mssql_transactions_snapshot_active_total` | _Not yet documented_ | counter | `instance`
`wmi_mssql_transactions_active_total` | _Not yet documented_ | counter | `instance`
`wmi_mssql_transactions_update_conflicts_total` | _Not yet documented_ | counter | `instance`
`wmi_mssql_transactions_update_snapshot_active_total` | _Not yet documented_ | counter | `instance`
`wmi_mssql_transactions_version_cleanup_rate_bytes` | _Not yet documented_ | gauge | `instance`
`wmi_mssql_transactions_version_generation_rate_bytes` | _Not yet documented_ | gauge | `instance`
`wmi_mssql_transactions_version_store_size_bytes` | _Not yet documented_ | gauge | `instance`
`wmi_mssql_transactions_version_store_units` | _Not yet documented_ | counter | `instance`
`wmi_mssql_transactions_version_store_creation_units` | _Not yet documented_ | counter | `instance`
`wmi_mssql_transactions_version_store_truncation_units` | _Not yet documented_ | counter | `instance`

### Example metric
_This collector does not yet have explained examples, we would appreciate your help adding them!_
Expand Down

0 comments on commit d01c669

Please sign in to comment.