Skip to content

Commit

Permalink
chore: upgrade for opentelemtry js v0.12.0
Browse files Browse the repository at this point in the history
  • Loading branch information
aabmass committed Oct 28, 2020
1 parent 48898d5 commit 65069d5
Show file tree
Hide file tree
Showing 15 changed files with 435 additions and 457 deletions.
46 changes: 23 additions & 23 deletions packages/opentelemetry-cloud-monitoring-exporter/package-lock.json

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

16 changes: 8 additions & 8 deletions packages/opentelemetry-cloud-monitoring-exporter/package.json
Original file line number Diff line number Diff line change
Expand Up @@ -37,10 +37,10 @@
"registry": "https://wombat-dressing-room.appspot.com"
},
"devDependencies": {
"@opentelemetry/api": "^0.11.0",
"@opentelemetry/core": "^0.11.0",
"@opentelemetry/metrics": "^0.11.0",
"@opentelemetry/resources": "^0.11.0",
"@opentelemetry/api": "^0.12.0",
"@opentelemetry/core": "^0.12.0",
"@opentelemetry/metrics": "^0.12.0",
"@opentelemetry/resources": "^0.12.0",
"@types/mocha": "7.0.2",
"@types/nock": "11.1.0",
"@types/node": "12.12.51",
Expand All @@ -60,9 +60,9 @@
"googleapis": "^46.0.0"
},
"peerDependencies": {
"@opentelemetry/api": "^0.11.0",
"@opentelemetry/core": "^0.11.0",
"@opentelemetry/metrics": "^0.11.0",
"@opentelemetry/resources": "^0.11.0"
"@opentelemetry/api": "^0.12.0",
"@opentelemetry/core": "^0.12.0",
"@opentelemetry/metrics": "^0.12.0",
"@opentelemetry/resources": "^0.12.0"
}
}
46 changes: 23 additions & 23 deletions packages/opentelemetry-cloud-monitoring-exporter/src/monitoring.ts
Original file line number Diff line number Diff line change
Expand Up @@ -16,26 +16,26 @@ import {
MetricExporter as IMetricExporter,
MetricRecord,
MetricDescriptor as OTMetricDescriptor,
} from '@opentelemetry/metrics';
import { ExportResult, NoopLogger, VERSION } from '@opentelemetry/core';
import { Logger } from '@opentelemetry/api';
import { ExporterOptions } from './external-types';
import { GoogleAuth, JWT } from 'google-auth-library';
import { google } from 'googleapis';
import { transformMetricDescriptor, createTimeSeries } from './transform';
import { TimeSeries } from './types';
import { partitionList } from './utils';
} from "@opentelemetry/metrics";
import { ExportResult, NoopLogger, VERSION } from "@opentelemetry/core";
import { Logger } from "@opentelemetry/api";
import { ExporterOptions } from "./external-types";
import { GoogleAuth, JWT } from "google-auth-library";
import { google } from "googleapis";
import { transformMetricDescriptor, createTimeSeries } from "./transform";
import { TimeSeries } from "./types";
import { partitionList } from "./utils";

// Stackdriver Monitoring v3 only accepts up to 200 TimeSeries per
// CreateTimeSeries call.
const MAX_BATCH_EXPORT_SIZE = 200;

const OT_USER_AGENT = {
product: 'opentelemetry-js',
product: "opentelemetry-js",
version: VERSION,
};
const OT_REQUEST_HEADER = {
'x-opentelemetry-outgoing-request': 0x1,
"x-opentelemetry-outgoing-request": 0x1,
};
google.options({ headers: OT_REQUEST_HEADER });

