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

Commit c10212f

Browse files
authored
add common utils (#321)
1 parent f98ec08 commit c10212f

File tree

6 files changed

+86
-70
lines changed

6 files changed

+86
-70
lines changed
Lines changed: 79 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,79 @@
1+
/**
2+
* Copyright 2019, OpenCensus Authors
3+
*
4+
* Licensed under the Apache License, Version 2.0 (the "License");
5+
* you may not use this file except in compliance with the License.
6+
* You may obtain a copy of the License at
7+
*
8+
* http://www.apache.org/licenses/LICENSE-2.0
9+
*
10+
* Unless required by applicable law or agreed to in writing, software
11+
* distributed under the License is distributed on an "AS IS" BASIS,
12+
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13+
* See the License for the specific language governing permissions and
14+
* limitations under the License.
15+
*/
16+
17+
import {Labels} from '@opencensus/core';
18+
import * as resource from '@opencensus/resource-util';
19+
import {MonitoredResource} from './types';
20+
21+
const STACKDRIVER_PROJECT_ID_KEY = 'project_id';
22+
const AWS_REGION_VALUE_PREFIX = 'aws:';
23+
24+
/* Return a self-configured StackDriver monitored resource. */
25+
export async function getDefaultResource(projectId: string):
26+
Promise<MonitoredResource> {
27+
const labels: Labels = {project_id: projectId};
28+
const autoDetectedResource = await resource.detectResource();
29+
const [type, mappings] = getTypeAndMappings(autoDetectedResource.type);
30+
Object.keys(mappings).forEach((key) => {
31+
if (autoDetectedResource.labels[mappings[key]]) {
32+
if (mappings[key] === resource.AWS_REGION_KEY) {
33+
labels[key] = `${AWS_REGION_VALUE_PREFIX}${
34+
autoDetectedResource.labels[mappings[key]]}`;
35+
} else {
36+
labels[key] = autoDetectedResource.labels[mappings[key]];
37+
}
38+
}
39+
});
40+
return {type, labels};
41+
}
42+
43+
function getTypeAndMappings(resourceType: string): [string, Labels] {
44+
switch (resourceType) {
45+
case resource.GCP_GCE_INSTANCE_TYPE:
46+
// https://cloud.google.com/monitoring/api/resources#tag_gce_instance
47+
return [
48+
'gce_instance', {
49+
'project_id': STACKDRIVER_PROJECT_ID_KEY,
50+
'instance_id': resource.GCP_INSTANCE_ID_KEY,
51+
'zone': resource.GCP_ZONE_KEY
52+
}
53+
];
54+
case resource.K8S_CONTAINER_TYPE:
55+
// https://cloud.google.com/monitoring/api/resources#tag_k8s_container
56+
return [
57+
'k8s_container', {
58+
'project_id': STACKDRIVER_PROJECT_ID_KEY,
59+
'location': resource.GCP_ZONE_KEY,
60+
'cluster_name': resource.K8S_CLUSTER_NAME_KEY,
61+
'namespace_name': resource.K8S_NAMESPACE_NAME_KEY,
62+
'pod_name': resource.K8S_POD_NAME_KEY,
63+
'container_name': resource.K8S_CONTAINER_NAME_KEY
64+
}
65+
];
66+
case resource.AWS_EC2_INSTANCE_TYPE:
67+
// https://cloud.google.com/monitoring/api/resources#tag_aws_ec2_instance
68+
return [
69+
'aws_ec2_instance', {
70+
'project_id': STACKDRIVER_PROJECT_ID_KEY,
71+
'instance_id': resource.AWS_INSTANCE_ID_KEY,
72+
'region': resource.AWS_REGION_KEY,
73+
'aws_account': resource.AWS_ACCOUNT_KEY
74+
}
75+
];
76+
default:
77+
return ['global', {}];
78+
}
79+
}

packages/opencensus-exporter-stackdriver/src/stackdriver-cloudtrace.ts

Lines changed: 2 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -14,14 +14,13 @@
1414
* limitations under the License.
1515
*/
1616

17-
import {Exporter, ExporterBuffer, ExporterConfig, RootSpan, Span, SpanContext} from '@opencensus/core';
17+
import {Exporter, ExporterBuffer, RootSpan, Span, SpanContext} from '@opencensus/core';
1818
import {logger, Logger} from '@opencensus/core';
1919
import {auth, JWT} from 'google-auth-library';
2020
import {google} from 'googleapis';
2121
// TODO change to use import when types for hex2dec will be available
2222
const {hexToDec}: {[key: string]: (input: string) => string} =
2323
require('hex2dec');
24-
2524
import {StackdriverExporterOptions, TracesWithCredentials, TranslatedSpan, TranslatedTrace} from './types';
2625

2726
google.options({headers: {'x-opencensus-outgoing-request': 0x1}});
@@ -152,4 +151,4 @@ export class StackdriverTraceExporter implements Exporter {
152151
throw (err);
153152
});
154153
}
155-
}
154+
}

