Skip to content

Commit 7a1aec4

Browse files
committed
create function by paramtre type
1 parent 73fe9af commit 7a1aec4

File tree

4 files changed

+159
-22
lines changed

4 files changed

+159
-22
lines changed

experimental/packages/opentelemetry-configuration/package.json

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -49,6 +49,7 @@
4949
"@types/mocha": "10.0.10",
5050
"@types/node": "18.6.5",
5151
"@types/sinon": "17.0.4",
52+
"eslint-plugin-n": "^17.23.0",
5253
"lerna": "6.6.2",
5354
"mocha": "11.7.2",
5455
"nyc": "17.1.0",

experimental/packages/opentelemetry-configuration/src/FileConfigProvider.ts

Lines changed: 10 additions & 22 deletions
Original file line numberDiff line numberDiff line change
@@ -14,17 +14,19 @@
1414
* limitations under the License.
1515
*/
1616

17-
import {
18-
diagLogLevelFromString,
19-
getStringFromEnv,
20-
} from '@opentelemetry/core';
17+
import { diagLogLevelFromString, getStringFromEnv } from '@opentelemetry/core';
2118
import {
2219
ConfigurationModel,
2320
initializeDefaultConfiguration,
2421
} from './configModel';
2522
import { ConfigProvider } from './IConfigProvider';
2623
import * as fs from 'fs';
2724
import * as yaml from 'yaml';
25+
import {
26+
getBooleanFromConfigFile,
27+
getNumberFromConfigFile,
28+
getStringFromConfigFile,
29+
} from './utils';
2830

