Skip to content
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

[receiver/sqlserver] Add SQL server host resource attributes #37004

Open
wants to merge 4 commits 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
30 changes: 30 additions & 0 deletions .chloggen/sqlserver-add-address-resource-attr.yaml
Original file line number Diff line number Diff line change
@@ -0,0 +1,30 @@
# 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/sqlserverreceiver

# A brief description of the change. Surround your text with quotes ("") if it needs to start with a backtick (`).
note: Add `server.address` and `server.port` resource attributes to SQL server receiver.

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

# (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: |
The new resource attributes are added to the SQL server receiver to distinguish metrics coming from different SQL server instances.
- `server.address`: The address of the SQL server host, enabled by default.
- `server.port`: The port of the SQL server host, 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: [user]
6 changes: 6 additions & 0 deletions receiver/sqlserverreceiver/config_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -112,6 +112,12 @@ func TestLoadConfig(t *testing.T) {
SqlserverComputerName: metadata.ResourceAttributeConfig{
Enabled: true,
},
ServerAddress: metadata.ResourceAttributeConfig{
Enabled: true,
},
ServerPort: metadata.ResourceAttributeConfig{
Enabled: true,
},
},
}
expected.ComputerName = "CustomServer"
Expand Down
2 changes: 2 additions & 0 deletions receiver/sqlserverreceiver/documentation.md
Original file line number Diff line number Diff line change
Expand Up @@ -323,6 +323,8 @@ This metric is only available when the receiver is configured to directly connec

| Name | Description | Values | Enabled |
| ---- | ----------- | ------ | ------- |
| server.address | The address of the SQL server host. | Any Str | true |
| server.port | The port of the SQL server host. | Any Int | false |
| sqlserver.computer.name | The name of the SQL Server instance being monitored. | Any Str | false |
| sqlserver.database.name | The name of the SQL Server database. | Any Str | true |
| sqlserver.instance.name | The name of the SQL Server instance being monitored. | Any Str | false |
6 changes: 2 additions & 4 deletions receiver/sqlserverreceiver/factory.go
Original file line number Diff line number Diff line change
Expand Up @@ -98,13 +98,11 @@ func setupSQLServerScrapers(params receiver.Settings, cfg *Config) []*sqlServerS
id := component.NewIDWithName(metadata.Type, fmt.Sprintf("query-%d: %s", i, query))

sqlServerScraper := newSQLServerScraper(id, query,
cfg.InstanceName,
cfg.ControllerConfig,
params.Logger,
sqlquery.TelemetryConfig{},
dbProviderFunc,
sqlquery.NewDbClient,
metadata.NewMetricsBuilder(cfg.MetricsBuilderConfig, params))
params,
cfg)

scrapers = append(scrapers, sqlServerScraper)
}
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.

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 @@ -56,6 +56,10 @@ all_set:
sqlserver.user.connection.count:
enabled: true
resource_attributes:
server.address:
enabled: true
server.port:
enabled: true
sqlserver.computer.name:
enabled: true
sqlserver.database.name:
Expand Down Expand Up @@ -119,6 +123,10 @@ none_set:
sqlserver.user.connection.count:
enabled: false
resource_attributes:
server.address:
enabled: false
server.port:
enabled: false
sqlserver.computer.name:
enabled: false
sqlserver.database.name:
Expand All @@ -127,6 +135,14 @@ none_set:
enabled: false
filter_set_include:
resource_attributes:
server.address:
enabled: true
metrics_include:
- regexp: ".*"
server.port:
enabled: true
metrics_include:
- regexp: ".*"
sqlserver.computer.name:
enabled: true
metrics_include:
Expand All @@ -141,6 +157,14 @@ filter_set_include:
- regexp: ".*"
filter_set_exclude:
resource_attributes:
server.address:
enabled: true
metrics_exclude:
- strict: "server.address-val"
server.port:
enabled: true
metrics_exclude:
- regexp: ".*"
sqlserver.computer.name:
enabled: true
metrics_exclude:
Expand Down
8 changes: 8 additions & 0 deletions receiver/sqlserverreceiver/metadata.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -22,6 +22,14 @@ resource_attributes:
description: The name of the SQL Server instance being monitored.
enabled: false
type: string
server.address:
description: The address of the SQL server host.
enabled: true
type: string
server.port:
description: The port of the SQL server host.
enabled: false
type: int

