-
Notifications
You must be signed in to change notification settings - Fork 804
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
feat(opentelemetry-resources): add runtime version information #2727
Changes from 12 commits
7ce4389
7bd392f
8607046
1827b95
304edad
abcf4a0
ee76a6c
d25b453
3d36a7c
caea930
b8e73c6
d78d714
478cab0
1f1ca09
c749614
508fce0
abb3d3a
09236f6
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,61 @@ | ||
/* | ||
* Copyright The OpenTelemetry Authors | ||
* | ||
* Licensed under the Apache License, Version 2.0 (the "License"); | ||
* you may not use this file except in compliance with the License. | ||
* You may obtain a copy of the License at | ||
* | ||
* https://www.apache.org/licenses/LICENSE-2.0 | ||
* | ||
* Unless required by applicable law or agreed to in writing, software | ||
* distributed under the License is distributed on an "AS IS" BASIS, | ||
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. | ||
* See the License for the specific language governing permissions and | ||
* limitations under the License. | ||
*/ | ||
|
||
import { diag } from '@opentelemetry/api'; | ||
import { SemanticResourceAttributes } from '@opentelemetry/semantic-conventions'; | ||
import { Detector, Resource, ResourceDetectionConfig } from '../../..'; | ||
import { ResourceAttributes } from '../../../types'; | ||
|
||
/** | ||
* BrowserDetector will be used to detect the resources related to browser. | ||
*/ | ||
class BrowserDetector implements Detector { | ||
async detect(config?: ResourceDetectionConfig): Promise<Resource> { | ||
const browserResource: ResourceAttributes = { | ||
[SemanticResourceAttributes.PROCESS_RUNTIME_NAME]: 'browser', | ||
legendecas marked this conversation as resolved.
Show resolved
Hide resolved
|
||
[SemanticResourceAttributes.PROCESS_RUNTIME_DESCRIPTION]: 'Web Browser', | ||
[SemanticResourceAttributes.PROCESS_RUNTIME_VERSION]: window.navigator.userAgent | ||
legendecas marked this conversation as resolved.
Show resolved
Hide resolved
|
||
}; | ||
return this._getResourceAttributes(browserResource, config); | ||
} | ||
/** | ||
* Validates process resource attribute map from process variables | ||
* | ||
* @param browserResource The un-sanitized resource attributes from process as key/value pairs. | ||
* @param config: Config | ||
* @returns The sanitized resource attributes. | ||
*/ | ||
private _getResourceAttributes( | ||
browserResource: ResourceAttributes, | ||
_config?: ResourceDetectionConfig | ||
) { | ||
if ( | ||
browserResource[SemanticResourceAttributes.PROCESS_RUNTIME_VERSION] === '' | ||
) { | ||
diag.debug( | ||
'BrowserDetector failed: Unable to find required browser resources. ' | ||
); | ||
return Resource.empty(); | ||
} else { | ||
return new Resource({ | ||
...browserResource, | ||
}); | ||
} | ||
} | ||
} | ||
|
||
|
||
export const browserDetector = new BrowserDetector(); |
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,17 @@ | ||
/* | ||
* Copyright The OpenTelemetry Authors | ||
* | ||
* Licensed under the Apache License, Version 2.0 (the "License"); | ||
* you may not use this file except in compliance with the License. | ||
* You may obtain a copy of the License at | ||
* | ||
* https://www.apache.org/licenses/LICENSE-2.0 | ||
* | ||
* Unless required by applicable law or agreed to in writing, software | ||
* distributed under the License is distributed on an "AS IS" BASIS, | ||
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. | ||
* See the License for the specific language governing permissions and | ||
* limitations under the License. | ||
*/ | ||
|
||
export * from './BrowserDetector'; |
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,73 @@ | ||
/* | ||
* Copyright The OpenTelemetry Authors | ||
* | ||
* Licensed under the Apache License, Version 2.0 (the "License"); | ||
* you may not use this file except in compliance with the License. | ||
* You may obtain a copy of the License at | ||
* | ||
* https://www.apache.org/licenses/LICENSE-2.0 | ||
* | ||
* Unless required by applicable law or agreed to in writing, software | ||
* distributed under the License is distributed on an "AS IS" BASIS, | ||
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. | ||
* See the License for the specific language governing permissions and | ||
* limitations under the License. | ||
*/ | ||
|
||
import { Resource } from '../Resource'; | ||
import { ResourceDetectionConfig } from '../config'; | ||
import { diag } from '@opentelemetry/api'; | ||
import * as util from 'util'; | ||
|
||
/** | ||
* Runs all resource detectors and returns the results merged into a single | ||
* Resource. | ||
* | ||
* @param config Configuration for resource detection | ||
*/ | ||
export const detectResources = async ( | ||
config: ResourceDetectionConfig = {} | ||
): Promise<Resource> => { | ||
const internalConfig: ResourceDetectionConfig = Object.assign(config); | ||
|
||
const resources: Resource[] = await Promise.all( | ||
(internalConfig.detectors || []).map(async d => { | ||
try { | ||
const resource = await d.detect(internalConfig); | ||
diag.debug(`${d.constructor.name} found resource.`, resource); | ||
return resource; | ||
} catch (e) { | ||
diag.debug(`${d.constructor.name} failed: ${e.message}`); | ||
return Resource.empty(); | ||
} | ||
}) | ||
); | ||
|
||
// Future check if verbose logging is enabled issue #1903 | ||
logResources(resources); | ||
|
||
return resources.reduce( | ||
(acc, resource) => acc.merge(resource), | ||
Resource.empty() | ||
); | ||
}; | ||
|
||
/** | ||
* Writes debug information about the detected resources to the logger defined in the resource detection config, if one is provided. | ||
* | ||
* @param resources The array of {@link Resource} that should be logged. Empty entries will be ignored. | ||
*/ | ||
const logResources = (resources: Array<Resource>) => { | ||
resources.forEach(resource => { | ||
// Print only populated resources | ||
if (Object.keys(resource.attributes).length > 0) { | ||
const resourceDebugString = util.inspect(resource.attributes, { | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more.
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. i will file an issue for this and work on it in another pr. thanks! |
||
depth: 2, | ||
breakLength: Infinity, | ||
sorted: true, | ||
compact: false, | ||
}); | ||
diag.verbose(resourceDebugString); | ||
} | ||
}); | ||
}; |
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,58 @@ | ||
/* | ||
* Copyright The OpenTelemetry Authors | ||
* | ||
* Licensed under the Apache License, Version 2.0 (the "License"); | ||
* you may not use this file except in compliance with the License. | ||
* You may obtain a copy of the License at | ||
* | ||
* https://www.apache.org/licenses/LICENSE-2.0 | ||
* | ||
* Unless required by applicable law or agreed to in writing, software | ||
* distributed under the License is distributed on an "AS IS" BASIS, | ||
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. | ||
* See the License for the specific language governing permissions and | ||
* limitations under the License. | ||
*/ | ||
import * as sinon from 'sinon'; | ||
import { Resource } from '../../src'; | ||
import { browserDetector } from '../../src/platform/browser/detectors'; | ||
import { | ||
assertResource, | ||
assertEmptyResource, | ||
} from '../util/resource-assertions'; | ||
|
||
|
||
describe('browserDetector()', () => { | ||
beforeEach(() => { | ||
(globalThis.window as {}) = {}; | ||
sinon.stub(globalThis, 'window').value({ | ||
navigator: { | ||
userAgent: '', | ||
} | ||
}); | ||
}); | ||
|
||
afterEach(() => { | ||
sinon.restore(); | ||
}); | ||
|
||
it('should return browser information', async () => { | ||
sinon.stub(globalThis, 'window').value({ | ||
navigator: { | ||
userAgent: 'dddd', | ||
} | ||
}); | ||
|
||
const resource: Resource = await browserDetector.detect(); | ||
assertResource(resource, { | ||
version: 'dddd', | ||
runtimeDescription: 'Web Browser', | ||
runtimeName: 'browser', | ||
}); | ||
}); | ||
it('should return empty resources if version is missing', async () => { | ||
const resource: Resource = await browserDetector.detect(); | ||
assertEmptyResource(resource); | ||
}); | ||
}); | ||
|
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
The detectors were moved to the
src/detectors
folder so that we can have a consistent export on both Node.js and Web environments. Anddetector.detect
will do nothing if the environment is not the expected one, like https://github.com/open-telemetry/opentelemetry-js/blob/main/packages/opentelemetry-resources/src/detectors/ProcessDetector.ts#L30.Could you move the BrowserDetector to
src/detectors
too so that it can be exported unconditionally?