2931
export class FileConfigProvider implements ConfigProvider {
3032
private _config: ConfigurationModel;
@@ -65,29 +67,26 @@ function parseConfigFile(config: ConfigurationModel) {
6567
parsedContent['file_format'] &&
6668
supportedFileVersions.includes(parsedContent['file_format'])
6769
) {
68-
const disabled = getValue(config.disabled, parsedContent['disabled']);
70+
const disabled = getBooleanFromConfigFile(parsedContent['disabled']);
6971
if (disabled || disabled === false) {
7072
config.disabled = disabled;
7173
}
7274

73-
const logLevel = getValue(
74-
config.log_level,
75+
const logLevel = getNumberFromConfigFile(
7576
diagLogLevelFromString(parsedContent['log_level'])
7677
);
7778
if (logLevel) {
7879
config.log_level = logLevel;
7980
}
8081

81-
const attrList = getValue(
82-
config.resource.attributes_list,
82+
const attrList = getStringFromConfigFile(
8383
parsedContent['resource']?.['attributes_list']
8484
);
8585
if (attrList) {
8686
config.resource.attributes_list = attrList;
8787
}
8888

89-
const schemaUrl = getValue(
90-
config.resource.schema_url,
89+
const schemaUrl = getStringFromConfigFile(
9190
parsedContent['resource']?.['schema_url']
9291
);
9392
if (schemaUrl) {
@@ -102,17 +101,6 @@ function parseConfigFile(config: ConfigurationModel) {
102101
}
103102
}
104103

105-
// eslint-disable-next-line @typescript-eslint/no-explicit-any
106-
function getValue(obj: unknown, value: unknown): any {
107-
if (typeof obj === 'boolean') {
108-
const raw = String(value)?.trim().toLowerCase();
109-
if (raw === 'false') {
110-
return false;
111-
}
112-
}
113-
return value;
114-
}
115-
116104
// eslint-disable-next-line @typescript-eslint/no-explicit-any
117105
function setResourceAttributes(config: ConfigurationModel, attributes: any[]) {
118106
if (attributes) {
Lines changed: 79 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,79 @@
1+
/*
2+
* Copyright The OpenTelemetry Authors
3+
*
4+
* Licensed under the Apache License, Version 2.0 (the "License");
5+
* you may not use this file except in compliance with the License.
6+
* You may obtain a copy of the License at
7+
*
8+
* https://www.apache.org/licenses/LICENSE-2.0
9+
*
10+
* Unless required by applicable law or agreed to in writing, software
11+
* distributed under the License is distributed on an "AS IS" BASIS,
12+
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13+
* See the License for the specific language governing permissions and
14+
* limitations under the License.
15+
*/
16+
import { diag } from '@opentelemetry/api';
17+
import { inspect } from 'util';
18+
19+
/**
20+
* Retrieves a boolean value from a configuration file parameter.
21+
* - Trims leading and trailing whitespace and ignores casing.
22+
* - Returns `undefined` if the value is empty, unset, or contains only whitespace.
23+
* - Returns `undefined` and a warning for values that cannot be mapped to a boolean.
24+
*
25+
* @param {unknown} value - The value from the config file.
26+
* @returns {boolean} - The boolean value or `false` if the environment variable is unset empty, unset, or contains only whitespace.
27+
*/
28+
export function getBooleanFromConfigFile(value: unknown): boolean | undefined {
29+
const raw = String(value)?.trim().toLowerCase();
30+
if (raw === 'true') {
31+
return true;
32+
} else if (raw === 'false') {
33+
return false;
34+
} else if (raw == null || raw === '') {
35+
return undefined;
36+
} else {
37+
diag.warn(`Unknown value ${inspect(raw)}, expected 'true' or 'false'`);
38+
return undefined;
39+
}
40+
}
41+
42+
/**
43+
* Retrieves a number from a configuration file parameter.
44+
* - Returns `undefined` if the environment variable is empty, unset, or contains only whitespace.
45+
* - Returns `undefined` and a warning if is not a number.
46+
* - Returns a number in all other cases.
47+
*
48+
* @param {unknown} value - The value from the config file.
49+
* @returns {number | undefined} - The number value or `undefined`.
50+
*/
51+
export function getNumberFromConfigFile(value: unknown): number | undefined {
52+
const raw = String(value)?.trim();
53+
if (raw == null || raw.trim() === '') {
54+
return undefined;
55+
}
56+
57+
const n = Number(raw);
58+
if (isNaN(n)) {
59+
diag.warn(`Unknown value ${inspect(raw)}, expected a number`);
60+
return undefined;
61+
}
62+
63+
return n;
64+
}
65+
66+
/**
67+
* Retrieves a string from a configuration file parameter.
68+
* - Returns `undefined` if the environment variable is empty, unset, or contains only whitespace.
69+
*
70+
* @param {unknown} value - The value from the config file.
71+
* @returns {string | undefined} - The string value or `undefined`.
72+
*/
73+
export function getStringFromConfigFile(value: unknown): string | undefined {
74+
const raw = String(value)?.trim();
75+
if (value == null || raw === '') {
76+
return undefined;
77+
}
78+
return raw;
79+
}
Lines changed: 69 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,69 @@
1+
/*
2+
* Copyright The OpenTelemetry Authors
3+
*
4+
* Licensed under the Apache License, Version 2.0 (the "License");
5+
* you may not use this file except in compliance with the License.
6+
* You may obtain a copy of the License at
7+
*
8+
* https://www.apache.org/licenses/LICENSE-2.0
9+
*
10+
* Unless required by applicable law or agreed to in writing, software
11+
* distributed under the License is distributed on an "AS IS" BASIS,
12+
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13+
* See the License for the specific language governing permissions and
14+
* limitations under the License.
15+
*/
16+
17+
import * as assert from 'assert';
18+
import * as sinon from 'sinon';
19+
import { diag } from '@opentelemetry/api';
20+
import {
21+
getBooleanFromConfigFile,
22+
getNumberFromConfigFile,
23+
getStringFromConfigFile,
24+
} from '../src/utils';
25+
26+
describe('config utils', function () {
27+
afterEach(function () {
28+
sinon.restore();
29+
});
30+
31+
it('should return correct values for getBooleanFromConfigFile', function () {
32+
assert.strictEqual(getBooleanFromConfigFile(null), undefined);
33+
assert.strictEqual(getBooleanFromConfigFile(' '), undefined);
34+
assert.strictEqual(getBooleanFromConfigFile(true), true);
35+
assert.strictEqual(getBooleanFromConfigFile('true'), true);
36+
assert.strictEqual(getBooleanFromConfigFile(false), false);
37+
assert.strictEqual(getBooleanFromConfigFile('false'), false);
38+
39+
const warnStub = sinon.stub(diag, 'warn');
40+
assert.strictEqual(getBooleanFromConfigFile('non-boolean'), undefined);
41+
sinon.assert.calledOnceWithMatch(
42+
warnStub,
43+
`Unknown value 'non-boolean', expected 'true' or 'false'`
44+
);
45+
});
46+
47+
it('should return correct values for getNumberFromConfigFile', function () {
48+
assert.strictEqual(getNumberFromConfigFile(null), undefined);
49+
assert.strictEqual(getNumberFromConfigFile(' '), undefined);
50+
assert.strictEqual(getNumberFromConfigFile(1), 1);
51+
assert.strictEqual(getNumberFromConfigFile(0), 0);
52+
assert.strictEqual(getNumberFromConfigFile(100), 100);
53+
54+
const warnStub = sinon.stub(diag, 'warn');
55+
assert.strictEqual(getNumberFromConfigFile('non-number'), undefined);
56+
sinon.assert.calledOnceWithMatch(
57+
warnStub,
58+
`Unknown value 'non-number', expected a number`
59+
);
60+
});
61+
62+
it('should return correct values for getStringFromConfigFile', function () {
63+
assert.strictEqual(getStringFromConfigFile(null), undefined);
64+
assert.strictEqual(getStringFromConfigFile(' '), undefined);
65+
assert.strictEqual(getStringFromConfigFile(undefined), undefined);
66+
assert.strictEqual(getStringFromConfigFile(1), '1');
67+
assert.strictEqual(getStringFromConfigFile('string-value'), 'string-value');
68+
});
69+
});

0 commit comments

Comments
 (0)