Skip to content

Commit

Permalink
Merge branch 'master' into remove-unused-axios
Browse files Browse the repository at this point in the history
  • Loading branch information
dyladan authored Jan 14, 2021
2 parents d159aee + b8afb04 commit 1570df5
Show file tree
Hide file tree
Showing 23 changed files with 429 additions and 292 deletions.
4 changes: 3 additions & 1 deletion CONTRIBUTING.md
Original file line number Diff line number Diff line change
Expand Up @@ -187,13 +187,15 @@ After adding the package, run `npm install` from the root of the project. This w
- No “changes requested” reviews.
- No unresolved conversations.
- 3 approvals, including the approvals of at least 2 maintainers
- A pull request opened by an approver may be merged with only 2 reviews.
- A pull request opened by an approver may be merged with only the 2 maintainer reviews.
- Small (simple typo, URL, update docs, or grammatical fix) or high-priority changes may be merged more quickly or with fewer reviewers at the discretion of the maintainers. This is typically indicated with the express label.
- For plugins, exporters, and propagators approval of the original code module author is preferred but not required.
- New or changed functionality is tested by unit tests.
- New or changed functionality is documented.
- Substantial changes should not be merged within 24 hours of opening in order to allow reviewers from all time zones to have a chance to review.

If all of the above requirements are met and there are no unresolved discussions, a pull request may be merged by either a maintainer or an approver.

### Generating API documentation