attributes:
page.operations:
Expand Down
30 changes: 16 additions & 14 deletions receiver/sqlserverreceiver/scraper.go
Original file line number Diff line number Diff line change
Expand Up @@ -14,7 +14,7 @@ import (
"go.opentelemetry.io/collector/component"
"go.opentelemetry.io/collector/pdata/pcommon"
"go.opentelemetry.io/collector/pdata/pmetric"
"go.opentelemetry.io/collector/receiver/scraperhelper"
"go.opentelemetry.io/collector/receiver"
"go.opentelemetry.io/collector/scraper"
"go.uber.org/zap"

Expand All @@ -29,9 +29,8 @@ const (

type sqlServerScraperHelper struct {
id component.ID
config *Config
sqlQuery string
instanceName string
scrapeCfg scraperhelper.ControllerConfig
clientProviderFunc sqlquery.ClientProviderFunc
dbProviderFunc sqlquery.DbProviderFunc
logger *zap.Logger
Expand All @@ -45,24 +44,21 @@ var _ scraper.Metrics = (*sqlServerScraperHelper)(nil)

func newSQLServerScraper(id component.ID,
query string,
instanceName string,
scrapeCfg scraperhelper.ControllerConfig,
logger *zap.Logger,
telemetry sqlquery.TelemetryConfig,
dbProviderFunc sqlquery.DbProviderFunc,
clientProviderFunc sqlquery.ClientProviderFunc,
mb *metadata.MetricsBuilder,
params receiver.Settings,
cfg *Config,
) *sqlServerScraperHelper {
return &sqlServerScraperHelper{
id: id,
config: cfg,
sqlQuery: query,
instanceName: instanceName,
scrapeCfg: scrapeCfg,
logger: logger,
logger: params.Logger,
telemetry: telemetry,
dbProviderFunc: dbProviderFunc,
clientProviderFunc: clientProviderFunc,
mb: mb,
mb: metadata.NewMetricsBuilder(cfg.MetricsBuilderConfig, params),
}
}

Expand All @@ -85,11 +81,11 @@ func (s *sqlServerScraperHelper) ScrapeMetrics(ctx context.Context) (pmetric.Met
var err error

switch s.sqlQuery {
case getSQLServerDatabaseIOQuery(s.instanceName):
case getSQLServerDatabaseIOQuery(s.config.InstanceName):
err = s.recordDatabaseIOMetrics(ctx)
case getSQLServerPerformanceCounterQuery(s.instanceName):
case getSQLServerPerformanceCounterQuery(s.config.InstanceName):
err = s.recordDatabasePerfCounterMetrics(ctx)
case getSQLServerPropertiesQuery(s.instanceName):
case getSQLServerPropertiesQuery(s.config.InstanceName):
err = s.recordDatabaseStatusMetrics(ctx)
default:
return pmetric.Metrics{}, fmt.Errorf("Attempted to get metrics from unsupported query: %s", s.sqlQuery)
Expand Down Expand Up @@ -138,6 +134,8 @@ func (s *sqlServerScraperHelper) recordDatabaseIOMetrics(ctx context.Context) er
rb.SetSqlserverComputerName(row[computerNameKey])
rb.SetSqlserverDatabaseName(row[databaseNameKey])
rb.SetSqlserverInstanceName(row[instanceNameKey])
rb.SetServerAddress(s.config.Server)
rb.SetServerPort(int64(s.config.Port))

val, err = strconv.ParseFloat(row[readLatencyMsKey], 64)
if err != nil {
Expand Down Expand Up @@ -199,6 +197,8 @@ func (s *sqlServerScraperHelper) recordDatabasePerfCounterMetrics(ctx context.Co
rb := s.mb.NewResourceBuilder()
rb.SetSqlserverComputerName(row[computerNameKey])
rb.SetSqlserverInstanceName(row[instanceNameKey])
rb.SetServerAddress(s.config.Server)
rb.SetServerPort(int64(s.config.Port))

switch row[counterKey] {
case batchRequestRate:
Expand Down Expand Up @@ -287,6 +287,8 @@ func (s *sqlServerScraperHelper) recordDatabaseStatusMetrics(ctx context.Context
rb := s.mb.NewResourceBuilder()
rb.SetSqlserverComputerName(row[computerNameKey])
rb.SetSqlserverInstanceName(row[instanceNameKey])
rb.SetServerAddress(s.config.Server)
rb.SetServerPort(int64(s.config.Port))

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