Skip to content

Commit

Permalink
Merge branch 'main' into instrumentation-iter-modules
Browse files Browse the repository at this point in the history
  • Loading branch information
vmarchaud authored Apr 20, 2021
2 parents 0a0bb77 + 27f64d9 commit a987527
Show file tree
Hide file tree
Showing 16 changed files with 422 additions and 109 deletions.
4 changes: 3 additions & 1 deletion README.md
Original file line number Diff line number Diff line change
Expand Up @@ -248,9 +248,11 @@ To request automatic tracing support for a module not on this list, please [file
### 0.18.x to 0.19.0

- All plugins have been removed in favor of instrumentations.

- The `@opentelemetry/propagator-b3` package previously exported three propagators: `B3Propagator`,`B3SinglePropagator`, and `B3MultiPropagator`, but now only exports the `B3Propagator`. It extracts b3 context in single and multi-header encodings, and injects context using the single-header encoding by default, but can be configured to inject context using the multi-header endcoding during construction: `new B3Propagator({ injectEncoding: B3InjectEncoding.MULTI_HEADER })`. If you were previously using the `B3SinglePropagator` or `B3MultiPropagator` directly, you should update your code to use the `B3Propagator` with the appropriate configuration. See the [readme](./packages/opentelemetry-propagator-b3/readme.md) for full details and usage.

- Sampling configuration via environment variable has changed. If you were using `OTEL_SAMPLING_PROBABILITY` then you should replace it with `OTEL_TRACES_SAMPLER=parentbased_traceidratio` and `OTEL_TRACES_SAMPLER_ARG=<number>` where `<number>` is a number in the [0..1] range, e.g. "0.25". Default is 1.0 if unset.

### 0.17.0 to 0.18.0

- `diag.setLogLevel` is removed and LogLevel can be set by an optional second parameter to `setLogger`
Expand Down
1 change: 1 addition & 0 deletions packages/opentelemetry-core/src/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -34,3 +34,4 @@ export * from './trace/TraceState';
export * from './trace/IdGenerator';
export * from './utils/url';
export * from './utils/wrap';
export * from './utils/sampling';
11 changes: 5 additions & 6 deletions packages/opentelemetry-core/src/utils/environment.ts
Original file line number Diff line number Diff line change
Expand Up @@ -15,6 +15,7 @@
*/

import { DiagLogLevel } from '@opentelemetry/api';
import { TracesSamplerValues } from './sampling';

const DEFAULT_LIST_SEPARATOR = ',';

Expand All @@ -27,7 +28,6 @@ const ENVIRONMENT_NUMBERS_KEYS = [
'OTEL_BSP_MAX_EXPORT_BATCH_SIZE',
'OTEL_BSP_MAX_QUEUE_SIZE',
'OTEL_BSP_SCHEDULE_DELAY',
'OTEL_SAMPLING_PROBABILITY',
'OTEL_SPAN_ATTRIBUTE_COUNT_LIMIT',
'OTEL_SPAN_EVENT_COUNT_LIMIT',
'OTEL_SPAN_LINK_COUNT_LIMIT',
Expand Down Expand Up @@ -70,6 +70,8 @@ export type ENVIRONMENT = {
OTEL_EXPORTER_ZIPKIN_ENDPOINT?: string;
OTEL_LOG_LEVEL?: DiagLogLevel;
OTEL_RESOURCE_ATTRIBUTES?: string;
OTEL_TRACES_SAMPLER_ARG?: string;
OTEL_TRACES_SAMPLER?: string;
} & ENVIRONMENT_NUMBERS &
ENVIRONMENT_LISTS;

Expand Down Expand Up @@ -100,10 +102,11 @@ export const DEFAULT_ENVIRONMENT: Required<ENVIRONMENT> = {
OTEL_NO_PATCH_MODULES: [],
OTEL_PROPAGATORS: ['tracecontext', 'baggage'],
OTEL_RESOURCE_ATTRIBUTES: '',
OTEL_SAMPLING_PROBABILITY: 1,
OTEL_SPAN_ATTRIBUTE_COUNT_LIMIT: 128,
OTEL_SPAN_EVENT_COUNT_LIMIT: 128,
OTEL_SPAN_LINK_COUNT_LIMIT: 128,
OTEL_TRACES_SAMPLER: TracesSamplerValues.ParentBasedAlwaysOn,
OTEL_TRACES_SAMPLER_ARG: '',
};

/**
Expand Down Expand Up @@ -196,10 +199,6 @@ export function parseEnvironment(values: RAW_ENVIRONMENT): ENVIRONMENT {
const key = env as keyof ENVIRONMENT;

switch (key) {
case 'OTEL_SAMPLING_PROBABILITY':
parseNumber(key, environment, values, 0, 1);
break;

case 'OTEL_LOG_LEVEL':
setLogLevelFromEnv(key, environment, values);
break;
Expand Down
24 changes: 24 additions & 0 deletions packages/opentelemetry-core/src/utils/sampling.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,24 @@
/*
* Copyright The 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.
*/

export enum TracesSamplerValues {
AlwaysOff = 'always_off',
AlwaysOn = 'always_on',
ParentBasedAlwaysOff = 'parentbased_always_off',
ParentBasedAlwaysOn = 'parentbased_always_on',
ParentBasedTraceIdRatio = 'parentbased_traceidratio',
TraceIdRatio = 'traceidratio',
}
28 changes: 10 additions & 18 deletions packages/opentelemetry-core/test/utils/environment.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -23,6 +23,7 @@ import {
import * as assert from 'assert';
import * as sinon from 'sinon';
import { DiagLogLevel } from '@opentelemetry/api';
import { TracesSamplerValues } from '../../src';

let lastMock: RAW_ENVIRONMENT = {};

Expand Down Expand Up @@ -83,15 +84,15 @@ describe('environment', () => {
OTEL_LOG_LEVEL: 'ERROR',
OTEL_NO_PATCH_MODULES: 'a,b,c',
OTEL_RESOURCE_ATTRIBUTES: '<attrs>',
OTEL_SAMPLING_PROBABILITY: '0.5',
OTEL_SPAN_ATTRIBUTE_COUNT_LIMIT: 10,
OTEL_SPAN_EVENT_COUNT_LIMIT: 20,
OTEL_SPAN_LINK_COUNT_LIMIT: 30,
OTEL_TRACES_SAMPLER: 'always_on',
OTEL_TRACES_SAMPLER_ARG: '0.5',
});
const env = getEnv();
assert.deepStrictEqual(env.OTEL_NO_PATCH_MODULES, ['a', 'b', 'c']);
assert.strictEqual(env.OTEL_LOG_LEVEL, DiagLogLevel.ERROR);
assert.strictEqual(env.OTEL_SAMPLING_PROBABILITY, 0.5);
assert.strictEqual(env.OTEL_SPAN_ATTRIBUTE_COUNT_LIMIT, 10);
assert.strictEqual(env.OTEL_SPAN_EVENT_COUNT_LIMIT, 20);
assert.strictEqual(env.OTEL_SPAN_LINK_COUNT_LIMIT, 30);
Expand All @@ -117,20 +118,8 @@ describe('environment', () => {
assert.strictEqual(env.OTEL_RESOURCE_ATTRIBUTES, '<attrs>');
assert.strictEqual(env.OTEL_BSP_MAX_EXPORT_BATCH_SIZE, 40);
assert.strictEqual(env.OTEL_BSP_SCHEDULE_DELAY, 50);
});

it('should match invalid values to closest valid equivalent', () => {
mockEnvironment({
OTEL_SAMPLING_PROBABILITY: '-0.1',
});
const minEnv = getEnv();
assert.strictEqual(minEnv.OTEL_SAMPLING_PROBABILITY, 0);

mockEnvironment({
OTEL_SAMPLING_PROBABILITY: '1.1',
});
const maxEnv = getEnv();
assert.strictEqual(maxEnv.OTEL_SAMPLING_PROBABILITY, 1);
assert.strictEqual(env.OTEL_TRACES_SAMPLER, 'always_on');
assert.strictEqual(env.OTEL_TRACES_SAMPLER_ARG, '0.5');
});

it('should parse OTEL_LOG_LEVEL despite casing', () => {
Expand Down Expand Up @@ -158,12 +147,15 @@ describe('environment', () => {
it('should remove a mock environment', () => {
mockEnvironment({
OTEL_LOG_LEVEL: 'DEBUG',
OTEL_SAMPLING_PROBABILITY: 0.5,
OTEL_TRACES_SAMPLER: TracesSamplerValues.AlwaysOff,
});
removeMockEnvironment();
const env = getEnv();
assert.strictEqual(env.OTEL_LOG_LEVEL, DiagLogLevel.INFO);
assert.strictEqual(env.OTEL_SAMPLING_PROBABILITY, 1);
assert.strictEqual(
env.OTEL_TRACES_SAMPLER,
TracesSamplerValues.ParentBasedAlwaysOn
);
});
});
});
10 changes: 9 additions & 1 deletion packages/opentelemetry-resource-detector-aws/README.md
Original file line number Diff line number Diff line change
Expand Up @@ -27,7 +27,15 @@ const resource = await detectResources({
const tracerProvider = new NodeTracerProvider({ resource });
```

**Note**: Besides `awsEc2Detector` there are also the following detectors available: `awsBeanstalkDetector`, `awsEksDetector` and `awsEcsDetector`
## Available detectors

- `awsBeanstalkDetector`: Populates `service` for processes running on [AWS Elastic Beanstalk](https://docs.aws.amazon.com/elasticbeanstalk/latest/dg/Welcome.html)
- `awsEc2Detector`: Populates `cloud` and `host` for processes running on [Amazon EC2](https://aws.amazon.com/ec2/), including abstractions such as ECS on EC2. Notably, it does not populate anything on AWS Fargate
- `awsEcsDetector`: Populates `container` for containers running on [Amazon ECS](https://aws.amazon.com/ecs/)
- `awsEksDetector`: Populates `container` and `k8s.cluster_name` for containers running on [Amazon EKS](https://aws.amazon.com/eks/)
- `k8s.cluster_name` is not always available depending on the configuration of CloudWatch monitoring for the EKS cluster
- `awsLambdaDetector`: Populates `faas` and `cloud` for functions running on [AWS Lambda](https://aws.amazon.com/lambda/)
- `faas.id` is currently not populated as it is not provided by the runtime at startup

## Useful links

Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,58 @@
/*
* Copyright The 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 {
Detector,
Resource,
CLOUD_RESOURCE,
ResourceDetectionConfig,
} from '@opentelemetry/resources';

/**
* The AwsLambdaDetector can be used to detect if a process is running in AWS Lambda
* and return a {@link Resource} populated with data about the environment.
* Returns an empty Resource if detection fails.
*/
export class AwsLambdaDetector implements Detector {
async detect(_config?: ResourceDetectionConfig): Promise<Resource> {
const functionName = process.env.AWS_LAMBDA_FUNCTION_NAME;
if (!functionName) {
return Resource.empty();
}

const functionVersion = process.env.AWS_LAMBDA_FUNCTION_VERSION;
const region = process.env.AWS_REGION;

const attributes = {
[CLOUD_RESOURCE.PROVIDER]: 'aws',
};
if (region) {
attributes[CLOUD_RESOURCE.REGION] = region;
}

// TODO(https://github.com/open-telemetry/opentelemetry-js/issues/2123): Migrate to FAAS_RESOURCE when defined.
if (functionName) {
attributes['faas.name'] = functionName;
}
if (functionVersion) {
attributes['faas.version'] = functionVersion;
}

return new Resource(attributes);
}
}

export const awsLambdaDetector = new AwsLambdaDetector();
Original file line number Diff line number Diff line change
Expand Up @@ -18,3 +18,4 @@ export * from './AwsEc2Detector';
export * from './AwsBeanstalkDetector';
export * from './AwsEcsDetector';
export * from './AwsEksDetector';
export * from './AwsLambdaDetector';
Original file line number Diff line number Diff line change
@@ -0,0 +1,64 @@
/*
* Copyright The 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 * as assert from 'assert';
import {
assertCloudResource,
assertEmptyResource,
} from '@opentelemetry/resources/test/util/resource-assertions';

import { awsLambdaDetector } from '../../src';

describe('awsLambdaDetector', () => {
let oldEnv: NodeJS.ProcessEnv;

beforeEach(() => {
oldEnv = { ...process.env };
});

afterEach(() => {
process.env = oldEnv;
});

describe('on lambda', () => {
it('fills resource', async () => {
process.env.AWS_LAMBDA_FUNCTION_NAME = 'name';
process.env.AWS_LAMBDA_FUNCTION_VERSION = 'v1';
process.env.AWS_REGION = 'us-east-1';

const resource = await awsLambdaDetector.detect();

assertCloudResource(resource, {
provider: 'aws',
region: 'us-east-1',
});

assert.strictEqual(resource.attributes['faas.name'], 'name');
assert.strictEqual(resource.attributes['faas.version'], 'v1');
});
});

describe('not on lambda', () => {
it('returns empty resource', async () => {
process.env.AWS_LAMBDA_FUNCTION_VERSION = 'v1';
process.env.AWS_REGION = 'us-east-1';

const resource = await awsLambdaDetector.detect();

assertEmptyResource(resource);
});
});
});
3 changes: 1 addition & 2 deletions packages/opentelemetry-tracing/README.md
Original file line number Diff line number Diff line change
Expand Up @@ -46,8 +46,7 @@ span.end();

Tracing configuration is a merge of user supplied configuration with both the default
configuration as specified in [config.ts](./src/config.ts) and an
environmentally configurable (via `OTEL_SAMPLING_PROBABILITY`) probability
sampler delegate of a [ParentBased](https://github.com/open-telemetry/opentelemetry-specification/blob/master/specification/trace/sdk.md#parentbased) sampler.
environmentally configurable sampling (via `OTEL_TRACES_SAMPLER` and `OTEL_TRACES_SAMPLER_ARG`).

## Example

Expand Down
Loading

0 comments on commit a987527

Please sign in to comment.