-
Notifications
You must be signed in to change notification settings - Fork 837
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: resource auto-detection #899
Merged
Merged
Changes from 1 commit
Commits
Show all changes
21 commits
Select commit
Hold shift + click to select a range
7d569da
feat: add resource detection from environment variable
mwear 8190a38
feat: add AWS EC2 resource detection
mwear 8c61b50
feat: add resource detection for GCP
mwear 2842f8e
refactor: create Labels interface
mwear 6325f1f
feat: add Resource.detect() method
mwear d0007e7
refactor: improve GcpDetector
mwear 9d54036
feat: make detection platform aware; extract to detectResources() fn
mwear 4ea66ec
chore: cleanup
mwear 2eb7aa2
chore: prefix private methods with _
mwear c466a45
fix: import URL for Node 8
mwear 98a41fe
chore: prefix private constants with _
mwear e5d4ff1
chore: do not export resources
mwear 568d72a
chore: abort request on timeout
mwear d403261
refactor: export instances of detectors instead of static classes
mwear 426ed85
refactor: formalize Detector interface
mwear 5d5b343
refactor: rename Labels -> ResourceLabels
mwear dce3718
feat: gracefully handle detectors that throw
mwear b7f0592
fix: do not resume in an end event
mwear 7df23d5
docs: document interfaces and idenitity document url
mwear 7a3ed94
fix: nock flakiness in resource tests
mwear 689ffd3
Merge branch 'master' into resource_autodetection
mayurkale22 File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
feat: make detection platform aware; extract to detectResources() fn
- Loading branch information
commit 9d540360f3a7b580bd173ad315bc374ead33fe09
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
26 changes: 26 additions & 0 deletions
26
packages/opentelemetry-resources/src/platform/browser/detect-resources.ts
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,26 @@ | ||
/** | ||
* Copyright 2020, 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'; | ||
|
||
/** | ||
* Detects resources for the browser platform, which is currently only the | ||
* telemetry SDK resource. More could be added in the future. This method | ||
* is async to match the signature of corresponding method for node. | ||
*/ | ||
export const detectResources = async (): Promise<Resource> => { | ||
return Resource.createTelemetrySDKResource(); | ||
}; |
17 changes: 17 additions & 0 deletions
17
packages/opentelemetry-resources/src/platform/browser/index.ts
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,17 @@ | ||
/*! | ||
* Copyright 2020, 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 './detect-resources'; |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,20 @@ | ||
/*! | ||
* Copyright 2020, 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. | ||
*/ | ||
|
||
// Use the node platform by default. The "browser" field of package.json is used | ||
// to override this file to use `./browser/index.ts` when packaged with | ||
// webpack, Rollup, etc. | ||
export * from './node'; |
34 changes: 34 additions & 0 deletions
34
packages/opentelemetry-resources/src/platform/node/detect-resources.ts
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,34 @@ | ||
/** | ||
* Copyright 2020, 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 { EnvDetector, AwsEc2Detector, GcpDetector } from './detectors'; | ||
|
||
const DETECTORS = [EnvDetector, AwsEc2Detector, GcpDetector]; | ||
|
||
/** | ||
* Runs all resource detectors and returns the results merged into a single | ||
* Resource. | ||
*/ | ||
export const detectResources = async (): Promise<Resource> => { | ||
const resources: Array<Resource> = await Promise.all( | ||
DETECTORS.map(d => d.detect()) | ||
); | ||
return resources.reduce( | ||
(acc, resource) => acc.merge(resource), | ||
Resource.createTelemetrySDKResource() | ||
); | ||
}; |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
File renamed without changes.
18 changes: 18 additions & 0 deletions
18
packages/opentelemetry-resources/src/platform/node/index.ts
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,18 @@ | ||
/*! | ||
* Copyright 2020, 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 './detect-resources'; | ||
export * from './detectors'; | ||
mwear marked this conversation as resolved.
Show resolved
Hide resolved
|
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
139 changes: 139 additions & 0 deletions
139
packages/opentelemetry-resources/test/detect-resources.test.ts
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,139 @@ | ||
/*! | ||
* Copyright 2020, 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 nock from 'nock'; | ||
import { Resource } from '../src'; | ||
import { detectResources, AwsEc2Detector } from '../src'; | ||
import { | ||
assertServiceResource, | ||
assertCloudResource, | ||
assertHostResource, | ||
} from './util/resource-assertions'; | ||
import { | ||
BASE_PATH, | ||
HEADER_NAME, | ||
HEADER_VALUE, | ||
HOST_ADDRESS, | ||
SECONDARY_HOST_ADDRESS, | ||
resetIsAvailableCache, | ||
} from 'gcp-metadata'; | ||
|
||
const HEADERS = { | ||
[HEADER_NAME.toLowerCase()]: HEADER_VALUE, | ||
}; | ||
const INSTANCE_PATH = BASE_PATH + '/instance'; | ||
const INSTANCE_ID_PATH = BASE_PATH + '/instance/id'; | ||
const PROJECT_ID_PATH = BASE_PATH + '/project/project-id'; | ||
const ZONE_PATH = BASE_PATH + '/instance/zone'; | ||
const CLUSTER_NAME_PATH = BASE_PATH + '/instance/attributes/cluster-name'; | ||
|
||
const { origin: AWS_HOST, pathname: AWS_PATH } = new URL( | ||
AwsEc2Detector.AWS_INSTANCE_IDENTITY_DOCUMENT_URI | ||
); | ||
|
||
const mockedAwsResponse = { | ||
instanceId: 'my-instance-id', | ||
accountId: 'my-account-id', | ||
region: 'my-region', | ||
}; | ||
|
||
describe('detectResource', async () => { | ||
before(() => { | ||
nock.disableNetConnect(); | ||
process.env.OTEL_RESOURCE_LABELS = | ||
'service.instance.id=627cc493,service.name=my-service,service.namespace=default,service.version=0.0.1'; | ||
}); | ||
|
||
after(() => { | ||
nock.cleanAll(); | ||
nock.enableNetConnect(); | ||
delete process.env.OTEL_RESOURCE_LABELS; | ||
}); | ||
|
||
describe('in GCP environment', () => { | ||
after(() => { | ||
resetIsAvailableCache(); | ||
}); | ||
|
||
it('returns a merged resource', async () => { | ||
const scope = nock(HOST_ADDRESS) | ||
.get(INSTANCE_PATH) | ||
.reply(200, {}, HEADERS) | ||
.get(INSTANCE_ID_PATH) | ||
.reply(200, () => 452003179927758, HEADERS) | ||
.get(PROJECT_ID_PATH) | ||
.reply(200, () => 'my-project-id', HEADERS) | ||
.get(ZONE_PATH) | ||
.reply(200, () => 'project/zone/my-zone', HEADERS) | ||
.get(CLUSTER_NAME_PATH) | ||
.reply(404); | ||
const secondaryScope = nock(SECONDARY_HOST_ADDRESS) | ||
.get(INSTANCE_PATH) | ||
.reply(200, {}, HEADERS); | ||
const resource: Resource = await detectResources(); | ||
secondaryScope.done(); | ||
scope.done(); | ||
|
||
assertCloudResource(resource, { | ||
provider: 'gcp', | ||
accountId: 'my-project-id', | ||
zone: 'my-zone', | ||
}); | ||
assertHostResource(resource, { id: '452003179927758' }); | ||
assertServiceResource(resource, { | ||
instanceId: '627cc493', | ||
name: 'my-service', | ||
namespace: 'default', | ||
version: '0.0.1', | ||
}); | ||
}); | ||
}); | ||
|
||
describe('in AWS environment', () => { | ||
it('returns a merged resource', async () => { | ||
const gcpScope = nock(HOST_ADDRESS) | ||
.get(INSTANCE_PATH) | ||
.replyWithError({ | ||
code: 'ENOTFOUND', | ||
}); | ||
const gcpSecondaryScope = nock(SECONDARY_HOST_ADDRESS) | ||
.get(INSTANCE_PATH) | ||
.replyWithError({ | ||
code: 'ENOTFOUND', | ||
}); | ||
const awsScope = nock(AWS_HOST) | ||
.get(AWS_PATH) | ||
.reply(200, () => mockedAwsResponse); | ||
const resource: Resource = await detectResources(); | ||
gcpSecondaryScope.done(); | ||
gcpScope.done(); | ||
awsScope.done(); | ||
|
||
assertCloudResource(resource, { | ||
provider: 'aws', | ||
accountId: 'my-account-id', | ||
region: 'my-region', | ||
}); | ||
assertHostResource(resource, { id: 'my-instance-id' }); | ||
assertServiceResource(resource, { | ||
instanceId: '627cc493', | ||
name: 'my-service', | ||
namespace: 'default', | ||
version: '0.0.1', | ||
}); | ||
}); | ||
}); | ||
}); |
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
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.
What happens if a detector throws?
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.
I added a try / catch.