Skip to content

Commit

Permalink
[receiver/sqlqueryreceiver] Add instrumentation scope to sqlqueryrece…
Browse files Browse the repository at this point in the history
…iver metrics & logs (#36812)

#### Description
Add instrumentation scope to the metrics and logs emitted by the
sqlqueryreceiver.

#### Link to tracking issue
Fixes
#31028
  • Loading branch information
dehaansa authored Dec 18, 2024
1 parent 1aa07f4 commit 017b3d4
Show file tree
Hide file tree
Showing 9 changed files with 102 additions and 46 deletions.
27 changes: 27 additions & 0 deletions .chloggen/sqlqueryreceiver-scope.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: sqlqueryreceiver

# A brief description of the change. Surround your text with quotes ("") if it needs to start with a backtick (`).
note: "Add instrumentation scope to SQL query receiver metrics and logs"

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

# (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: [user]
39 changes: 21 additions & 18 deletions internal/sqlquery/scraper.go
Original file line number Diff line number Diff line change
Expand Up @@ -26,29 +26,31 @@ type DbProviderFunc func() (*sql.DB, error)
type ClientProviderFunc func(Db, string, *zap.Logger, TelemetryConfig) DbClient

type Scraper struct {
id component.ID
Query Query
ScrapeCfg scraperhelper.ControllerConfig
StartTime pcommon.Timestamp
ClientProviderFunc ClientProviderFunc
DbProviderFunc DbProviderFunc
Logger *zap.Logger
Telemetry TelemetryConfig
Client DbClient
Db *sql.DB
id component.ID
Query Query
ScrapeCfg scraperhelper.ControllerConfig
StartTime pcommon.Timestamp
ClientProviderFunc ClientProviderFunc
DbProviderFunc DbProviderFunc
Logger *zap.Logger
Telemetry TelemetryConfig
Client DbClient
Db *sql.DB
InstrumentationScope pcommon.InstrumentationScope
}

var _ scraper.Metrics = (*Scraper)(nil)

func NewScraper(id component.ID, query Query, scrapeCfg scraperhelper.ControllerConfig, logger *zap.Logger, telemetry TelemetryConfig, dbProviderFunc DbProviderFunc, clientProviderFunc ClientProviderFunc) *Scraper {
func NewScraper(id component.ID, query Query, scrapeCfg scraperhelper.ControllerConfig, logger *zap.Logger, telemetry TelemetryConfig, dbProviderFunc DbProviderFunc, clientProviderFunc ClientProviderFunc, instrumentationScope pcommon.InstrumentationScope) *Scraper {
return &Scraper{
id: id,
Query: query,
ScrapeCfg: scrapeCfg,
Logger: logger,
Telemetry: telemetry,
DbProviderFunc: dbProviderFunc,
ClientProviderFunc: clientProviderFunc,
id: id,
Query: query,
ScrapeCfg: scrapeCfg,
Logger: logger,
Telemetry: telemetry,
DbProviderFunc: dbProviderFunc,
ClientProviderFunc: clientProviderFunc,
InstrumentationScope: instrumentationScope,
}
}

Expand Down Expand Up @@ -83,6 +85,7 @@ func (s *Scraper) ScrapeMetrics(ctx context.Context) (pmetric.Metrics, error) {
rm := rms.AppendEmpty()
sms := rm.ScopeMetrics()
sm := sms.AppendEmpty()
s.InstrumentationScope.CopyTo(sm.Scope())
ms := sm.Metrics()
var errs []error
for _, metricCfg := range s.Query.Metrics {
Expand Down
56 changes: 37 additions & 19 deletions internal/sqlquery/scraper_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -20,6 +20,7 @@ import (

func TestScraper_ErrorOnStart(t *testing.T) {
scrpr := Scraper{
InstrumentationScope: pcommon.NewInstrumentationScope(),
DbProviderFunc: func() (*sql.DB, error) {
return nil, errors.New("oops")
},
Expand All @@ -33,7 +34,8 @@ func TestScraper_ClientErrorOnScrape(t *testing.T) {
Err: errors.New("oops"),
}
scrpr := Scraper{
Client: client,
InstrumentationScope: pcommon.NewInstrumentationScope(),
Client: client,
}
_, err := scrpr.ScrapeMetrics(context.Background())
require.Error(t, err)
Expand All @@ -46,7 +48,8 @@ func TestScraper_RowToMetricErrorOnScrape_Float(t *testing.T) {
},
}
scrpr := Scraper{
Client: client,
InstrumentationScope: pcommon.NewInstrumentationScope(),
Client: client,
Query: Query{
Metrics: []MetricCfg{{
MetricName: "my.float",
Expand All @@ -68,7 +71,8 @@ func TestScraper_RowToMetricErrorOnScrape_Int(t *testing.T) {
},
}
scrpr := Scraper{
Client: client,
InstrumentationScope: pcommon.NewInstrumentationScope(),
Client: client,
Query: Query{
Metrics: []MetricCfg{{
MetricName: "my.int",
Expand All @@ -91,7 +95,8 @@ func TestScraper_RowToMetricMultiErrorsOnScrape(t *testing.T) {
}},
}
scrpr := Scraper{
Client: client,
InstrumentationScope: pcommon.NewInstrumentationScope(),
Client: client,
Query: Query{
Metrics: []MetricCfg{{
MetricName: "my.col",
Expand All @@ -108,6 +113,7 @@ func TestScraper_RowToMetricMultiErrorsOnScrape(t *testing.T) {

func TestScraper_SingleRow_MultiMetrics(t *testing.T) {
scrpr := Scraper{
InstrumentationScope: pcommon.NewInstrumentationScope(),
Client: &FakeDBClient{
StringMaps: [][]StringMap{{{
"count": "42",
Expand Down Expand Up @@ -191,7 +197,8 @@ func TestScraper_MultiRow(t *testing.T) {
}},
}
scrpr := Scraper{
Client: client,
InstrumentationScope: pcommon.NewInstrumentationScope(),
Client: client,
Query: Query{
Metrics: []MetricCfg{
{
Expand Down Expand Up @@ -231,7 +238,8 @@ func TestScraper_MultiResults_CumulativeSum(t *testing.T) {
},
}
scrpr := Scraper{
Client: client,
InstrumentationScope: pcommon.NewInstrumentationScope(),
Client: client,
Query: Query{
Metrics: []MetricCfg{{
MetricName: "transaction.count",
Expand All @@ -254,7 +262,8 @@ func TestScraper_MultiResults_DeltaSum(t *testing.T) {
},
}
scrpr := Scraper{
Client: client,
InstrumentationScope: pcommon.NewInstrumentationScope(),
Client: client,
Query: Query{
Metrics: []MetricCfg{{
MetricName: "transaction.count",
Expand Down Expand Up @@ -290,7 +299,8 @@ func TestScraper_Float(t *testing.T) {
},
}
scrpr := Scraper{
Client: client,
InstrumentationScope: pcommon.NewInstrumentationScope(),
Client: client,
Query: Query{
Metrics: []MetricCfg{{
MetricName: "my.float",
Expand All @@ -314,7 +324,8 @@ func TestScraper_DescriptionAndUnit(t *testing.T) {
},
}
scrpr := Scraper{
Client: client,
InstrumentationScope: pcommon.NewInstrumentationScope(),
Client: client,
Query: Query{
Metrics: []MetricCfg{{
MetricName: "my.name",
Expand All @@ -335,8 +346,9 @@ func TestScraper_FakeDB_Warnings(t *testing.T) {
db := fakeDB{rowVals: [][]any{{42, nil}}}
logger := zap.NewNop()
scrpr := Scraper{
Client: NewDbClient(db, "", logger, TelemetryConfig{}),
Logger: logger,
InstrumentationScope: pcommon.NewInstrumentationScope(),
Client: NewDbClient(db, "", logger, TelemetryConfig{}),
Logger: logger,
Query: Query{
Metrics: []MetricCfg{{
MetricName: "my.name",
Expand All @@ -354,8 +366,9 @@ func TestScraper_FakeDB_MultiRows_Warnings(t *testing.T) {
db := fakeDB{rowVals: [][]any{{42, nil}, {43, nil}}}
logger := zap.NewNop()
scrpr := Scraper{
Client: NewDbClient(db, "", logger, TelemetryConfig{}),
Logger: logger,
InstrumentationScope: pcommon.NewInstrumentationScope(),
Client: NewDbClient(db, "", logger, TelemetryConfig{}),
Logger: logger,
Query: Query{
Metrics: []MetricCfg{{
MetricName: "my.col.0",
Expand All @@ -375,8 +388,9 @@ func TestScraper_FakeDB_MultiRows_Error(t *testing.T) {
db := fakeDB{rowVals: [][]any{{42, nil}, {43, nil}}}
logger := zap.NewNop()
scrpr := Scraper{
Client: NewDbClient(db, "", logger, TelemetryConfig{}),
Logger: logger,
InstrumentationScope: pcommon.NewInstrumentationScope(),
Client: NewDbClient(db, "", logger, TelemetryConfig{}),
Logger: logger,
Query: Query{
Metrics: []MetricCfg{
{
Expand Down Expand Up @@ -412,7 +426,8 @@ func TestScraper_StartAndTSColumn(t *testing.T) {
}},
}
scrpr := Scraper{
Client: client,
InstrumentationScope: pcommon.NewInstrumentationScope(),
Client: client,
Query: Query{
Metrics: []MetricCfg{{
MetricName: "my.name",
Expand Down Expand Up @@ -441,7 +456,8 @@ func TestScraper_StartAndTS_ErrorOnColumnNotFound(t *testing.T) {
}},
}
scrpr := Scraper{
Client: client,
InstrumentationScope: pcommon.NewInstrumentationScope(),
Client: client,
Query: Query{
Metrics: []MetricCfg{{
MetricName: "my.name",
Expand All @@ -466,7 +482,8 @@ func TestScraper_CollectRowToMetricsErrors(t *testing.T) {
}},
}
scrpr := Scraper{
Client: client,
InstrumentationScope: pcommon.NewInstrumentationScope(),
Client: client,
Query: Query{
Metrics: []MetricCfg{{
MetricName: "my.name",
Expand Down Expand Up @@ -496,7 +513,8 @@ func TestScraper_StartAndTS_ErrorOnParse(t *testing.T) {
}},
}
scrpr := Scraper{
Client: client,
InstrumentationScope: pcommon.NewInstrumentationScope(),
Client: client,
Query: Query{
Metrics: []MetricCfg{{
MetricName: "my.name",
Expand Down
2 changes: 0 additions & 2 deletions receiver/sqlqueryreceiver/integration_test.go
Original file line number Diff line number Diff line change
@@ -1,8 +1,6 @@
// Copyright The OpenTelemetry Authors
// SPDX-License-Identifier: Apache-2.0

//go:build integration

package sqlqueryreceiver

import (
Expand Down
4 changes: 3 additions & 1 deletion receiver/sqlqueryreceiver/logs_receiver.go
Original file line number Diff line number Diff line change
Expand Up @@ -285,7 +285,9 @@ func (queryReceiver *logsQueryReceiver) collect(ctx context.Context) (plog.Logs,
}

var errs []error
scopeLogs := logs.ResourceLogs().AppendEmpty().ScopeLogs().AppendEmpty().LogRecords()
scope := logs.ResourceLogs().AppendEmpty().ScopeLogs().AppendEmpty()
scope.Scope().SetName(metadata.ScopeName)
scopeLogs := scope.LogRecords()
for logsConfigIndex, logsConfig := range queryReceiver.query.Logs {
for _, row := range rows {
logRecord := scopeLogs.AppendEmpty()
Expand Down
5 changes: 4 additions & 1 deletion receiver/sqlqueryreceiver/receiver.go
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,7 @@ import (

"go.opentelemetry.io/collector/component"
"go.opentelemetry.io/collector/consumer"
"go.opentelemetry.io/collector/pdata/pcommon"
"go.opentelemetry.io/collector/receiver"
"go.opentelemetry.io/collector/receiver/scraperhelper"

Expand Down Expand Up @@ -46,7 +47,9 @@ func createMetricsReceiverFunc(sqlOpenerFunc sqlquery.SQLOpenerFunc, clientProvi
dbProviderFunc := func() (*sql.DB, error) {
return sqlOpenerFunc(sqlCfg.Driver, sqlCfg.DataSource)
}
mp := sqlquery.NewScraper(id, query, sqlCfg.ControllerConfig, settings.TelemetrySettings.Logger, sqlCfg.Config.Telemetry, dbProviderFunc, clientProviderFunc)
scope := pcommon.NewInstrumentationScope()
scope.SetName(metadata.ScopeName)
mp := sqlquery.NewScraper(id, query, sqlCfg.ControllerConfig, settings.TelemetrySettings.Logger, sqlCfg.Config.Telemetry, dbProviderFunc, clientProviderFunc, scope)

opt := scraperhelper.AddScraper(metadata.Type, mp)
opts = append(opts, opt)
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -38,7 +38,8 @@ resourceMetrics:
stringValue: Action
timeUnixNano: "1684614725494431000"
name: genre.imdb
scope: {}
scope:
name: github.com/open-telemetry/opentelemetry-collector-contrib/receiver/sqlqueryreceiver
- resource: {}
scopeMetrics:
- metrics:
Expand Down Expand Up @@ -72,4 +73,5 @@ resourceMetrics:
- asDouble: 3.4
timeUnixNano: "1684614725499638000"
name: f
scope: {}
scope:
name: github.com/open-telemetry/opentelemetry-collector-contrib/receiver/sqlqueryreceiver
Original file line number Diff line number Diff line change
Expand Up @@ -38,4 +38,5 @@ resourceMetrics:
stringValue: Action
timeUnixNano: "1684632870521947236"
name: genre.imdb
scope: {}
scope:
name: github.com/open-telemetry/opentelemetry-collector-contrib/receiver/sqlqueryreceiver
Original file line number Diff line number Diff line change
Expand Up @@ -38,7 +38,8 @@ resourceMetrics:
stringValue: SciFi
timeUnixNano: "1684613308016320000"
name: genre.imdb
scope: {}
scope:
name: github.com/open-telemetry/opentelemetry-collector-contrib/receiver/sqlqueryreceiver
- resource: {}
scopeMetrics:
- metrics:
Expand Down Expand Up @@ -81,4 +82,5 @@ resourceMetrics:
dataPoints:
- timeUnixNano: "1684613308030553000"
name: h
scope: {}
scope:
name: github.com/open-telemetry/opentelemetry-collector-contrib/receiver/sqlqueryreceiver

0 comments on commit 017b3d4

Please sign in to comment.