Skip to content

Commit 09110b1

Browse files
dehaansazeck-ops
authored andcommitted
[receiver/sqlqueryreceiver] Add instrumentation scope to sqlqueryreceiver metrics & logs (open-telemetry#36812)
#### Description Add instrumentation scope to the metrics and logs emitted by the sqlqueryreceiver. #### Link to tracking issue Fixes open-telemetry#31028
1 parent 209fae3 commit 09110b1

File tree

9 files changed

+102
-46
lines changed

9 files changed

+102
-46
lines changed

.chloggen/sqlqueryreceiver-scope.yaml

Lines changed: 27 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,27 @@
1+
# Use this changelog template to create an entry for release notes.
2+
3+
# One of 'breaking', 'deprecation', 'new_component', 'enhancement', 'bug_fix'
4+
change_type: enhancement
5+
6+
# The name of the component, or a single word describing the area of concern, (e.g. filelogreceiver)
7+
component: sqlqueryreceiver
8+
9+
# A brief description of the change. Surround your text with quotes ("") if it needs to start with a backtick (`).
10+
note: "Add instrumentation scope to SQL query receiver metrics and logs"
11+
12+
# Mandatory: One or more tracking issues related to the change. You can use the PR number here if no issue exists.
13+
issues: [31028]
14+
15+
# (Optional) One or more lines of additional information to render under the primary note.
16+
# These lines will be padded with 2 spaces and then inserted directly into the document.
17+
# Use pipe (|) for multiline entries.
18+
subtext:
19+
20+
# If your change doesn't affect end users or the exported elements of any package,
21+
# you should instead start your pull request title with [chore] or use the "Skip Changelog" label.
22+
# Optional: The change log or logs in which this entry should be included.
23+
# e.g. '[user]' or '[user, api]'
24+
# Include 'user' if the change is relevant to end users.
25+
# Include 'api' if there is a change to a library API.
26+
# Default: '[user]'
27+
change_logs: [user]

internal/sqlquery/scraper.go

Lines changed: 21 additions & 18 deletions
Original file line numberDiff line numberDiff line change
@@ -26,29 +26,31 @@ type DbProviderFunc func() (*sql.DB, error)
2626
type ClientProviderFunc func(Db, string, *zap.Logger, TelemetryConfig) DbClient
2727

2828
type Scraper struct {
29-
id component.ID
30-
Query Query
31-
ScrapeCfg scraperhelper.ControllerConfig
32-
StartTime pcommon.Timestamp
33-
ClientProviderFunc ClientProviderFunc
34-
DbProviderFunc DbProviderFunc
35-
Logger *zap.Logger
36-
Telemetry TelemetryConfig
37-
Client DbClient
38-
Db *sql.DB
29+
id component.ID
30+
Query Query
31+
ScrapeCfg scraperhelper.ControllerConfig
32+
StartTime pcommon.Timestamp
33+
ClientProviderFunc ClientProviderFunc
34+
DbProviderFunc DbProviderFunc
35+
Logger *zap.Logger
36+
Telemetry TelemetryConfig
37+
Client DbClient
38+
Db *sql.DB
39+
InstrumentationScope pcommon.InstrumentationScope
3940
}
4041

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

43-
func NewScraper(id component.ID, query Query, scrapeCfg scraperhelper.ControllerConfig, logger *zap.Logger, telemetry TelemetryConfig, dbProviderFunc DbProviderFunc, clientProviderFunc ClientProviderFunc) *Scraper {
44+
func NewScraper(id component.ID, query Query, scrapeCfg scraperhelper.ControllerConfig, logger *zap.Logger, telemetry TelemetryConfig, dbProviderFunc DbProviderFunc, clientProviderFunc ClientProviderFunc, instrumentationScope pcommon.InstrumentationScope) *Scraper {
4445
return &Scraper{
45-
id: id,
46-
Query: query,
47-
ScrapeCfg: scrapeCfg,
48-
Logger: logger,
49-
Telemetry: telemetry,
50-
DbProviderFunc: dbProviderFunc,
51-
ClientProviderFunc: clientProviderFunc,
46+
id: id,
47+
Query: query,
48+
ScrapeCfg: scrapeCfg,
49+
Logger: logger,
50+
Telemetry: telemetry,
51+
DbProviderFunc: dbProviderFunc,
52+
ClientProviderFunc: clientProviderFunc,
53+
InstrumentationScope: instrumentationScope,
5254
}
5355
}
5456

@@ -83,6 +85,7 @@ func (s *Scraper) ScrapeMetrics(ctx context.Context) (pmetric.Metrics, error) {
8385
rm := rms.AppendEmpty()
8486
sms := rm.ScopeMetrics()
8587
sm := sms.AppendEmpty()
88+
s.InstrumentationScope.CopyTo(sm.Scope())
8689
ms := sm.Metrics()
8790
var errs []error
8891
for _, metricCfg := range s.Query.Metrics {

internal/sqlquery/scraper_test.go

Lines changed: 37 additions & 19 deletions
Original file line numberDiff line numberDiff line change
@@ -20,6 +20,7 @@ import (
2020

2121
func TestScraper_ErrorOnStart(t *testing.T) {
2222
scrpr := Scraper{
23+
InstrumentationScope: pcommon.NewInstrumentationScope(),
2324
DbProviderFunc: func() (*sql.DB, error) {
2425
return nil, errors.New("oops")
2526
},
@@ -33,7 +34,8 @@ func TestScraper_ClientErrorOnScrape(t *testing.T) {
3334
Err: errors.New("oops"),
3435
}
3536
scrpr := Scraper{
36-
Client: client,
37+
InstrumentationScope: pcommon.NewInstrumentationScope(),
38+
Client: client,
3739
}
3840
_, err := scrpr.ScrapeMetrics(context.Background())
3941
require.Error(t, err)
@@ -46,7 +48,8 @@ func TestScraper_RowToMetricErrorOnScrape_Float(t *testing.T) {
4648
},
4749
}
4850
scrpr := Scraper{
49-
Client: client,
51+
InstrumentationScope: pcommon.NewInstrumentationScope(),
52+
Client: client,
5053
Query: Query{
5154
Metrics: []MetricCfg{{
5255
MetricName: "my.float",
@@ -68,7 +71,8 @@ func TestScraper_RowToMetricErrorOnScrape_Int(t *testing.T) {
6871
},
6972
}
7073
scrpr := Scraper{
71-
Client: client,
74+
InstrumentationScope: pcommon.NewInstrumentationScope(),
75+
Client: client,
7276
Query: Query{
7377
Metrics: []MetricCfg{{
7478
MetricName: "my.int",
@@ -91,7 +95,8 @@ func TestScraper_RowToMetricMultiErrorsOnScrape(t *testing.T) {
9195
}},
9296
}
9397
scrpr := Scraper{
94-
Client: client,
98+
InstrumentationScope: pcommon.NewInstrumentationScope(),
99+
Client: client,
95100
Query: Query{
96101
Metrics: []MetricCfg{{
97102
MetricName: "my.col",
@@ -108,6 +113,7 @@ func TestScraper_RowToMetricMultiErrorsOnScrape(t *testing.T) {
108113

109114
func TestScraper_SingleRow_MultiMetrics(t *testing.T) {
110115
scrpr := Scraper{
116+
InstrumentationScope: pcommon.NewInstrumentationScope(),
111117
Client: &FakeDBClient{
112118
StringMaps: [][]StringMap{{{
113119
"count": "42",
@@ -191,7 +197,8 @@ func TestScraper_MultiRow(t *testing.T) {
191197
}},
192198
}
193199
scrpr := Scraper{
194-
Client: client,
200+
InstrumentationScope: pcommon.NewInstrumentationScope(),
201+
Client: client,
195202
Query: Query{
196203
Metrics: []MetricCfg{
197204
{
@@ -231,7 +238,8 @@ func TestScraper_MultiResults_CumulativeSum(t *testing.T) {
231238
},
232239
}
233240
scrpr := Scraper{
234-
Client: client,
241+
InstrumentationScope: pcommon.NewInstrumentationScope(),
242+
Client: client,
235243
Query: Query{
236244
Metrics: []MetricCfg{{
237245
MetricName: "transaction.count",
@@ -254,7 +262,8 @@ func TestScraper_MultiResults_DeltaSum(t *testing.T) {
254262
},
255263
}
256264
scrpr := Scraper{
257-
Client: client,
265+
InstrumentationScope: pcommon.NewInstrumentationScope(),
266+
Client: client,
258267
Query: Query{
259268
Metrics: []MetricCfg{{
260269
MetricName: "transaction.count",
@@ -290,7 +299,8 @@ func TestScraper_Float(t *testing.T) {
290299
},
291300
}
292301
scrpr := Scraper{
293-
Client: client,
302+
InstrumentationScope: pcommon.NewInstrumentationScope(),
303+
Client: client,
294304
Query: Query{
295305
Metrics: []MetricCfg{{
296306
MetricName: "my.float",
@@ -314,7 +324,8 @@ func TestScraper_DescriptionAndUnit(t *testing.T) {
314324
},
315325
}
316326
scrpr := Scraper{
317-
Client: client,
327+
InstrumentationScope: pcommon.NewInstrumentationScope(),
328+
Client: client,
318329
Query: Query{
319330
Metrics: []MetricCfg{{
320331
MetricName: "my.name",
@@ -335,8 +346,9 @@ func TestScraper_FakeDB_Warnings(t *testing.T) {
335346
db := fakeDB{rowVals: [][]any{{42, nil}}}
336347
logger := zap.NewNop()
337348
scrpr := Scraper{
338-
Client: NewDbClient(db, "", logger, TelemetryConfig{}),
339-
Logger: logger,
349+
InstrumentationScope: pcommon.NewInstrumentationScope(),
350+
Client: NewDbClient(db, "", logger, TelemetryConfig{}),
351+
Logger: logger,
340352
Query: Query{
341353
Metrics: []MetricCfg{{
342354
MetricName: "my.name",
@@ -354,8 +366,9 @@ func TestScraper_FakeDB_MultiRows_Warnings(t *testing.T) {
354366
db := fakeDB{rowVals: [][]any{{42, nil}, {43, nil}}}
355367
logger := zap.NewNop()
356368
scrpr := Scraper{
357-
Client: NewDbClient(db, "", logger, TelemetryConfig{}),
358-
Logger: logger,
369+
InstrumentationScope: pcommon.NewInstrumentationScope(),
370+
Client: NewDbClient(db, "", logger, TelemetryConfig{}),
371+
Logger: logger,
359372
Query: Query{
360373
Metrics: []MetricCfg{{
361374
MetricName: "my.col.0",
@@ -375,8 +388,9 @@ func TestScraper_FakeDB_MultiRows_Error(t *testing.T) {
375388
db := fakeDB{rowVals: [][]any{{42, nil}, {43, nil}}}
376389
logger := zap.NewNop()
377390
scrpr := Scraper{
378-
Client: NewDbClient(db, "", logger, TelemetryConfig{}),
379-
Logger: logger,
391+
InstrumentationScope: pcommon.NewInstrumentationScope(),
392+
Client: NewDbClient(db, "", logger, TelemetryConfig{}),
393+
Logger: logger,
380394
Query: Query{
381395
Metrics: []MetricCfg{
382396
{
@@ -412,7 +426,8 @@ func TestScraper_StartAndTSColumn(t *testing.T) {
412426
}},
413427
}
414428
scrpr := Scraper{
415-
Client: client,
429+
InstrumentationScope: pcommon.NewInstrumentationScope(),
430+
Client: client,
416431
Query: Query{
417432
Metrics: []MetricCfg{{
418433
MetricName: "my.name",
@@ -441,7 +456,8 @@ func TestScraper_StartAndTS_ErrorOnColumnNotFound(t *testing.T) {
441456
}},
442457
}
443458
scrpr := Scraper{
444-
Client: client,
459+
InstrumentationScope: pcommon.NewInstrumentationScope(),
460+
Client: client,
445461
Query: Query{
446462
Metrics: []MetricCfg{{
447463
MetricName: "my.name",
@@ -466,7 +482,8 @@ func TestScraper_CollectRowToMetricsErrors(t *testing.T) {
466482
}},
467483
}
468484
scrpr := Scraper{
469-
Client: client,
485+
InstrumentationScope: pcommon.NewInstrumentationScope(),
486+
Client: client,
470487
Query: Query{
471488
Metrics: []MetricCfg{{
472489
MetricName: "my.name",
@@ -496,7 +513,8 @@ func TestScraper_StartAndTS_ErrorOnParse(t *testing.T) {
496513
}},
497514
}
498515
scrpr := Scraper{
499-
Client: client,
516+
InstrumentationScope: pcommon.NewInstrumentationScope(),
517+
Client: client,
500518
Query: Query{
501519
Metrics: []MetricCfg{{
502520
MetricName: "my.name",

receiver/sqlqueryreceiver/integration_test.go

Lines changed: 0 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,8 +1,6 @@
11
// Copyright The OpenTelemetry Authors
22
// SPDX-License-Identifier: Apache-2.0
33

4-
//go:build integration
5-
64
package sqlqueryreceiver
75

86
import (

receiver/sqlqueryreceiver/logs_receiver.go

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -285,7 +285,9 @@ func (queryReceiver *logsQueryReceiver) collect(ctx context.Context) (plog.Logs,
285285
}
286286

287287
var errs []error
288-
scopeLogs := logs.ResourceLogs().AppendEmpty().ScopeLogs().AppendEmpty().LogRecords()
288+
scope := logs.ResourceLogs().AppendEmpty().ScopeLogs().AppendEmpty()
289+
scope.Scope().SetName(metadata.ScopeName)
290+
scopeLogs := scope.LogRecords()
289291
for logsConfigIndex, logsConfig := range queryReceiver.query.Logs {
290292
for _, row := range rows {
291293
logRecord := scopeLogs.AppendEmpty()

receiver/sqlqueryreceiver/receiver.go

Lines changed: 4 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -10,6 +10,7 @@ import (
1010

1111
"go.opentelemetry.io/collector/component"
1212
"go.opentelemetry.io/collector/consumer"
13+
"go.opentelemetry.io/collector/pdata/pcommon"
1314
"go.opentelemetry.io/collector/receiver"
1415
"go.opentelemetry.io/collector/receiver/scraperhelper"
1516

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

5154
opt := scraperhelper.AddScraper(metadata.Type, mp)
5255
opts = append(opts, opt)

receiver/sqlqueryreceiver/testdata/integration/mysql/expected.yaml

Lines changed: 4 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -38,7 +38,8 @@ resourceMetrics:
3838
stringValue: Action
3939
timeUnixNano: "1684614725494431000"
4040
name: genre.imdb
41-
scope: {}
41+
scope:
42+
name: github.com/open-telemetry/opentelemetry-collector-contrib/receiver/sqlqueryreceiver
4243
- resource: {}
4344
scopeMetrics:
4445
- metrics:
@@ -72,4 +73,5 @@ resourceMetrics:
7273
- asDouble: 3.4
7374
timeUnixNano: "1684614725499638000"
7475
name: f
75-
scope: {}
76+
scope:
77+
name: github.com/open-telemetry/opentelemetry-collector-contrib/receiver/sqlqueryreceiver

receiver/sqlqueryreceiver/testdata/integration/oracle/expected.yaml

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -38,4 +38,5 @@ resourceMetrics:
3838
stringValue: Action
3939
timeUnixNano: "1684632870521947236"
4040
name: genre.imdb
41-
scope: {}
41+
scope:
42+
name: github.com/open-telemetry/opentelemetry-collector-contrib/receiver/sqlqueryreceiver

receiver/sqlqueryreceiver/testdata/integration/postgresql/expected.yaml

Lines changed: 4 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -38,7 +38,8 @@ resourceMetrics:
3838
stringValue: SciFi
3939
timeUnixNano: "1684613308016320000"
4040
name: genre.imdb
41-
scope: {}
41+
scope:
42+
name: github.com/open-telemetry/opentelemetry-collector-contrib/receiver/sqlqueryreceiver
4243
- resource: {}
4344
scopeMetrics:
4445
- metrics:
@@ -81,4 +82,5 @@ resourceMetrics:
8182
dataPoints:
8283
- timeUnixNano: "1684613308030553000"
8384
name: h
84-
scope: {}
85+
scope:
86+
name: github.com/open-telemetry/opentelemetry-collector-contrib/receiver/sqlqueryreceiver

0 commit comments

Comments
 (0)