Skip to content

[receiver/sqlserver] Add new metric: lock wait count #39930

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

Open
wants to merge 1 commit into
base: main
Choose a base branch
from
Open
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
27 changes: 27 additions & 0 deletions .chloggen/sqlserver_add_lock_wait_count.yaml
Original file line number Diff line number Diff line change
@@ -0,0 +1,27 @@
# Use this changelog template to create an entry for release notes.

# One of 'breaking', 'deprecation', 'new_component', 'enhancement', 'bug_fix'
change_type: enhancement

# The name of the component, or a single word describing the area of concern, (e.g. filelogreceiver)
component: receiver/sqlserver

# A brief description of the change. Surround your text with quotes ("") if it needs to start with a backtick (`).
note: Add new metric `sqlserver.lock.wait.count`

# Mandatory: One or more tracking issues related to the change. You can use the PR number here if no issue exists.
issues: [39892]

# (Optional) One or more lines of additional information to render under the primary note.
# These lines will be padded with 2 spaces and then inserted directly into the document.
# Use pipe (|) for multiline entries.
subtext: This metric is disabled by default.

# If your change doesn't affect end users or the exported elements of any package,
# you should instead start your pull request title with [chore] or use the "Skip Changelog" label.
# Optional: The change log or logs in which this entry should be included.
# e.g. '[user]' or '[user, api]'
# Include 'user' if the change is relevant to end users.
# Include 'api' if there is a change to a library API.
# Default: '[user]'
change_logs: []
10 changes: 10 additions & 0 deletions receiver/sqlserverreceiver/documentation.md
Original file line number Diff line number Diff line change
Expand Up @@ -359,6 +359,16 @@ Total number of lock timeouts.
| ---- | ----------- | ---------- |
| “{timeouts}/s” | Gauge | Double |

### sqlserver.lock.wait.count

Cumulative count of lock waits that occurred.

This metric is only available when the receiver is configured to directly connect to SQL Server.

| Unit | Metric Type | Value Type | Aggregation Temporality | Monotonic |
| ---- | ----------- | ---------- | ----------------------- | --------- |
| {waits} | Sum | Int | Cumulative | true |

### sqlserver.login.rate

