Skip to content

Commit b36ab12

Browse files
authored
fix(instrumentation): do not import 'path' in browser runtimes (#4386)
* fix(instrumentation): do not import 'path' in browser runtimes * fix(changelog): clean up and add entry * fix(instrumentation): add missing license header * fix(changelog): formatting * fix(instrumentation): do not throw
1 parent 0206181 commit b36ab12

File tree

8 files changed

+83
-3
lines changed

8 files changed

+83
-3
lines changed

experimental/CHANGELOG.md

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -10,7 +10,9 @@ All notable changes to experimental packages in this project will be documented
1010

1111
### :bug: (Bug Fix)
1212

13-
fix(instrumentation): use caret range on import-in-the-middle [#4380](https://github.com/open-telemetry/opentelemetry-js/pull/4380) @pichlermarc
13+
* fix(instrumentation): use caret range on import-in-the-middle [#4380](https://github.com/open-telemetry/opentelemetry-js/pull/4380) @pichlermarc
14+
* fix(instrumentation): do not import 'path' in browser runtimes [#4378](https://github.com/open-telemetry/opentelemetry-js/pull/4378) @pichlermarc
15+
* Fixes a bug where bundling for web would fail due to `InstrumentationNodeModuleDefinition` importing `path`
1416

1517
### :books: (Refine Doc)
1618

experimental/packages/opentelemetry-instrumentation/src/instrumentationNodeModuleFile.ts

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -15,7 +15,7 @@
1515
*/
1616

1717
import { InstrumentationModuleFile } from './types';
18-
import { normalize } from 'path';
18+
import { normalize } from './platform/index';
1919

2020
export class InstrumentationNodeModuleFile<T>
2121
implements InstrumentationModuleFile<T>

experimental/packages/opentelemetry-instrumentation/src/platform/browser/index.ts

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -15,3 +15,4 @@
1515
*/
1616

1717
export { InstrumentationBase } from './instrumentation';
18+
export { normalize } from './noop-normalize';
Lines changed: 35 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,35 @@
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 { diag } from '@opentelemetry/api';
18+
19+
/**
20+
* Placeholder normalize function to replace the node variant in browser runtimes,
21+
* this should never be called and will perform a no-op and warn if it is called regardless.
22+
*
23+
* This is a workaround to fix https://github.com/open-telemetry/opentelemetry-js/issues/4373 until the instrumentation
24+
* package can be made node-only.
25+
*
26+
* @param path input path
27+
* @return unmodified path
28+
* @internal
29+
*/
30+
export function normalize(path: string): string {
31+
diag.warn(
32+
'Path normalization is not implemented for this platform. To silence this warning, ensure no node-specific instrumentations are loaded, and node-specific types (e.g. InstrumentationNodeModuleFile), are not used in a browser context)'
33+
);
34+
return path;
35+
}

experimental/packages/opentelemetry-instrumentation/src/platform/index.ts

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

17-
export { InstrumentationBase } from './node';
17+
export { InstrumentationBase, normalize } from './node';

experimental/packages/opentelemetry-instrumentation/src/platform/node/index.ts

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -14,3 +14,4 @@
1414
* limitations under the License.
1515
*/
1616
export { InstrumentationBase } from './instrumentation';
17+
export { normalize } from './normalize';
Lines changed: 17 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,17 @@
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+
export { normalize } from 'path';
Lines changed: 24 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,24 @@
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 { normalize } from '../../src/platform/browser';
19+
20+
describe('noop-normalize', function () {
21+
it('should not normalize input', function () {
22+
assert.strictEqual(normalize('/tmp/foo/../bar'), '/tmp/foo/../bar');
23+
});
24+
});

0 commit comments

Comments
 (0)