Skip to content

Commit

Permalink
fix(resources): fix when attribute values contain spaces (#3302)
Browse files Browse the repository at this point in the history
Co-authored-by: Daniel Dyla <dyladan@users.noreply.github.com>
  • Loading branch information
anthony-telljohann and dyladan authored Oct 14, 2022
1 parent 07a16b7 commit a7d053a
Show file tree
Hide file tree
Showing 3 changed files with 10 additions and 7 deletions.
3 changes: 3 additions & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -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)
Expand Down
4 changes: 2 additions & 2 deletions packages/opentelemetry-resources/src/detectors/EnvDetector.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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}`);
}
Expand All @@ -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;
}
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -14,7 +14,6 @@
* limitations under the License.
*/

import { SemanticResourceAttributes } from '@opentelemetry/semantic-conventions';
import { envDetector, Resource } from '../../../src';
import {
assertK8sResource,
Expand All @@ -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(() => {
Expand All @@ -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'
});
});
});
Expand Down

0 comments on commit a7d053a

Please sign in to comment.