Expand All @@ -50,16 +50,16 @@ export class MetricExporter implements IMetricExporter {
private readonly _auth: GoogleAuth;
private readonly _startTime = new Date().toISOString();

static readonly DEFAULT_DISPLAY_NAME_PREFIX: string = 'OpenTelemetry';
static readonly DEFAULT_DISPLAY_NAME_PREFIX: string = "OpenTelemetry";
static readonly CUSTOM_OPENTELEMETRY_DOMAIN: string =
'custom.googleapis.com/opentelemetry';
"custom.googleapis.com/opentelemetry";

private registeredMetricDescriptors: Map<
string,
OTMetricDescriptor
> = new Map();

private static readonly _monitoring = google.monitoring('v3');
private static readonly _monitoring = google.monitoring("v3");

constructor(options: ExporterOptions = {}) {
this._logger = options.logger || new NoopLogger();
Expand All @@ -73,12 +73,12 @@ export class MetricExporter implements IMetricExporter {
keyFile: options.keyFile,
keyFilename: options.keyFilename,
projectId: options.projectId,
scopes: ['https://www.googleapis.com/auth/cloud-platform'],
scopes: ["https://www.googleapis.com/auth/cloud-platform"],
});

// Start this async process as early as possible. It will be
// awaited on the first export because constructors are synchronous
this._projectId = this._auth.getProjectId().catch(err => {
this._projectId = this._auth.getProjectId().catch((err) => {
this._logger.error(err);
});
}
Expand All @@ -99,11 +99,11 @@ export class MetricExporter implements IMetricExporter {
}

if (!this._projectId) {
this._logger.error('expecting a non-blank ProjectID');
this._logger.error("expecting a non-blank ProjectID");
return cb(ExportResult.FAILED_NOT_RETRYABLE);
}

this._logger.debug('Google Cloud Monitoring export');
this._logger.debug("Google Cloud Monitoring export");
const timeSeries: TimeSeries[] = [];
for (const metric of metrics) {
const isRegistered = await this._registerMetricDescriptor(
Expand Down Expand Up @@ -140,7 +140,7 @@ export class MetricExporter implements IMetricExporter {
cb(ExportResult.SUCCESS);
}

shutdown(): void {}
async shutdown(): Promise<void> {}

/**
* Returns true if the given metricDescriptor is successfully registered to
Expand Down Expand Up @@ -174,7 +174,7 @@ export class MetricExporter implements IMetricExporter {
);
return true;
})
.catch(err => {
.catch((err) => {
this._logger.error(err);
return false;
});
Expand Down Expand Up @@ -202,7 +202,7 @@ export class MetricExporter implements IMetricExporter {
request,
{ headers: OT_REQUEST_HEADER, userAgentDirectives: [OT_USER_AGENT] },
(err: Error | null) => {
this._logger.debug('sent metric descriptor', request.resource);
this._logger.debug("sent metric descriptor", request.resource);
err ? reject(err) : resolve();
}
);
Expand All @@ -219,7 +219,7 @@ export class MetricExporter implements IMetricExporter {
return Promise.resolve();
}

return this._authorize().then(authClient => {
return this._authorize().then((authClient) => {
const request = {
name: `projects/${this._projectId}`,
resource: { timeSeries },
Expand All @@ -231,7 +231,7 @@ export class MetricExporter implements IMetricExporter {
request,
{ headers: OT_REQUEST_HEADER, userAgentDirectives: [OT_USER_AGENT] },
(err: Error | null) => {
this._logger.debug('sent time series', request.resource.timeSeries);
this._logger.debug("sent time series", request.resource.timeSeries);
err ? reject(err) : resolve();
}
);
Expand Down
54 changes: 20 additions & 34 deletions packages/opentelemetry-cloud-monitoring-exporter/src/transform.ts
Original file line number Diff line number Diff line change
Expand Up @@ -16,33 +16,32 @@ import {
MetricDescriptor as OTMetricDescriptor,
MetricKind as OTMetricKind,
MetricRecord,
Distribution as OTDistribution,
Histogram as OTHistogram,
Point as OTPoint,
PointValueType,
} from '@opentelemetry/metrics';
import { ValueType as OTValueType } from '@opentelemetry/api';
} from "@opentelemetry/metrics";
import { ValueType as OTValueType } from "@opentelemetry/api";
import {
Resource,
CLOUD_RESOURCE,
HOST_RESOURCE,
} from '@opentelemetry/resources';
} from "@opentelemetry/resources";
import {
MetricDescriptor,
MetricKind,
ValueType,
TimeSeries,
Point,
MonitoredResource,
} from './types';
import * as path from 'path';
import * as os from 'os';
} from "./types";
import * as path from "path";
import * as os from "os";

const OPENTELEMETRY_TASK = 'opentelemetry_task';
const OPENTELEMETRY_TASK_DESCRIPTION = 'OpenTelemetry task identifier';
const AWS_REGION_VALUE_PREFIX = 'aws:';
const GCP_GCE_INSTANCE = 'gce_instance';
const AWS_EC2_INSTANCE = 'aws_ec2_instance';
const OPENTELEMETRY_TASK = "opentelemetry_task";
const OPENTELEMETRY_TASK_DESCRIPTION = "OpenTelemetry task identifier";
const AWS_REGION_VALUE_PREFIX = "aws:";
const GCP_GCE_INSTANCE = "gce_instance";
const AWS_EC2_INSTANCE = "aws_ec2_instance";
export const OPENTELEMETRY_TASK_VALUE_DEFAULT = generateDefaultTaskValue();

export function transformMetricDescriptor(
Expand Down Expand Up @@ -143,7 +142,7 @@ function transformResource(
// Checks the resource's value for a required key
const resourceValue = resource.attributes[templateResource.labels[key]];
if (!resourceValue) {
return { type: 'global', labels: { project_id: projectId } };
return { type: "global", labels: { project_id: projectId } };
}
if (
type === AWS_EC2_INSTANCE &&
Expand All @@ -164,15 +163,15 @@ function transformResource(
*/
function getTypeAndMappings(resource: Resource): MonitoredResource {
const cloudProvider = `${resource.attributes[CLOUD_RESOURCE.PROVIDER]}`;
if (cloudProvider === 'gcp') {
if (cloudProvider === "gcp") {
return {
type: GCP_GCE_INSTANCE,
labels: {
instance_id: HOST_RESOURCE.ID,
zone: CLOUD_RESOURCE.ZONE,
},
};
} else if (cloudProvider === 'aws') {
} else if (cloudProvider === "aws") {
return {
type: AWS_EC2_INSTANCE,
labels: {
Expand All @@ -182,7 +181,7 @@ function getTypeAndMappings(resource: Resource): MonitoredResource {
},
};
}
return { type: 'global', labels: {} };
return { type: "global", labels: {} };
}

function transformMetric(
Expand All @@ -193,7 +192,7 @@ function transformMetric(
const labels: { [key: string]: string } = {};

Object.keys(metric.labels).forEach(
key => (labels[key] = `${metric.labels[key]}`)
(key) => (labels[key] = `${metric.labels[key]}`)
);
labels[OPENTELEMETRY_TASK] = OPENTELEMETRY_TASK_VALUE_DEFAULT;
return { type, labels };
Expand Down Expand Up @@ -232,10 +231,6 @@ function transformPoint(

/** Transforms a OpenTelemetry Point's value to a StackDriver Point value. */
function transformValue(valueType: OTValueType, value: PointValueType) {
if (isDistributionValue(value)) {
throw Error('unsupported distribution value type');
// no buckets aggregated, which is a required param in `distributionValue` for Cloud Monitoring v3
}
if (isHistogramValue(value)) {
return {
distributionValue: {
Expand All @@ -258,25 +253,16 @@ function transformValue(valueType: OTValueType, value: PointValueType) {
throw Error(`unsupported value type: ${valueType}`);
}

/** Returns true if value is of type OTDistribution */
function isDistributionValue(
value: number | OTDistribution | OTHistogram
): value is OTDistribution {
return Object.prototype.hasOwnProperty.call(value, 'min');
}

/** Returns true if value is of type OTHistogram */
function isHistogramValue(
value: number | OTDistribution | OTHistogram
): value is OTHistogram {
return Object.prototype.hasOwnProperty.call(value, 'buckets');
function isHistogramValue(value: PointValueType): value is OTHistogram {
return Object.prototype.hasOwnProperty.call(value, "buckets");
}

/** Returns a task label value in the format of 'nodejs-<pid>@<hostname>'. */
function generateDefaultTaskValue(): string {
const pid = process.pid;
const hostname = os.hostname() || 'localhost';
return 'nodejs-' + pid + '@' + hostname;
const hostname = os.hostname() || "localhost";
return "nodejs-" + pid + "@" + hostname;
}

export const TEST_ONLY = {
Expand Down
Loading

0 comments on commit 65069d5

Please sign in to comment.