Skip to content

Commit

Permalink
[receiver/sqlserver] Emit correct database name resource attribute (o…
Browse files Browse the repository at this point in the history
…pen-telemetry#35038)

**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.-->
As explained in the bug description, the receiver was incorrectly
setting the database name resource attribute based on the first row
returned from the query. The returned rows may have different database
names, which means metrics were being labeled as being from the wrong
database.

**Link to tracking Issue:** <Issue number if applicable>
Fixes
open-telemetry#35036

**Testing:** <Describe what testing was performed and which tests were
added.>
Updated tests for new expected resource metrics. View changed
`testdata/` files to see changed output.
  • Loading branch information
crobert-1 authored and jriguera committed Oct 4, 2024
1 parent 7a8f0d2 commit 9966d5b
Show file tree
Hide file tree
Showing 5 changed files with 996 additions and 382 deletions.
27 changes: 27 additions & 0 deletions .chloggen/fix_sqlserver_resource_attrs.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: bug_fix

# 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: Fix bug where metrics were being emitted with the wrong database name resource attribute

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

# (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: []
42 changes: 21 additions & 21 deletions receiver/sqlserverreceiver/scraper.go
Original file line number Diff line number Diff line change
Expand Up @@ -80,14 +80,13 @@ func (s *sqlServerScraperHelper) Start(context.Context, component.Host) error {
func (s *sqlServerScraperHelper) Scrape(ctx context.Context) (pmetric.Metrics, error) {
var err error

rb := s.mb.NewResourceBuilder()
switch s.sqlQuery {
case getSQLServerDatabaseIOQuery(s.instanceName):
err = s.recordDatabaseIOMetrics(ctx, rb)
err = s.recordDatabaseIOMetrics(ctx)
case getSQLServerPerformanceCounterQuery(s.instanceName):
err = s.recordDatabasePerfCounterMetrics(ctx, rb)
err = s.recordDatabasePerfCounterMetrics(ctx)
case getSQLServerPropertiesQuery(s.instanceName):
err = s.recordDatabaseStatusMetrics(ctx, rb)
err = s.recordDatabaseStatusMetrics(ctx)
default:
return pmetric.Metrics{}, fmt.Errorf("Attempted to get metrics from unsupported query: %s", s.sqlQuery)
}
Expand All @@ -96,7 +95,7 @@ func (s *sqlServerScraperHelper) Scrape(ctx context.Context) (pmetric.Metrics, e
return pmetric.Metrics{}, err
}

return s.mb.Emit(metadata.WithResource(rb.Emit())), nil
return s.mb.Emit(), nil
}

func (s *sqlServerScraperHelper) Shutdown(_ context.Context) error {
Expand All @@ -106,8 +105,7 @@ func (s *sqlServerScraperHelper) Shutdown(_ context.Context) error {
return nil
}

func (s *sqlServerScraperHelper) recordDatabaseIOMetrics(ctx context.Context, rb *metadata.ResourceBuilder) error {
// TODO: Move constants out to the package level when other queries are added.
func (s *sqlServerScraperHelper) recordDatabaseIOMetrics(ctx context.Context) error {
const computerNameKey = "computer_name"
const databaseNameKey = "database_name"
const physicalFilenameKey = "physical_filename"
Expand All @@ -133,11 +131,10 @@ func (s *sqlServerScraperHelper) recordDatabaseIOMetrics(ctx context.Context, rb
now := pcommon.NewTimestampFromTime(time.Now())
var val float64
for i, row := range rows {
if i == 0 {
rb.SetSqlserverComputerName(row[computerNameKey])
rb.SetSqlserverDatabaseName(row[databaseNameKey])
rb.SetSqlserverInstanceName(row[instanceNameKey])
}
rb := s.mb.NewResourceBuilder()
rb.SetSqlserverComputerName(row[computerNameKey])
rb.SetSqlserverDatabaseName(row[databaseNameKey])
rb.SetSqlserverInstanceName(row[instanceNameKey])

val, err = strconv.ParseFloat(row[readLatencyMsKey], 64)
if err != nil {
Expand All @@ -159,6 +156,8 @@ func (s *sqlServerScraperHelper) recordDatabaseIOMetrics(ctx context.Context, rb
errs = append(errs, s.mb.RecordSqlserverDatabaseOperationsDataPoint(now, row[writeCountKey], row[physicalFilenameKey], row[logicalFilenameKey], row[fileTypeKey], metadata.AttributeDirectionWrite))
errs = append(errs, s.mb.RecordSqlserverDatabaseIoDataPoint(now, row[readBytesKey], row[physicalFilenameKey], row[logicalFilenameKey], row[fileTypeKey], metadata.AttributeDirectionRead))
errs = append(errs, s.mb.RecordSqlserverDatabaseIoDataPoint(now, row[writeBytesKey], row[physicalFilenameKey], row[logicalFilenameKey], row[fileTypeKey], metadata.AttributeDirectionWrite))

s.mb.EmitForResource(metadata.WithResource(rb.Emit()))
}

if len(rows) == 0 {
Expand All @@ -168,7 +167,7 @@ func (s *sqlServerScraperHelper) recordDatabaseIOMetrics(ctx context.Context, rb
return errors.Join(errs...)
}

func (s *sqlServerScraperHelper) recordDatabasePerfCounterMetrics(ctx context.Context, rb *metadata.ResourceBuilder) error {
func (s *sqlServerScraperHelper) recordDatabasePerfCounterMetrics(ctx context.Context) error {
const counterKey = "counter"
const valueKey = "value"
// Constants are the columns for metrics from query
Expand All @@ -195,9 +194,8 @@ func (s *sqlServerScraperHelper) recordDatabasePerfCounterMetrics(ctx context.Co
var errs []error
now := pcommon.NewTimestampFromTime(time.Now())
for i, row := range rows {
if i == 0 {
rb.SetSqlserverInstanceName(row[instanceNameKey])
}
rb := s.mb.NewResourceBuilder()
rb.SetSqlserverInstanceName(row[instanceNameKey])

switch row[counterKey] {
case batchRequestRate:
Expand Down Expand Up @@ -255,12 +253,14 @@ func (s *sqlServerScraperHelper) recordDatabasePerfCounterMetrics(ctx context.Co
s.mb.RecordSqlserverUserConnectionCountDataPoint(now, val)
}
}

s.mb.EmitForResource(metadata.WithResource(rb.Emit()))
}

return errors.Join(errs...)
}

func (s *sqlServerScraperHelper) recordDatabaseStatusMetrics(ctx context.Context, rb *metadata.ResourceBuilder) error {
func (s *sqlServerScraperHelper) recordDatabaseStatusMetrics(ctx context.Context) error {
// Constants are the column names of the database status
const dbOnline = "db_online"
const dbRestoring = "db_restoring"
Expand All @@ -281,10 +281,9 @@ func (s *sqlServerScraperHelper) recordDatabaseStatusMetrics(ctx context.Context

var errs []error
now := pcommon.NewTimestampFromTime(time.Now())
for i, row := range rows {
if i == 0 {
rb.SetSqlserverInstanceName(row[instanceNameKey])
}
for _, row := range rows {
rb := s.mb.NewResourceBuilder()
rb.SetSqlserverInstanceName(row[instanceNameKey])

errs = append(errs, s.mb.RecordSqlserverDatabaseCountDataPoint(now, row[dbOnline], metadata.AttributeDatabaseStatusOnline))
errs = append(errs, s.mb.RecordSqlserverDatabaseCountDataPoint(now, row[dbRestoring], metadata.AttributeDatabaseStatusRestoring))
Expand All @@ -293,6 +292,7 @@ func (s *sqlServerScraperHelper) recordDatabaseStatusMetrics(ctx context.Context
errs = append(errs, s.mb.RecordSqlserverDatabaseCountDataPoint(now, row[dbSuspect], metadata.AttributeDatabaseStatusSuspect))
errs = append(errs, s.mb.RecordSqlserverDatabaseCountDataPoint(now, row[dbOffline], metadata.AttributeDatabaseStatusOffline))

s.mb.EmitForResource(metadata.WithResource(rb.Emit()))
}

return errors.Join(errs...)
Expand Down
3 changes: 2 additions & 1 deletion receiver/sqlserverreceiver/scraper_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -110,7 +110,8 @@ func TestSuccessfulScrape(t *testing.T) {
assert.NoError(t, pmetrictest.CompareMetrics(actualMetrics, expectedMetrics,
pmetrictest.IgnoreMetricDataPointsOrder(),
pmetrictest.IgnoreStartTimestamp(),
pmetrictest.IgnoreTimestamp()))
pmetrictest.IgnoreTimestamp(),
pmetrictest.IgnoreResourceMetricsOrder()))
}
}

Expand Down
Loading

0 comments on commit 9966d5b

Please sign in to comment.