Skip to content

Commit 13c0790

Browse files
feat(opentelemetry-configuration): set attributes from attribute list from env variables (#6043)
Co-authored-by: Jamie Danielson <[email protected]>
1 parent 0e09680 commit 13c0790

File tree

6 files changed

+61
-4
lines changed

6 files changed

+61
-4
lines changed

experimental/CHANGELOG.md

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -6,6 +6,8 @@ For notes on migrating to 2.x / 0.200.x see [the upgrade guide](doc/upgrade-to-2
66

77
## Unreleased
88

9+
* feat(opentelemetry-configuration): set attributes from attribute list from env variables [#6043](https://github.com/open-telemetry/opentelemetry-js/pull/6043) @maryliag
10+
911
### :boom: Breaking Changes
1012

1113
* feat(otlp-exporter-base)!: allow passing an async function to headers option [#5994](https://github.com/open-telemetry/opentelemetry-js/pull/5994/files) @pichlermarc

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

Lines changed: 11 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -73,8 +73,18 @@ export function setResources(config: ConfigurationModel): void {
7373
}
7474

7575
const resourceAttrList = getStringFromEnv('OTEL_RESOURCE_ATTRIBUTES');
76-
if (resourceAttrList) {
76+
const list = getStringListFromEnv('OTEL_RESOURCE_ATTRIBUTES');
77+
if (list && list.length > 0) {
7778
config.resource.attributes_list = resourceAttrList;
79+
config.resource.attributes = [];
80+
for (let i = 0; i < list.length; i++) {
81+
const element = list[i].split('=');
82+
config.resource.attributes.push({
83+
name: element[0],
84+
value: element[1],
85+
type: 'string',
86+
});
87+
}
7888
}
7989

8090
const serviceName = getStringFromEnv('OTEL_SERVICE_NAME');

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

Lines changed: 18 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -121,6 +121,24 @@ export function parseConfigFile(config: ConfigurationModel) {
121121
);
122122
if (attrList) {
123123
config.resource.attributes_list = attrList;
124+
const list = getStringListFromConfigFile(
125+
parsedContent['resource']?.['attributes_list']
126+
);
127+
if (
128+
list &&
129+
list.length > 0 &&
130+
parsedContent['resource']?.['attributes'] == null
131+
) {
132+
config.resource.attributes = [];
133+
for (let i = 0; i < list.length; i++) {
134+
const element = list[i].split('=');
135+
config.resource.attributes.push({
136+
name: element[0],
137+
value: element[1],
138+
type: 'string',
139+
});
140+
}
141+
}
124142
}
125143

126144
const schemaUrl = getStringFromConfigFile(

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

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -14,6 +14,6 @@
1414
* limitations under the License.
1515
*/
1616

17-
export { EnvironmentConfigProvider } from './EnvironmentConfigProvider';
1817
export type { ConfigProvider } from './IConfigProvider';
1918
export type { ConfigurationModel as Configuration } from './models/configModel';
19+
export { createConfigProvider } from './ConfigProvider';

experimental/packages/opentelemetry-configuration/test/ConfigProvider.test.ts

Lines changed: 26 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -809,6 +809,18 @@ describe('ConfigProvider', function () {
809809
resource: {
810810
attributes_list:
811811
'service.namespace=my-namespace,service.version=1.0.0',
812+
attributes: [
813+
{
814+
name: 'service.namespace',
815+
value: 'my-namespace',
816+
type: 'string',
817+
},
818+
{
819+
name: 'service.version',
820+
value: '1.0.0',
821+
type: 'string',
822+
},
823+
],
812824
},
813825
};
814826
const configProvider = createConfigProvider();
@@ -1371,9 +1383,22 @@ describe('ConfigProvider', function () {
13711383
process.env.OTEL_EXPERIMENTAL_CONFIG_FILE =
13721384
'test/fixtures/short-config.yml';
13731385
const configProvider = createConfigProvider();
1386+
const expectedConfig: Configuration = {
1387+
...defaultConfig,
1388+
resource: {
1389+
attributes_list: 'service.instance.id=123',
1390+
attributes: [
1391+
{
1392+
name: 'service.instance.id',
1393+
value: '123',
1394+
type: 'string',
1395+
},
1396+
],
1397+
},
1398+
};
13741399
assert.deepStrictEqual(
13751400
configProvider.getInstrumentationConfig(),
1376-
defaultConfig
1401+
expectedConfig
13771402
);
13781403
});
13791404

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,2 +1,4 @@
11
file_format: "1.0-rc.1"
2-
disabled: false
2+
disabled: false
3+
resource:
4+
attributes_list: service.instance.id=123

0 commit comments

Comments
 (0)