Skip to content

Commit

Permalink
[receiver/sqlserver] Add computer name resource attribute to relevant…
Browse files Browse the repository at this point in the history
… metrics (#35040)

**Description:** <Describe what has changed.>
<!--Ex. Fixing a bug - Describe the bug and how this fixes the issue.
Ex. Adding a feature - Explain what this achieves.-->
The computer name resource attribute is relevant for all metrics coming
from the SQL server receiver when directly connected to the DB. This
change records the resource attribute with all remaining metrics that it
wasn't recorded with yet.

**Link to tracking Issue:** <Issue number if applicable>
This was discovered at the same time as
#35036,
but I split out the changes to hopefully make them easier to review.

**Testing:** <Describe what testing was performed and which tests were
added.>
Tests have been updated and are passing.
  • Loading branch information
crobert-1 authored Sep 30, 2024
1 parent 4c6d6a5 commit 1e15770
Show file tree
Hide file tree
Showing 9 changed files with 330 additions and 3 deletions.
27 changes: 27 additions & 0 deletions .chloggen/sqlserver_computer_rattr.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: sqlserverreceiver

# A brief description of the change. Surround your text with quotes ("") if it needs to start with a backtick (`).
note: Add computer name resource attribute to relevant metrics

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

# (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:

# 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: []
2 changes: 2 additions & 0 deletions receiver/sqlserverreceiver/queries.go
Original file line number Diff line number Diff line change
Expand Up @@ -211,6 +211,7 @@ INSERT INTO @PCounters SELECT * FROM PerfCounters;
SELECT
'sqlserver_performance' AS [measurement]
,REPLACE(@@SERVERNAME,'\',':') AS [sql_instance]
,HOST_NAME() AS [computer_name]
,pc.[object_name] AS [object]
,pc.[counter_name] AS [counter]
,CASE pc.[instance_name] WHEN '_Total' THEN 'Total' ELSE ISNULL(pc.[instance_name],'') END AS [instance]
Expand Down Expand Up @@ -288,6 +289,7 @@ EXEC [xp_instance_regread]
SELECT
''sqlserver_server_properties'' AS [measurement]
,REPLACE(@@SERVERNAME,''\'','':'') AS [sql_instance]
,HOST_NAME() AS [computer_name]
,@@SERVICENAME AS [service_name]
,si.[cpu_count]
,(SELECT [total_physical_memory_kb] FROM sys.[dm_os_sys_memory]) AS [server_memory]
Expand Down
8 changes: 6 additions & 2 deletions receiver/sqlserverreceiver/scraper.go
Original file line number Diff line number Diff line change
Expand Up @@ -21,7 +21,10 @@ import (
"github.com/open-telemetry/opentelemetry-collector-contrib/receiver/sqlserverreceiver/internal/metadata"
)

const instanceNameKey = "sql_instance"
const (
computerNameKey = "computer_name"
instanceNameKey = "sql_instance"
)

type sqlServerScraperHelper struct {
id component.ID
Expand Down Expand Up @@ -106,7 +109,6 @@ func (s *sqlServerScraperHelper) Shutdown(_ context.Context) error {
}

func (s *sqlServerScraperHelper) recordDatabaseIOMetrics(ctx context.Context) error {
const computerNameKey = "computer_name"
const databaseNameKey = "database_name"
const physicalFilenameKey = "physical_filename"
const logicalFilenameKey = "logical_filename"
Expand Down Expand Up @@ -195,6 +197,7 @@ func (s *sqlServerScraperHelper) recordDatabasePerfCounterMetrics(ctx context.Co
now := pcommon.NewTimestampFromTime(time.Now())
for i, row := range rows {
rb := s.mb.NewResourceBuilder()
rb.SetSqlserverComputerName(row[computerNameKey])
rb.SetSqlserverInstanceName(row[instanceNameKey])

switch row[counterKey] {
Expand Down Expand Up @@ -283,6 +286,7 @@ func (s *sqlServerScraperHelper) recordDatabaseStatusMetrics(ctx context.Context
now := pcommon.NewTimestampFromTime(time.Now())
for _, row := range rows {
rb := s.mb.NewResourceBuilder()
rb.SetSqlserverComputerName(row[computerNameKey])
rb.SetSqlserverInstanceName(row[instanceNameKey])

errs = append(errs, s.mb.RecordSqlserverDatabaseCountDataPoint(now, row[dbOnline], metadata.AttributeDatabaseStatusOnline))
Expand Down
Loading

0 comments on commit 1e15770

Please sign in to comment.