packages/opencensus-exporter-stackdriver/src/stackdriver-monitoring.ts

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -17,8 +17,8 @@
1717
import {logger, Logger, Measurement, Metric, MetricDescriptor as OCMetricDescriptor, MetricProducerManager, Metrics, StatsEventListener, TagKey, TagValue, version, View} from '@opencensus/core';
1818
import {auth, JWT} from 'google-auth-library';
1919
import {google} from 'googleapis';
20-
21-
import {createMetricDescriptorData, createTimeSeriesList, getDefaultResource} from './stackdriver-stats-utils';
20+
import {getDefaultResource} from './common-utils';
21+
import {createMetricDescriptorData, createTimeSeriesList} from './stackdriver-stats-utils';
2222
import {MonitoredResource, StackdriverExporterOptions, TimeSeries} from './types';
2323

2424
const OC_USER_AGENT = {

packages/opencensus-exporter-stackdriver/src/stackdriver-stats-utils.ts

Lines changed: 1 addition & 61 deletions
Original file line numberDiff line numberDiff line change
@@ -14,36 +14,14 @@
1414
* limitations under the License.
1515
*/
1616

17-
import {BucketOptions, DistributionBucket, DistributionValue, LabelKey, Labels, LabelValue, Metric, MetricDescriptor as OCMetricDescriptor, MetricDescriptorType, TimeSeriesPoint, Timestamp} from '@opencensus/core';
18-
import * as resource from '@opencensus/resource-util';
17+
import {BucketOptions, DistributionBucket, DistributionValue, LabelKey, LabelValue, Metric, MetricDescriptor as OCMetricDescriptor, MetricDescriptorType, TimeSeriesPoint, Timestamp} from '@opencensus/core';
1918
import * as os from 'os';
2019
import * as path from 'path';
21-
2220
import {Distribution, LabelDescriptor, MetricDescriptor, MetricKind, MonitoredResource, Point, TimeSeries, ValueType} from './types';
2321

2422
const OPENCENSUS_TASK = 'opencensus_task';
2523
const OPENCENSUS_TASK_DESCRIPTION = 'Opencensus task identifier';
2624
export const OPENCENSUS_TASK_VALUE_DEFAULT = generateDefaultTaskValue();
27-
const STACKDRIVER_PROJECT_ID_KEY = 'project_id';
28-
29-
/* Return a self-configured StackDriver monitored resource. */
30-
export async function getDefaultResource(projectId: string):
31-
Promise<MonitoredResource> {
32-
const labels: Labels = {project_id: projectId};
33-
const autoDetectedResource = await resource.detectResource();
34-
const [type, mappings] = getTypeAndMappings(autoDetectedResource.type);
35-
Object.keys(mappings).forEach((key) => {
36-
if (autoDetectedResource.labels[mappings[key]]) {
37-
if (mappings[key] === resource.AWS_REGION_KEY) {
38-
labels[key] = `${resource.AWS_REGION_VALUE_PREFIX}${
39-
autoDetectedResource.labels[mappings[key]]}`;
40-
} else {
41-
labels[key] = autoDetectedResource.labels[mappings[key]];
42-
}
43-
}
44-
});
45-
return {type, labels};
46-
}
4725

4826
/** Converts a OpenCensus MetricDescriptor to a StackDriver MetricDescriptor. */
4927
export function createMetricDescriptorData(
@@ -258,44 +236,6 @@ function leftZeroPad(ns: number) {
258236
return `${pad}${str}`;
259237
}
260238

261-
function getTypeAndMappings(resourceType: string): [string, Labels] {
262-
switch (resourceType) {
263-
case resource.GCP_GCE_INSTANCE_TYPE:
264-
// https://cloud.google.com/monitoring/api/resources#tag_gce_instance
265-
return [
266-
'gce_instance', {
267-
'project_id': STACKDRIVER_PROJECT_ID_KEY,
268-
'instance_id': resource.GCP_INSTANCE_ID_KEY,
269-
'zone': resource.GCP_ZONE_KEY
270-
}
271-
];
272-
case resource.K8S_CONTAINER_TYPE:
273-
// https://cloud.google.com/monitoring/api/resources#tag_k8s_container
274-
return [
275-
'k8s_container', {
276-
'project_id': STACKDRIVER_PROJECT_ID_KEY,
277-
'location': resource.GCP_ZONE_KEY,
278-
'cluster_name': resource.K8S_CLUSTER_NAME_KEY,
279-
'namespace_name': resource.K8S_NAMESPACE_NAME_KEY,
280-
'pod_name': resource.K8S_POD_NAME_KEY,
281-
'container_name': resource.K8S_CONTAINER_NAME_KEY
282-
}
283-
];
284-
case resource.AWS_EC2_INSTANCE_TYPE:
285-
// https://cloud.google.com/monitoring/api/resources#tag_aws_ec2_instance
286-
return [
287-
'aws_ec2_instance', {
288-
'project_id': STACKDRIVER_PROJECT_ID_KEY,
289-
'instance_id': resource.AWS_INSTANCE_ID_KEY,
290-
'region': resource.AWS_REGION_KEY,
291-
'aws_account': resource.AWS_ACCOUNT_KEY
292-
}
293-
];
294-
default:
295-
return ['global', {}];
296-
}
297-
}
298-
299239
export const TEST_ONLY = {
300240
createMetricType,
301241
createDisplayName,

packages/opencensus-exporter-stackdriver/test/test-stackdriver-stats-utils.ts

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -17,8 +17,9 @@
1717
import {CoreResource, DistributionValue, LabelKey, LabelValue, MetricDescriptor as OCMetricDescriptor, MetricDescriptorType, TimeSeriesPoint, Timestamp} from '@opencensus/core';
1818
import * as assert from 'assert';
1919

20+
import {getDefaultResource} from '../src/common-utils';
2021
import {StackdriverStatsExporter} from '../src/stackdriver-monitoring';
21-
import {createMetricDescriptorData, createTimeSeriesList, getDefaultResource, OPENCENSUS_TASK_VALUE_DEFAULT, TEST_ONLY} from '../src/stackdriver-stats-utils';
22+
import {createMetricDescriptorData, createTimeSeriesList, OPENCENSUS_TASK_VALUE_DEFAULT, TEST_ONLY} from '../src/stackdriver-stats-utils';
2223
import {Distribution, MetricDescriptor, MetricKind, ValueType} from '../src/types';
2324

2425
import * as nocks from './nocks';

packages/opencensus-resource-util/src/resource-labels.ts

Lines changed: 0 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -28,9 +28,6 @@ export const AWS_ACCOUNT_KEY = 'aws.com/ec2/account_id';
2828
/** AWS key that represents the VM instance identifier assigned by AWS. */
2929
export const AWS_INSTANCE_ID_KEY = 'aws.com/ec2/instance_id';
3030

31-
/** AWS key that represents a prefix for region value. */
32-
export const AWS_REGION_VALUE_PREFIX = 'aws:';
33-
3431
/** GCP GCE key that represents a type of the resource. */
3532
export const GCP_GCE_INSTANCE_TYPE = 'cloud.google.com/gce/instance';
3633

0 commit comments

Comments
 (0)