Skip to content
This repository has been archived by the owner on Oct 3, 2023. It is now read-only.

Commit

Permalink
Adds monitored resource to [core]
Browse files Browse the repository at this point in the history
  • Loading branch information
isaikevych committed Nov 1, 2018
1 parent 006d3db commit f3e812c
Show file tree
Hide file tree
Showing 9 changed files with 704 additions and 12 deletions.
142 changes: 131 additions & 11 deletions packages/opencensus-core/package-lock.json

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

4 changes: 3 additions & 1 deletion packages/opencensus-core/package.json
Original file line number Diff line number Diff line change
Expand Up @@ -40,7 +40,8 @@
"devDependencies": {
"@types/continuation-local-storage": "^3.2.1",
"@types/mocha": "^2.2.48",
"@types/node": "^9.4.7",
"@types/nock": "^9.3.0",
"@types/node": "^9.6.35",
"@types/once": "^1.4.0",
"@types/semver": "^5.5.0",
"@types/shimmer": "^1.0.1",
Expand All @@ -49,6 +50,7 @@
"intercept-stdout": "^0.1.2",
"mocha": "^5.0.4",
"ncp": "^2.0.0",
"nock": "^10.0.1",
"nyc": "11.6.0",
"ts-node": "^4.0.0",
"typescript": "~2.6.1"
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,100 @@
/**
* 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';

/** 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 => {
if (Object.keys(metadata).length) {
AwsIdentityDocumentUtils.metadata = metadata;
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;
}
}
Loading

0 comments on commit f3e812c

Please sign in to comment.