Skip to content

Commit ac74006

Browse files
authored
[receiver/oracledb] Fix incorrect values for a couple of metrics (#32028)
**Description:** Values were being scraped incorrectly for the metrics `oracledb.tablespace_size.limit` and `oracledb.tablespace_size.usage`. The changes these metrics to be scraped from the [`DBA_TABLESPACE_USAGE_METRICS`](https://docs.oracle.com/en/database/oracle/oracle-database/19/refrn/DBA_TABLESPACE_USAGE_METRICS.html#GUID-FE479528-BB37-4B55-92CF-9EC19EDF4F46) table. This results in a slight loss of granularity in these metrics, as values will always be in multiples of the respective tablespace's block size, but I think the clarity and simplicity is worth the trade off. Note: The value of the usage metric was generally close to the expected value, but the limit was being calculated as potential theoretical capacity, unbound by server capacity. For example, in testing in a docker container on my local machine, limit was set to **17TB**. This doesn't line up with user expectations. **Link to tracking Issue:** Fixes #31451 **Testing:** Updated existing tests, added a couple new ones. Also, the original issue filed was comparing `DBA_TABLESPACE_USAGE_METRICS` output for percent used to what we got from `usage/limit * 100`. Here's the local testing outputs compared to show they now line up. ``` 2024-03-27T16:31:57.938-0700 info oracledbreceiver/scraper.go:285 DBA_TABLESPACE_USAGE_METRICS: Tablespace name: SYSTEM, used space: 111288, tablespace size: 3518587, percent used: 3.16286054600895188892586711654422641816 {"kind": "receiver", "name": "oracledb", "data_type": "metrics"} ``` ``` Metric #20 Descriptor: -> Name: oracledb.tablespace_size.usage -> Description: Used tablespace in bytes. -> Unit: By -> DataType: Gauge NumberDataPoints #0 Data point attributes: -> tablespace_name: Str(SYSTEM) StartTimestamp: 2024-03-27 23:31:56.873576 +0000 UTC Timestamp: 2024-03-27 23:32:12.523295 +0000 UTC Value: 911671296 ``` ``` Metric #19 Descriptor: -> Name: oracledb.tablespace_size.limit -> Description: Maximum size of tablespace in bytes, -1 if unlimited. -> Unit: By -> DataType: Gauge NumberDataPoints #0 Data point attributes: -> tablespace_name: Str(SYSTEM) StartTimestamp: 2024-03-27 23:31:56.873576 +0000 UTC Timestamp: 2024-03-27 23:32:12.523295 +0000 UTC Value: 28824264704 ``` Doing the same calculation, we get: ``` (911671296 / 28824264704) * 100 = ~3.16% ```
1 parent b8efeeb commit ac74006

File tree

6 files changed

+85
-41
lines changed

6 files changed

+85
-41
lines changed
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: bug_fix
5+
6+
# The name of the component, or a single word describing the area of concern, (e.g. filelogreceiver)
7+
component: oracledbreceiver
8+
9+
# A brief description of the change. Surround your text with quotes ("") if it needs to start with a backtick (`).
10+
note: Fix incorrect values being set for oracledb.tablespace_size.limit and oracledb.tablespace_size.usage
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: [31451]
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: []

receiver/oracledbreceiver/internal/metadata/generated_metrics.go

Lines changed: 1 addition & 6 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

receiver/oracledbreceiver/internal/metadata/generated_metrics_test.go

Lines changed: 1 addition & 1 deletion
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

receiver/oracledbreceiver/metadata.yaml

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -233,7 +233,6 @@ metrics:
233233
enabled: true
234234
gauge:
235235
value_type: int
236-
input_type: string
237236
unit: By
238237
oracledb.db_block_gets:
239238
description: Number of times a current block was requested from the buffer cache.

receiver/oracledbreceiver/scraper.go

Lines changed: 33 additions & 25 deletions
Original file line numberDiff line numberDiff line change
@@ -38,8 +38,10 @@ const (
3838
consistentGets = "consistent gets"
3939
sessionCountSQL = "select status, type, count(*) as VALUE FROM v$session GROUP BY status, type"
4040
systemResourceLimitsSQL = "select RESOURCE_NAME, CURRENT_UTILIZATION, LIMIT_VALUE, CASE WHEN TRIM(INITIAL_ALLOCATION) LIKE 'UNLIMITED' THEN '-1' ELSE TRIM(INITIAL_ALLOCATION) END as INITIAL_ALLOCATION, CASE WHEN TRIM(LIMIT_VALUE) LIKE 'UNLIMITED' THEN '-1' ELSE TRIM(LIMIT_VALUE) END as LIMIT_VALUE from v$resource_limit"
41-
tablespaceUsageSQL = "select TABLESPACE_NAME, BYTES from DBA_DATA_FILES"
42-
tablespaceMaxSpaceSQL = "select TABLESPACE_NAME, (BLOCK_SIZE*MAX_EXTENTS) AS VALUE FROM DBA_TABLESPACES"
41+
tablespaceUsageSQL = `
42+
select um.TABLESPACE_NAME, um.USED_SPACE, um.TABLESPACE_SIZE, ts.BLOCK_SIZE
43+
FROM DBA_TABLESPACE_USAGE_METRICS um INNER JOIN DBA_TABLESPACES ts
44+
ON um.TABLESPACE_NAME = ts.TABLESPACE_NAME`
4345
)
4446

4547
type dbProviderFunc func() (*sql.DB, error)
@@ -48,7 +50,6 @@ type clientProviderFunc func(*sql.DB, string, *zap.Logger) dbClient
4850

4951
type scraper struct {
5052
statsClient dbClient
51-
tablespaceMaxSpaceClient dbClient
5253
tablespaceUsageClient dbClient
5354
systemResourceLimitsClient dbClient
5455
sessionCountClient dbClient
@@ -88,7 +89,6 @@ func (s *scraper) start(context.Context, component.Host) error {
8889
s.sessionCountClient = s.clientProviderFunc(s.db, sessionCountSQL, s.logger)
8990
s.systemResourceLimitsClient = s.clientProviderFunc(s.db, systemResourceLimitsSQL, s.logger)
9091
s.tablespaceUsageClient = s.clientProviderFunc(s.db, tablespaceUsageSQL, s.logger)
91-
s.tablespaceMaxSpaceClient = s.clientProviderFunc(s.db, tablespaceMaxSpaceSQL, s.logger)
9292
return nil
9393
}
9494

@@ -274,41 +274,49 @@ func (s *scraper) scrape(ctx context.Context) (pmetric.Metrics, error) {
274274
}
275275
}
276276
}
277-
if s.metricsBuilderConfig.Metrics.OracledbTablespaceSizeUsage.Enabled {
277+
278+
if s.metricsBuilderConfig.Metrics.OracledbTablespaceSizeUsage.Enabled ||
279+
s.metricsBuilderConfig.Metrics.OracledbTablespaceSizeLimit.Enabled {
278280
rows, err := s.tablespaceUsageClient.metricRows(ctx)
279281
if err != nil {
280282
scrapeErrors = append(scrapeErrors, fmt.Errorf("error executing %s: %w", tablespaceUsageSQL, err))
281283
} else {
282284
now := pcommon.NewTimestampFromTime(time.Now())
283285
for _, row := range rows {
284286
tablespaceName := row["TABLESPACE_NAME"]
285-
err := s.mb.RecordOracledbTablespaceSizeUsageDataPoint(now, row["BYTES"], tablespaceName)
287+
usedSpaceBlockCount, err := strconv.ParseInt(row["USED_SPACE"], 10, 64)
286288
if err != nil {
287-
scrapeErrors = append(scrapeErrors, err)
289+
scrapeErrors = append(scrapeErrors, fmt.Errorf("failed to parse int64 for OracledbTablespaceSizeUsage, value was %s: %w", row["USED_SPACE"], err))
290+
continue
288291
}
289-
}
290-
}
291-
}
292-
if s.metricsBuilderConfig.Metrics.OracledbTablespaceSizeLimit.Enabled {
293-
rows, err := s.tablespaceMaxSpaceClient.metricRows(ctx)
294-
if err != nil {
295-
scrapeErrors = append(scrapeErrors, fmt.Errorf("error executing %s: %w", tablespaceMaxSpaceSQL, err))
296-
} else {
297-
now := pcommon.NewTimestampFromTime(time.Now())
298-
for _, row := range rows {
299-
tablespaceName := row["TABLESPACE_NAME"]
300-
var val int64
301-
inputVal := row["VALUE"]
302-
if inputVal == "" {
303-
val = -1
292+
293+
tablespaceSizeOriginal := row["TABLESPACE_SIZE"]
294+
var tablespaceSizeBlockCount int64
295+
// Tablespace size should never be empty using the DBA_TABLESPACE_USAGE_METRICS query. This logic is done
296+
// to preserve backward compatibility for with the original metric gathered from querying DBA_TABLESPACES
297+
if tablespaceSizeOriginal == "" {
298+
tablespaceSizeBlockCount = -1
304299
} else {
305-
val, err = strconv.ParseInt(inputVal, 10, 64)
300+
tablespaceSizeBlockCount, err = strconv.ParseInt(tablespaceSizeOriginal, 10, 64)
306301
if err != nil {
307-
scrapeErrors = append(scrapeErrors, fmt.Errorf("failed to parse int64 for OracledbTablespaceSizeLimit, value was %s: %w", inputVal, err))
302+
scrapeErrors = append(scrapeErrors, fmt.Errorf("failed to parse int64 for OracledbTablespaceSizeLimit, value was %s: %w", tablespaceSizeOriginal, err))
308303
continue
309304
}
310305
}
311-
s.mb.RecordOracledbTablespaceSizeLimitDataPoint(now, val, tablespaceName)
306+
307+
blockSize, err := strconv.ParseInt(row["BLOCK_SIZE"], 10, 64)
308+
if err != nil {
309+
scrapeErrors = append(scrapeErrors, fmt.Errorf("failed to parse int64 for OracledbBlockSize, value was %s: %w", row["BLOCK_SIZE"], err))
310+
continue
311+
}
312+
313+
s.mb.RecordOracledbTablespaceSizeUsageDataPoint(now, usedSpaceBlockCount*blockSize, tablespaceName)
314+
315+
if tablespaceSizeBlockCount < 0 {
316+
s.mb.RecordOracledbTablespaceSizeLimitDataPoint(now, -1, tablespaceName)
317+
} else {
318+
s.mb.RecordOracledbTablespaceSizeLimitDataPoint(now, tablespaceSizeBlockCount*blockSize, tablespaceName)
319+
}
312320
}
313321
}
314322
}

receiver/oracledbreceiver/scraper_test.go

Lines changed: 23 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -36,8 +36,7 @@ var queryResponses = map[string][]metricRow{
3636
sessionCountSQL: {{"VALUE": "1"}},
3737
systemResourceLimitsSQL: {{"RESOURCE_NAME": "processes", "CURRENT_UTILIZATION": "3", "MAX_UTILIZATION": "10", "INITIAL_ALLOCATION": "100", "LIMIT_VALUE": "100"},
3838
{"RESOURCE_NAME": "locks", "CURRENT_UTILIZATION": "3", "MAX_UTILIZATION": "10", "INITIAL_ALLOCATION": "-1", "LIMIT_VALUE": "-1"}},
39-
tablespaceUsageSQL: {{"TABLESPACE_NAME": "SYS", "BYTES": "1024"}},
40-
tablespaceMaxSpaceSQL: {{"TABLESPACE_NAME": "SYS", "VALUE": "1024"}},
39+
tablespaceUsageSQL: {{"TABLESPACE_NAME": "SYS", "USED_SPACE": "111288", "TABLESPACE_SIZE": "3518587", "BLOCK_SIZE": "8192"}},
4140
}
4241

4342
func TestScraper_Scrape(t *testing.T) {
@@ -76,11 +75,11 @@ func TestScraper_Scrape(t *testing.T) {
7675
{
7776
name: "no limit on tablespace",
7877
dbclientFn: func(_ *sql.DB, s string, _ *zap.Logger) dbClient {
79-
if s == tablespaceMaxSpaceSQL {
78+
if s == tablespaceUsageSQL {
8079
return &fakeDbClient{Responses: [][]metricRow{
8180
{
82-
{"TABLESPACE_NAME": "SYS", "VALUE": "1024"},
83-
{"TABLESPACE_NAME": "FOO", "VALUE": ""},
81+
{"TABLESPACE_NAME": "SYS", "TABLESPACE_SIZE": "1024", "USED_SPACE": "111288", "BLOCK_SIZE": "8192"},
82+
{"TABLESPACE_NAME": "FOO", "TABLESPACE_SIZE": "", "USED_SPACE": "111288", "BLOCK_SIZE": "8192"},
8483
},
8584
}}
8685
}
@@ -92,11 +91,11 @@ func TestScraper_Scrape(t *testing.T) {
9291
{
9392
name: "bad value on tablespace",
9493
dbclientFn: func(_ *sql.DB, s string, _ *zap.Logger) dbClient {
95-
if s == tablespaceMaxSpaceSQL {
94+
if s == tablespaceUsageSQL {
9695
return &fakeDbClient{Responses: [][]metricRow{
9796
{
98-
{"TABLESPACE_NAME": "SYS", "VALUE": "1024"},
99-
{"TABLESPACE_NAME": "FOO", "VALUE": "ert"},
97+
{"TABLESPACE_NAME": "SYS", "TABLESPACE_SIZE": "1024", "USED_SPACE": "111288", "BLOCK_SIZE": "8192"},
98+
{"TABLESPACE_NAME": "FOO", "TABLESPACE_SIZE": "ert", "USED_SPACE": "111288", "BLOCK_SIZE": "8192"},
10099
},
101100
}}
102101
}
@@ -106,6 +105,22 @@ func TestScraper_Scrape(t *testing.T) {
106105
},
107106
errWanted: `failed to parse int64 for OracledbTablespaceSizeLimit, value was ert: strconv.ParseInt: parsing "ert": invalid syntax`,
108107
},
108+
{
109+
name: "Empty block size",
110+
dbclientFn: func(_ *sql.DB, s string, _ *zap.Logger) dbClient {
111+
if s == tablespaceUsageSQL {
112+
return &fakeDbClient{Responses: [][]metricRow{
113+
{
114+
{"TABLESPACE_NAME": "SYS", "TABLESPACE_SIZE": "1024", "USED_SPACE": "111288", "BLOCK_SIZE": ""},
115+
},
116+
}}
117+
}
118+
return &fakeDbClient{Responses: [][]metricRow{
119+
queryResponses[s],
120+
}}
121+
},
122+
errWanted: `failed to parse int64 for OracledbBlockSize, value was : strconv.ParseInt: parsing "": invalid syntax`,
123+
},
109124
}
110125
for _, test := range tests {
111126
t.Run(test.name, func(t *testing.T) {

0 commit comments

Comments
 (0)