Total number of logins.
Expand Down
1 change: 1 addition & 0 deletions receiver/sqlserverreceiver/factory.go
Original file line number Diff line number Diff line change
Expand Up @@ -267,6 +267,7 @@ func isPerfCounterQueryEnabled(metrics *metadata.MetricsConfig) bool {
metrics.SqlserverDeadlockRate.Enabled ||
metrics.SqlserverIndexSearchRate.Enabled ||
metrics.SqlserverLockTimeoutRate.Enabled ||
metrics.SqlserverLockWaitCount.Enabled ||
metrics.SqlserverLockWaitRate.Enabled ||
metrics.SqlserverLoginRate.Enabled ||
metrics.SqlserverLogoutRate.Enabled ||
Expand Down
2 changes: 1 addition & 1 deletion receiver/sqlserverreceiver/factory_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -264,7 +264,7 @@ func TestSetupQueries(t *testing.T) {

metricsMetadata, ok := metadata["metrics"].(map[string]any)
require.True(t, ok)
require.Len(t, metricsMetadata, 45,
require.Len(t, metricsMetadata, 46,
"Every time metrics are added or removed, the function `setupQueries` must "+
"be modified to properly account for the change. Please update `setupQueries` and then, "+
"and only then, update the expected metric count here.")
Expand Down

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

Original file line number Diff line number Diff line change
Expand Up @@ -31,6 +31,8 @@ all_set:
enabled: true
sqlserver.lock.timeout.rate:
enabled: true
sqlserver.lock.wait.count:
enabled: true
sqlserver.lock.wait.rate:
enabled: true
sqlserver.lock.wait_time.avg:
Expand Down Expand Up @@ -134,6 +136,8 @@ none_set:
enabled: false
sqlserver.lock.timeout.rate:
enabled: false
sqlserver.lock.wait.count:
enabled: false
sqlserver.lock.wait.rate:
enabled: false
sqlserver.lock.wait_time.avg:
Expand Down
9 changes: 9 additions & 0 deletions receiver/sqlserverreceiver/metadata.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -86,6 +86,15 @@ metrics:
gauge:
value_type: double
extended_documentation: This metric is only available when running on Windows.
sqlserver.lock.wait.count:
enabled: false
description: Cumulative count of lock waits that occurred.
unit: "{waits}"
Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I'd appreciate feedback on what the unit should be here, if anyone has any thoughts.

sum:
monotonic: true
aggregation_temporality: cumulative
value_type: int
extended_documentation: This metric is only available when the receiver is configured to directly connect to SQL Server.
sqlserver.lock.wait.rate:
enabled: true
description: Number of lock requests resulting in a wait.
Expand Down
32 changes: 32 additions & 0 deletions receiver/sqlserverreceiver/queries.go
Original file line number Diff line number Diff line change
Expand Up @@ -76,6 +76,8 @@ END
DECLARE
@SqlStatement AS nvarchar(max)
,@MajorMinorVersion AS int = CAST(PARSENAME(CAST(SERVERPROPERTY('ProductVersion') AS nvarchar),4) AS int)*100 + CAST(PARSENAME(CAST(SERVERPROPERTY('ProductVersion') AS nvarchar),3) AS int)
,@Columns AS nvarchar(MAX) = ''
,@PivotColumns AS nvarchar(MAX) = ''

DECLARE @PCounters TABLE
(
Expand Down Expand Up @@ -210,6 +212,36 @@ SELECT DISTINCT

INSERT INTO @PCounters SELECT * FROM PerfCounters;

SET @SqlStatement = N'
SELECT
''SQLServer:Workload Group Stats'' AS [object]
,[counter]
,[instance]
,CAST(vs.[value] AS BIGINT) AS [value]
,1
FROM
(
SELECT
rgwg.name AS instance
,rgwg.total_request_count AS [Request Count]
Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Including extra selects here in case we want to add these metrics in the future.

,rgwg.total_queued_request_count AS [Queued Request Count]
,rgwg.total_cpu_limit_violation_count AS [CPU Limit Violation Count]
,rgwg.total_cpu_usage_ms AS [CPU Usage (time)]
,rgwg.total_lock_wait_count AS [Lock Wait Count]
,rgwg.total_lock_wait_time_ms AS [Lock Wait Time]
,rgwg.total_reduced_memgrant_count AS [Reduced Memory Grant Count]
' + @Columns + N'
FROM sys.[dm_resource_governor_workload_groups] AS rgwg
INNER JOIN sys.[dm_resource_governor_resource_pools] AS rgrp
ON rgwg.[pool_id] = rgrp.[pool_id]
) AS rg
UNPIVOT (
value FOR counter IN ( [Request Count], [Queued Request Count], [CPU Limit Violation Count], [CPU Usage (time)], [Lock Wait Count], [Lock Wait Time], [Reduced Memory Grant Count] ' + @PivotColumns + N')
) AS vs'

INSERT INTO @PCounters
EXEC( @SqlStatement )

SELECT
'sqlserver_performance' AS [measurement]
,REPLACE(@@SERVERNAME,'\',':') AS [sql_instance]
Expand Down
9 changes: 9 additions & 0 deletions receiver/sqlserverreceiver/scraper.go
Original file line number Diff line number Diff line change
Expand Up @@ -218,6 +218,7 @@ func (s *sqlServerScraperHelper) recordDatabasePerfCounterMetrics(ctx context.Co
const fullScansPerSec = "Full Scans/sec"
const indexSearchesPerSec = "Index Searches/sec"
const lockTimeoutsPerSec = "Lock Timeouts/sec"
const lockWaitCount = "Lock Wait Count"
const lockWaits = "Lock Waits/sec"
const loginsPerSec = "Logins/sec"
const logoutPerSec = "Logouts/sec"
Expand Down Expand Up @@ -352,6 +353,14 @@ func (s *sqlServerScraperHelper) recordDatabasePerfCounterMetrics(ctx context.Co
} else {
s.mb.RecordSqlserverLockTimeoutRateDataPoint(now, val.(float64))
}
case lockWaitCount:
val, err := retrieveInt(row, valueKey)
Copy link
Member Author

@crobert-1 crobert-1 May 7, 2025

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

retrieveInt is called here instead of strconv.ParseInt to conform to new functionality introduced in #39905 (which hasn't been merged yet).

if err != nil {
err = fmt.Errorf("failed to parse valueKey for row %d: %w in %s", i, err, lockWaitCount)
errs = append(errs, err)
} else {
s.mb.RecordSqlserverLockWaitCountDataPoint(now, val.(int64))
}
case lockWaits:
val, err := retrieveFloat(row, valueKey)
if err != nil {
Expand Down
Loading
Loading