From a7d053ae5a9fb073ccc3b639c3359fba19594e3d Mon Sep 17 00:00:00 2001 From: Anthony Telljohann Date: Fri, 14 Oct 2022 11:10:42 -0400 Subject: [PATCH] fix(resources): fix when attribute values contain spaces (#3302) Co-authored-by: Daniel Dyla --- CHANGELOG.md | 3 +++ .../src/detectors/EnvDetector.ts | 4 ++-- .../test/detectors/node/EnvDetector.test.ts | 10 +++++----- 3 files changed, 10 insertions(+), 7 deletions(-) diff --git a/CHANGELOG.md b/CHANGELOG.md index 5af8762b41..b17fd36d5b 100644 --- a/CHANGELOG.md +++ b/CHANGELOG.md @@ -12,6 +12,9 @@ All notable changes to this project will be documented in this file. ### :bug: (Bug Fix) +* fix(resources): fix EnvDetector throwing errors when attribute values contain spaces + [#3295](https://github.com/open-telemetry/opentelemetry-js/issues/3295) + ### :books: (Refine Doc) ### :house: (Internal) diff --git a/packages/opentelemetry-resources/src/detectors/EnvDetector.ts b/packages/opentelemetry-resources/src/detectors/EnvDetector.ts index 782f664a1a..44f15dcdfe 100644 --- a/packages/opentelemetry-resources/src/detectors/EnvDetector.ts +++ b/packages/opentelemetry-resources/src/detectors/EnvDetector.ts @@ -110,7 +110,7 @@ class EnvDetector implements Detector { let [key, value] = keyValuePair; // Leading and trailing whitespaces are trimmed. key = key.trim(); - value = value.trim().split('^"|"$').join(''); + value = value.trim().split(/^"|"$/).join(''); if (!this._isValidAndNotEmpty(key)) { throw new Error(`Attribute key ${this._ERROR_MESSAGE_INVALID_CHARS}`); } @@ -136,7 +136,7 @@ class EnvDetector implements Detector { private _isPrintableString(str: string): boolean { for (let i = 0; i < str.length; i++) { const ch: string = str.charAt(i); - if (ch <= ' ' || ch >= '~') { + if (ch < ' ' || ch >= '~') { return false; } } diff --git a/packages/opentelemetry-resources/test/detectors/node/EnvDetector.test.ts b/packages/opentelemetry-resources/test/detectors/node/EnvDetector.test.ts index 2a2746e14b..486de2b4bb 100644 --- a/packages/opentelemetry-resources/test/detectors/node/EnvDetector.test.ts +++ b/packages/opentelemetry-resources/test/detectors/node/EnvDetector.test.ts @@ -14,7 +14,6 @@ * limitations under the License. */ -import { SemanticResourceAttributes } from '@opentelemetry/semantic-conventions'; import { envDetector, Resource } from '../../../src'; import { assertK8sResource, @@ -26,7 +25,7 @@ describeNode('envDetector() on Node.js', () => { describe('with valid env', () => { before(() => { process.env.OTEL_RESOURCE_ATTRIBUTES = - 'k8s.pod.name="pod-xyz-123",k8s.cluster.name="c1",k8s.namespace.name="default"'; + 'k8s.pod.name="pod-xyz-123",k8s.cluster.name="c1",k8s.namespace.name="default",k8s.deployment.name="deployment name"'; }); after(() => { @@ -36,9 +35,10 @@ describeNode('envDetector() on Node.js', () => { it('should return resource information from environment variable', async () => { const resource: Resource = await envDetector.detect(); assertK8sResource(resource, { - [SemanticResourceAttributes.K8S_POD_NAME]: 'pod-xyz-123', - [SemanticResourceAttributes.K8S_CLUSTER_NAME]: 'c1', - [SemanticResourceAttributes.K8S_NAMESPACE_NAME]: 'default', + podName: 'pod-xyz-123', + clusterName: 'c1', + namespaceName: 'default', + deploymentName: 'deployment name' }); }); });