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

Adds monitored resource to [core] #173

Closed
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
125 changes: 115 additions & 10 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,105 @@
/**
Copy link
Member

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.

Copy link
Contributor Author

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

* 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;
}
}
Loading