This repository has been archived by the owner on Oct 3, 2023. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 96
Adds monitored resource to [core] #173
Closed
isaikevych
wants to merge
3
commits into
census-instrumentation:master
from
isaikevych:autopopulate_k8s_container
Closed
Changes from all commits
Commits
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
There are no files selected for viewing
Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.
Oops, something went wrong.
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
105 changes: 105 additions & 0 deletions
105
packages/opencensus-core/src/common/monitored-resource/aws-identity-document-utils.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,105 @@ | ||
/** | ||
* Copyright 2018, OpenCensus 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 | ||
* | ||
* http://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 {get} from 'http'; | ||
import * as logger from '../../common/console-logger'; | ||
import * as loggerTypes from '../../common/types'; | ||
import {monitoredResourceAttributes} from './types'; | ||
|
||
/** Util methods for getting and parsing AWS instance identity document. */ | ||
export class AwsIdentityDocumentUtils { | ||
/** Aws instance identity URL */ | ||
static HOST = '169.254.169.254'; | ||
static readonly PATH = '/latest/dynamic/instance-identity/document'; | ||
static readonly PORT = 80; | ||
static runned = false; | ||
static metadata: Record<string, string> = {}; | ||
static promise: Promise<Record<string, string>>; | ||
static readonly logger: loggerTypes.Logger = logger.logger(); | ||
|
||
/** | ||
* Establishes an HTTP connection to AWS instance identity document url. | ||
* If the application is running on an EC2 instance, we should be able | ||
* to get back a valid JSON document. Parses that document and stores | ||
* the identity properties in a local map. | ||
*/ | ||
static run() { | ||
if (AwsIdentityDocumentUtils.runned) { | ||
return AwsIdentityDocumentUtils.promise; | ||
} | ||
AwsIdentityDocumentUtils.promise = | ||
AwsIdentityDocumentUtils.getDocument().then(metadata => { | ||
const data: Record<string, string> = {}; | ||
Object.keys(monitoredResourceAttributes.AWS).forEach(key => { | ||
data[key] = metadata[key]; | ||
}); | ||
if (Object.keys(data).length) { | ||
AwsIdentityDocumentUtils.metadata = data; | ||
AwsIdentityDocumentUtils.runned = true; | ||
} | ||
return metadata; | ||
}); | ||
return AwsIdentityDocumentUtils.promise; | ||
} | ||
|
||
/** | ||
* Fetches the requested instance metadata entry. | ||
* @param name Attribute name relative to the computeMetadata/v1 prefix | ||
*/ | ||
private static getDocument() { | ||
const promise = new Promise((resolve, reject) => { | ||
get({ | ||
host: AwsIdentityDocumentUtils.HOST, | ||
path: AwsIdentityDocumentUtils.PATH, | ||
port: AwsIdentityDocumentUtils.PORT | ||
}, | ||
(response) => { | ||
let body = ''; | ||
response.on('data', chunk => body += chunk); | ||
response.on('end', () => { | ||
if (response.statusCode >= 200 && response.statusCode < 300) { | ||
try { | ||
const data = JSON.parse(body); | ||
resolve(data); | ||
} catch (e) { | ||
response.resume(); | ||
} | ||
} else { | ||
const errorMessage = | ||
`Request Failed. Status code: ${response.statusCode}`; | ||
AwsIdentityDocumentUtils.logger.error(errorMessage); | ||
reject(errorMessage); | ||
response.resume(); | ||
} | ||
}); | ||
}); | ||
}); | ||
return promise.catch(e => e); | ||
} | ||
|
||
/** | ||
* AWS Instance Identity Document is a JSON file. | ||
* See | ||
* docs.aws.amazon.com/AWSEC2/latest/UserGuide/instance-identity-documents.html. | ||
*/ | ||
getMetadata() { | ||
return AwsIdentityDocumentUtils.metadata; | ||
} | ||
|
||
static isRunning() { | ||
return AwsIdentityDocumentUtils.runned; | ||
} | ||
} |
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.
Please change "monitored-resource" => "resource-util" and this shouldn't be part of core package.
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.
This new requirements blocked by Resource API