Skip to content

Commit

Permalink
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
refactor: formalize Detector interface
Browse files Browse the repository at this point in the history
mwear committed Apr 1, 2020

Verified

This commit was signed with the committer’s verified signature.
stmcginnis Sean McGinnis
1 parent a2c936f commit db0df52
Showing 7 changed files with 38 additions and 11 deletions.
5 changes: 1 addition & 4 deletions packages/opentelemetry-resources/src/Resource.ts
Original file line number Diff line number Diff line change
@@ -16,6 +16,7 @@

import { SDK_INFO } from '@opentelemetry/base';
import { TELEMETRY_SDK_RESOURCE } from './constants';
import { Labels } from './types';

/**
* A Resource describes the entity for which a signals (metrics or trace) are
@@ -67,7 +68,3 @@ export class Resource {
return new Resource(mergedLabels);
}
}

export interface Labels {
[key: string]: number | string | boolean;
}
3 changes: 2 additions & 1 deletion packages/opentelemetry-resources/src/index.ts
Original file line number Diff line number Diff line change
@@ -14,6 +14,7 @@
* limitations under the License.
*/

export { Resource, Labels } from './Resource';
export * from './Resource';
export * from './platform';
export * from './constants';
export * from './types';
Original file line number Diff line number Diff line change
@@ -16,8 +16,9 @@

import { Resource } from '../../Resource';
import { envDetector, awsEc2Detector, gcpDetector } from './detectors';
import { Detector } from '../../types';

const DETECTORS = [envDetector, awsEc2Detector, gcpDetector];
const DETECTORS: Array<Detector> = [envDetector, awsEc2Detector, gcpDetector];

/**
* Runs all resource detectors and returns the results merged into a single
Original file line number Diff line number Diff line change
@@ -17,13 +17,14 @@
import * as http from 'http';
import { Resource } from '../../../Resource';
import { CLOUD_RESOURCE, HOST_RESOURCE } from '../../../constants';
import { Detector } from '../../../types';

/**
* The AwsEc2Detector can be used to detect if a process is running in AWS EC2
* and return a {@link Resource} populated with metadata about the EC2
* instance. Returns an empty Resource if detection fails.
*/
class AwsEc2Detector {
class AwsEc2Detector implements Detector {
readonly AWS_INSTANCE_IDENTITY_DOCUMENT_URI =
'http://169.254.169.254/latest/dynamic/instance-identity/document';

Original file line number Diff line number Diff line change
@@ -14,13 +14,14 @@
* limitations under the License.
*/

import { Resource, Labels } from '../../../Resource';
import { Resource } from '../../../Resource';
import { Detector, Labels } from '../../../types';

/**
* EnvDetector can be used to detect the presence of and create a Resource
* from the OTEL_RESOURCE_LABELS environment variable.
*/
class EnvDetector {
class EnvDetector implements Detector {
// Type, label keys, and label values should not exceed 256 characters.
private readonly _MAX_LENGTH = 255;

Original file line number Diff line number Diff line change
@@ -16,7 +16,8 @@

import * as os from 'os';
import * as gcpMetadata from 'gcp-metadata';
import { Resource, Labels } from '../../../Resource';
import { Resource } from '../../../Resource';
import { Detector, Labels } from '../../../types';
import {
CLOUD_RESOURCE,
HOST_RESOURCE,
@@ -29,7 +30,7 @@ import {
* Cloud Platofrm and return a {@link Resource} populated with metadata about
* the instance. Returns an empty Resource if detection fails.
*/
class GcpDetector {
class GcpDetector implements Detector {
async detect(): Promise<Resource> {
if (!(await gcpMetadata.isAvailable())) return Resource.empty();

25 changes: 25 additions & 0 deletions packages/opentelemetry-resources/src/types.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,25 @@
/**
* Copyright 2020, OpenTelemetry 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
*
* https://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 { Resource } from './Resource';

export interface Labels {
[key: string]: number | string | boolean;
}

export interface Detector {
detect(): Promise<Resource>;
}

0 comments on commit db0df52

Please sign in to comment.