- `npm run docs` to generate API documentation. Generates the documentation in `packages/opentelemetry-api/docs/out`
Expand Down
1 change: 1 addition & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -134,6 +134,7 @@ Approvers ([@open-telemetry/js-approvers](https://github.com/orgs/open-telemetry
- [Matthew Wear](https://github.com/mwear), LightStep
- [Naseem K. Ullah](https://github.com/naseemkullah), Transit
- [Chengzhong Wu](https://github.com/legendecas), Alibaba
- [Gerhard Stöbich](https://github.com/Flarna), Dynatrace

*Find more about the approver role in [community repository](https://github.com/open-telemetry/community/blob/master/community-membership.md#approver).*

Expand Down
7 changes: 7 additions & 0 deletions packages/opentelemetry-api/src/api/propagation.ts
Original file line number Diff line number Diff line change
Expand Up @@ -96,6 +96,13 @@ export class PropagationAPI {
return this._getGlobalPropagator().extract(context, carrier, getter);
}

/**
* Return a list of all fields which may be used by the propagator.
*/
public fields(): string[] {
return this._getGlobalPropagator().fields();
}

/** Remove the global propagator */
public disable() {
delete _global[GLOBAL_PROPAGATION_API_KEY];
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -67,9 +67,6 @@ export interface TextMapPropagator<Carrier = any> {

/**
* Return a list of all fields which may be used by the propagator.
*
* This list should be used to clear fields before calling inject if a carrier is
* used more than once.
*/
fields(): string[];
}
Expand Down
3 changes: 2 additions & 1 deletion packages/opentelemetry-api/src/trace/SpanOptions.ts
Original file line number Diff line number Diff line change
Expand Up @@ -14,6 +14,7 @@
* limitations under the License.
*/

import { TimeInput } from '../common/Time';
import { Attributes } from './attributes';
import { Link } from './link';
import { SpanKind } from './span_kind';
Expand All @@ -35,7 +36,7 @@ export interface SpanOptions {
links?: Link[];

/** A manually specified start time for the created `Span` object. */
startTime?: number;
startTime?: TimeInput;

/** The new span should be a root span. (Ignore parent from context). */
root?: boolean;
Expand Down
7 changes: 7 additions & 0 deletions packages/opentelemetry-api/test/api/api.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -150,6 +150,13 @@ describe('API', () => {
assert.strictEqual(data.carrier, carrier);
assert.strictEqual(data.getter, getter);
});

it('fields', () => {
api.propagation.setGlobalPropagator(new TestTextMapPropagation());

const fields = api.propagation.fields();
assert.deepStrictEqual(fields, ['TestField']);
});
});
});
});
Original file line number Diff line number Diff line change
Expand Up @@ -17,15 +17,15 @@
import {
DEFAULT_ENVIRONMENT,
ENVIRONMENT,
ENVIRONMENT_MAP,
RAW_ENVIRONMENT,
parseEnvironment,
} from '../../utils/environment';

/**
* Gets the environment variables
*/
export function getEnv(): Required<ENVIRONMENT> {
const _window = window as typeof window & ENVIRONMENT_MAP;
const _window = window as typeof window & RAW_ENVIRONMENT;
const globalEnv = parseEnvironment(_window);
return Object.assign({}, DEFAULT_ENVIRONMENT, globalEnv);
}
13 changes: 10 additions & 3 deletions packages/opentelemetry-core/src/platform/node/environment.ts
Original file line number Diff line number Diff line change
Expand Up @@ -14,17 +14,24 @@
* limitations under the License.
*/

import * as os from 'os';
import {
DEFAULT_ENVIRONMENT,
ENVIRONMENT,
ENVIRONMENT_MAP,
RAW_ENVIRONMENT,
parseEnvironment,
} from '../../utils/environment';

/**
* Gets the environment variables
*/
export function getEnv(): Required<ENVIRONMENT> {
const processEnv = parseEnvironment(process.env as ENVIRONMENT_MAP);
return Object.assign({}, DEFAULT_ENVIRONMENT, processEnv);
const processEnv = parseEnvironment(process.env as RAW_ENVIRONMENT);
return Object.assign(
{
HOSTNAME: os.hostname(),
},
DEFAULT_ENVIRONMENT,
processEnv
);
}
117 changes: 90 additions & 27 deletions packages/opentelemetry-core/src/utils/environment.ts
Original file line number Diff line number Diff line change
Expand Up @@ -16,37 +16,78 @@

import { LogLevel } from '../common/types';

export type ENVIRONMENT_MAP = { [key: string]: string | number };
const DEFAULT_LIST_SEPARATOR = ',';

/**
* Environment interface to define all names
*/
export interface ENVIRONMENT {
OTEL_LOG_LEVEL?: LogLevel;
OTEL_NO_PATCH_MODULES?: string;
OTEL_SAMPLING_PROBABILITY?: number;
OTEL_SPAN_ATTRIBUTE_COUNT_LIMIT?: number;
OTEL_SPAN_EVENT_COUNT_LIMIT?: number;
OTEL_SPAN_LINK_COUNT_LIMIT?: number;
OTEL_BSP_MAX_BATCH_SIZE?: number;
OTEL_BSP_SCHEDULE_DELAY_MILLIS?: number;
}

const ENVIRONMENT_NUMBERS: Partial<keyof ENVIRONMENT>[] = [
const ENVIRONMENT_NUMBERS_KEYS = [
'OTEL_BSP_MAX_BATCH_SIZE',
'OTEL_BSP_SCHEDULE_DELAY_MILLIS',
'OTEL_SAMPLING_PROBABILITY',
'OTEL_SPAN_ATTRIBUTE_COUNT_LIMIT',
'OTEL_SPAN_EVENT_COUNT_LIMIT',
'OTEL_SPAN_LINK_COUNT_LIMIT',
'OTEL_BSP_MAX_BATCH_SIZE',
'OTEL_BSP_SCHEDULE_DELAY_MILLIS',
];
] as const;

type ENVIRONMENT_NUMBERS = {
[K in typeof ENVIRONMENT_NUMBERS_KEYS[number]]?: number;
};

function isEnvVarANumber(key: unknown): key is keyof ENVIRONMENT_NUMBERS {
return (
ENVIRONMENT_NUMBERS_KEYS.indexOf(key as keyof ENVIRONMENT_NUMBERS) > -1
);
}

const ENVIRONMENT_LISTS_KEYS = ['OTEL_NO_PATCH_MODULES'] as const;

type ENVIRONMENT_LISTS = {
[K in typeof ENVIRONMENT_LISTS_KEYS[number]]?: string[];
};

function isEnvVarAList(key: unknown): key is keyof ENVIRONMENT_LISTS {
return ENVIRONMENT_LISTS_KEYS.indexOf(key as keyof ENVIRONMENT_LISTS) > -1;
}

export type ENVIRONMENT = {
CONTAINER_NAME?: string;
ECS_CONTAINER_METADATA_URI_V4?: string;
ECS_CONTAINER_METADATA_URI?: string;
HOSTNAME?: string;
KUBERNETES_SERVICE_HOST?: string;
NAMESPACE?: string;
OTEL_EXPORTER_JAEGER_AGENT_HOST?: string;
OTEL_EXPORTER_JAEGER_ENDPOINT?: string;
OTEL_EXPORTER_JAEGER_PASSWORD?: string;
OTEL_EXPORTER_JAEGER_USER?: string;
OTEL_LOG_LEVEL?: LogLevel;
OTEL_RESOURCE_ATTRIBUTES?: string;
} & ENVIRONMENT_NUMBERS &
ENVIRONMENT_LISTS;

export type RAW_ENVIRONMENT = {
[key: string]: string | number | undefined | string[];
};

/**
* Default environment variables
*/
export const DEFAULT_ENVIRONMENT: Required<ENVIRONMENT> = {
OTEL_NO_PATCH_MODULES: '',
CONTAINER_NAME: '',
ECS_CONTAINER_METADATA_URI_V4: '',
ECS_CONTAINER_METADATA_URI: '',
HOSTNAME: '',
KUBERNETES_SERVICE_HOST: '',
NAMESPACE: '',
OTEL_EXPORTER_JAEGER_AGENT_HOST: '',
OTEL_EXPORTER_JAEGER_ENDPOINT: '',
OTEL_EXPORTER_JAEGER_PASSWORD: '',
OTEL_EXPORTER_JAEGER_USER: '',
OTEL_LOG_LEVEL: LogLevel.INFO,
OTEL_NO_PATCH_MODULES: [],
OTEL_RESOURCE_ATTRIBUTES: '',
OTEL_SAMPLING_PROBABILITY: 1,
OTEL_SPAN_ATTRIBUTE_COUNT_LIMIT: 1000,
OTEL_SPAN_EVENT_COUNT_LIMIT: 1000,
Expand All @@ -64,15 +105,14 @@ export const DEFAULT_ENVIRONMENT: Required<ENVIRONMENT> = {
* @param max
*/
function parseNumber(
name: keyof ENVIRONMENT,
environment: ENVIRONMENT_MAP | ENVIRONMENT,
values: ENVIRONMENT_MAP,
name: keyof ENVIRONMENT_NUMBERS,
environment: ENVIRONMENT,
values: RAW_ENVIRONMENT,
min = -Infinity,
max = Infinity
) {
if (typeof values[name] !== 'undefined') {
const value = Number(values[name] as string);

if (!isNaN(value)) {
if (value < min) {
environment[name] = min;
Expand All @@ -85,6 +125,25 @@ function parseNumber(
}
}

/**
* Parses list-like strings from input into output.
* @param name
* @param environment
* @param values
* @param separator
*/
function parseStringList(
name: keyof ENVIRONMENT_LISTS,
output: ENVIRONMENT,
input: RAW_ENVIRONMENT,
separator = DEFAULT_LIST_SEPARATOR
) {
const givenValue = input[name];
if (typeof givenValue === 'string') {
output[name] = givenValue.split(separator).map(v => v.trim());
}
}

/**
* Environmentally sets log level if valid log level string is provided
* @param key
Expand All @@ -93,8 +152,8 @@ function parseNumber(
*/
function setLogLevelFromEnv(
key: keyof ENVIRONMENT,
environment: ENVIRONMENT_MAP | ENVIRONMENT,
values: ENVIRONMENT_MAP
environment: RAW_ENVIRONMENT | ENVIRONMENT,
values: RAW_ENVIRONMENT
) {
const value = values[key];
switch (typeof value === 'string' ? value.toUpperCase() : value) {
Expand Down Expand Up @@ -124,11 +183,12 @@ function setLogLevelFromEnv(
* Parses environment values
* @param values
*/
export function parseEnvironment(values: ENVIRONMENT_MAP): ENVIRONMENT {
const environment: ENVIRONMENT_MAP = {};
export function parseEnvironment(values: RAW_ENVIRONMENT): ENVIRONMENT {
const environment: ENVIRONMENT = {};

for (const env in DEFAULT_ENVIRONMENT) {
const key = env as keyof ENVIRONMENT;

switch (key) {
case 'OTEL_SAMPLING_PROBABILITY':
parseNumber(key, environment, values, 0, 1);
Expand All @@ -139,11 +199,14 @@ export function parseEnvironment(values: ENVIRONMENT_MAP): ENVIRONMENT {
break;

default:
if (ENVIRONMENT_NUMBERS.indexOf(key) >= 0) {
if (isEnvVarANumber(key)) {
parseNumber(key, environment, values);
} else if (isEnvVarAList(key)) {
parseStringList(key, environment, values);
} else {
if (typeof values[key] !== 'undefined') {
environment[key] = values[key];
const value = values[key];
if (typeof value !== 'undefined' && value !== null) {
environment[key] = String(value);
}
}
}
Expand Down
Loading

0 comments on commit 1570df5

Please sign in to comment.