Skip to content

Commit

Permalink
Merge branch 'main' into prometheus-serialiser
Browse files Browse the repository at this point in the history
  • Loading branch information
dyladan authored Jul 29, 2022
2 parents edebdad + df58fac commit 355fc6a
Show file tree
Hide file tree
Showing 43 changed files with 1,349 additions and 168 deletions.
3 changes: 3 additions & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,8 @@ All notable changes to this project will be documented in this file.

### :rocket: (Enhancement)

feat(sdk-trace-base): move Sampler declaration into sdk-trace-base [#3088](https://github.com/open-telemetry/opentelemetry-js/pull/3088) @legendecas

### :bug: (Bug Fix)

### :books: (Refine Doc)
Expand All @@ -26,6 +28,7 @@ All notable changes to this project will be documented in this file.

* fix(resources): fix browser compatibility for host and os detectors [#3004](https://github.com/open-telemetry/opentelemetry-js/pull/3004) @legendecas
* fix(sdk-trace-base): fix crash on environments without global document [#3000](https://github.com/open-telemetry/opentelemetry-js/pull/3000) @legendecas
* fix(sdk-trace-base): fix spanLimits attribute length/count to consider env values [#3068](https://github.com/open-telemetry/opentelemetry-js/pull/3068) @svetlanabrennan

### :house: (Internal)

Expand Down
5 changes: 3 additions & 2 deletions experimental/packages/opentelemetry-sdk-node/src/types.ts
Original file line number Diff line number Diff line change
Expand Up @@ -15,14 +15,15 @@
*/

import type { ContextManager, SpanAttributes } from '@opentelemetry/api';
import { Sampler, TextMapPropagator } from '@opentelemetry/api';
import { TextMapPropagator } from '@opentelemetry/api';
import { InstrumentationOption } from '@opentelemetry/instrumentation';
import { Resource } from '@opentelemetry/resources';
import { MetricReader } from '@opentelemetry/sdk-metrics-base';
import {
Sampler,
SpanExporter,
SpanLimits,
SpanProcessor
SpanProcessor,
} from '@opentelemetry/sdk-trace-base';

export interface NodeSDKConfiguration {
Expand Down
106 changes: 0 additions & 106 deletions packages/opentelemetry-core/README.md
Original file line number Diff line number Diff line change
Expand Up @@ -13,11 +13,6 @@ This package provides default implementations of the OpenTelemetry API for trace
- [W3CTraceContextPropagator Propagator](#w3ctracecontextpropagator-propagator)
- [Composite Propagator](#composite-propagator)
- [Baggage Propagator](#baggage-propagator)
- [Built-in Sampler](#built-in-sampler)
- [AlwaysOn Sampler](#alwayson-sampler)
- [AlwaysOff Sampler](#alwaysoff-sampler)
- [TraceIdRatioBased Sampler](#traceidratiobased-sampler)
- [ParentBased Sampler](#parentbased-sampler)
- [Useful links](#useful-links)
- [License](#license)

Expand Down Expand Up @@ -61,107 +56,6 @@ const { W3CBaggagePropagator } = require("@opentelemetry/core");
api.propagation.setGlobalPropagator(new W3CBaggagePropagator());
```

### Built-in Sampler

Sampler is used to make decisions on `Span` sampling.

#### AlwaysOn Sampler

Samples every trace regardless of upstream sampling decisions.

> This is used as a default Sampler
```js
const { NodeTracerProvider } = require("@opentelemetry/sdk-trace-node");
const { AlwaysOnSampler } = require("@opentelemetry/core");

const tracerProvider = new NodeTracerProvider({
sampler: new AlwaysOnSampler()
});
```

#### AlwaysOff Sampler

Doesn't sample any trace, regardless of upstream sampling decisions.

```js
const { NodeTracerProvider } = require("@opentelemetry/sdk-trace-node");
const { AlwaysOffSampler } = require("@opentelemetry/core");

const tracerProvider = new NodeTracerProvider({
sampler: new AlwaysOffSampler()
});
```

#### TraceIdRatioBased Sampler

Samples some percentage of traces, calculated deterministically using the trace ID.
Any trace that would be sampled at a given percentage will also be sampled at any higher percentage.

The `TraceIDRatioSampler` may be used with the `ParentBasedSampler` to respect the sampled flag of an incoming trace.

```js
const { NodeTracerProvider } = require("@opentelemetry/sdk-trace-node");
const { TraceIdRatioBasedSampler } = require("@opentelemetry/core");

const tracerProvider = new NodeTracerProvider({
// See details of ParentBasedSampler below
sampler: new ParentBasedSampler({
// Trace ID Ratio Sampler accepts a positional argument
// which represents the percentage of traces which should
// be sampled.
root: new TraceIdRatioBasedSampler(0.5)
});
});
```

#### ParentBased Sampler

- This is a composite sampler. `ParentBased` helps distinguished between the
following cases:
- No parent (root span).
- Remote parent with `sampled` flag `true`
- Remote parent with `sampled` flag `false`
- Local parent with `sampled` flag `true`
- Local parent with `sampled` flag `false`

Required parameters:

- `root(Sampler)` - Sampler called for spans with no parent (root spans)

Optional parameters:

- `remoteParentSampled(Sampler)` (default: `AlwaysOn`)
- `remoteParentNotSampled(Sampler)` (default: `AlwaysOff`)
- `localParentSampled(Sampler)` (default: `AlwaysOn`)
- `localParentNotSampled(Sampler)` (default: `AlwaysOff`)

|Parent| parent.isRemote() | parent.isSampled()| Invoke sampler|
|--|--|--|--|
|absent| n/a | n/a |`root()`|
|present|true|true|`remoteParentSampled()`|
|present|true|false|`remoteParentNotSampled()`|
|present|false|true|`localParentSampled()`|
|present|false|false|`localParentNotSampled()`|

```js
const { NodeTracerProvider } = require("@opentelemetry/sdk-trace-node");
const { ParentBasedSampler, AlwaysOffSampler, TraceIdRatioBasedSampler } = require("@opentelemetry/core");

const tracerProvider = new NodeTracerProvider({
sampler: new ParentBasedSampler({
// By default, the ParentBasedSampler will respect the parent span's sampling
// decision. This is configurable by providing a different sampler to use
// based on the situation. See configuration details above.
//
// This will delegate the sampling decision of all root traces (no parent)
// to the TraceIdRatioBasedSampler.
// See details of TraceIdRatioBasedSampler above.
root: new TraceIdRatioBasedSampler(0.5)
})
});
```

## Useful links

- For more information on OpenTelemetry, visit: <https://opentelemetry.io/>
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -18,6 +18,9 @@ import { IdGenerator } from '../../trace/IdGenerator';
const SPAN_ID_BYTES = 8;
const TRACE_ID_BYTES = 16;

/**
* @deprecated Use the one defined in @opentelemetry/sdk-trace-base instead.
*/
export class RandomIdGenerator implements IdGenerator {
/**
* Returns a random 16-byte trace ID formatted/encoded as a 32 lowercase hex
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -18,6 +18,9 @@ import { IdGenerator } from '../../trace/IdGenerator';
const SPAN_ID_BYTES = 8;
const TRACE_ID_BYTES = 16;

/**
* @deprecated Use the one defined in @opentelemetry/sdk-trace-base instead.
*/
export class RandomIdGenerator implements IdGenerator {
/**
* Returns a random 16-byte trace ID formatted/encoded as a 32 lowercase hex
Expand Down
5 changes: 4 additions & 1 deletion packages/opentelemetry-core/src/trace/IdGenerator.ts
Original file line number Diff line number Diff line change
Expand Up @@ -14,7 +14,10 @@
* limitations under the License.
*/

/** IdGenerator provides an interface for generating Trace Id and Span Id */
/**
* @deprecated Use the one defined in @opentelemetry/sdk-trace-base instead.
* IdGenerator provides an interface for generating Trace Id and Span Id.
*/
export interface IdGenerator {
/** Returns a trace ID composed of 32 lowercase hex characters. */
generateTraceId(): string;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -16,7 +16,10 @@

import { Sampler, SamplingDecision, SamplingResult } from '@opentelemetry/api';

/** Sampler that samples no traces. */
/**
* @deprecated Use the one defined in @opentelemetry/sdk-trace-base instead.
* Sampler that samples no traces.
*/
export class AlwaysOffSampler implements Sampler {
shouldSample(): SamplingResult {
return {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -16,7 +16,10 @@

import { Sampler, SamplingDecision, SamplingResult } from '@opentelemetry/api';

/** Sampler that samples all traces. */
/**
* @deprecated Use the one defined in @opentelemetry/sdk-trace-base instead.
* Sampler that samples all traces.
*/
export class AlwaysOnSampler implements Sampler {
shouldSample(): SamplingResult {
return {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -29,6 +29,7 @@ import { AlwaysOffSampler } from './AlwaysOffSampler';
import { AlwaysOnSampler } from './AlwaysOnSampler';

/**
* @deprecated Use the one defined in @opentelemetry/sdk-trace-base instead.
* A composite sampler that either respects the parent span's sampling decision
* or delegates to `delegateSampler` for root spans.
*/
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -21,7 +21,10 @@ import {
isValidTraceId,
} from '@opentelemetry/api';

/** Sampler that samples a given fraction of traces based of trace id deterministically. */
/**
* @deprecated Use the one defined in @opentelemetry/sdk-trace-base instead.
* Sampler that samples a given fraction of traces based of trace id deterministically.
*/
export class TraceIdRatioBasedSampler implements Sampler {
private _upperBound: number;

Expand Down
11 changes: 11 additions & 0 deletions packages/opentelemetry-core/src/utils/environment.ts
Original file line number Diff line number Diff line change
Expand Up @@ -16,6 +16,7 @@

import { DiagLogLevel } from '@opentelemetry/api';
import { TracesSamplerValues } from './sampling';
import { _globalThis } from '../platform/browser/globalThis';

const DEFAULT_LIST_SEPARATOR = ',';

Expand Down Expand Up @@ -283,3 +284,13 @@ export function parseEnvironment(values: RAW_ENVIRONMENT): ENVIRONMENT {

return environment;
}

/**
* Get environment in node or browser without
* populating default values.
*/
export function getEnvWithoutDefaults(): ENVIRONMENT {
return typeof process !== 'undefined' ?
parseEnvironment(process.env as RAW_ENVIRONMENT) :
parseEnvironment(_globalThis as typeof globalThis & RAW_ENVIRONMENT);
}
101 changes: 101 additions & 0 deletions packages/opentelemetry-sdk-trace-base/README.md
Original file line number Diff line number Diff line change
Expand Up @@ -46,6 +46,107 @@ Tracing configuration is a merge of user supplied configuration with both the de
configuration as specified in [config.ts](./src/config.ts) and an
environmentally configurable sampling (via `OTEL_TRACES_SAMPLER` and `OTEL_TRACES_SAMPLER_ARG`).

## Built-in Samplers

Sampler is used to make decisions on `Span` sampling.

### AlwaysOn Sampler

Samples every trace regardless of upstream sampling decisions.

> This is used as a default Sampler
```js
const { NodeTracerProvider } = require("@opentelemetry/sdk-trace-node");
const { AlwaysOnSampler } = require("@opentelemetry/core");

const tracerProvider = new NodeTracerProvider({
sampler: new AlwaysOnSampler()
});
```

### AlwaysOff Sampler

Doesn't sample any trace, regardless of upstream sampling decisions.

```js
const { NodeTracerProvider } = require("@opentelemetry/sdk-trace-node");
const { AlwaysOffSampler } = require("@opentelemetry/core");

const tracerProvider = new NodeTracerProvider({
sampler: new AlwaysOffSampler()
});
```

### TraceIdRatioBased Sampler

Samples some percentage of traces, calculated deterministically using the trace ID.
Any trace that would be sampled at a given percentage will also be sampled at any higher percentage.

The `TraceIDRatioSampler` may be used with the `ParentBasedSampler` to respect the sampled flag of an incoming trace.

```js
const { NodeTracerProvider } = require("@opentelemetry/sdk-trace-node");
const { TraceIdRatioBasedSampler } = require("@opentelemetry/core");

const tracerProvider = new NodeTracerProvider({
// See details of ParentBasedSampler below
sampler: new ParentBasedSampler({
// Trace ID Ratio Sampler accepts a positional argument
// which represents the percentage of traces which should
// be sampled.
root: new TraceIdRatioBasedSampler(0.5)
});
});
```

### ParentBased Sampler

- This is a composite sampler. `ParentBased` helps distinguished between the
following cases:
- No parent (root span).
- Remote parent with `sampled` flag `true`
- Remote parent with `sampled` flag `false`
- Local parent with `sampled` flag `true`
- Local parent with `sampled` flag `false`

Required parameters:

- `root(Sampler)` - Sampler called for spans with no parent (root spans)

Optional parameters:

- `remoteParentSampled(Sampler)` (default: `AlwaysOn`)
- `remoteParentNotSampled(Sampler)` (default: `AlwaysOff`)
- `localParentSampled(Sampler)` (default: `AlwaysOn`)
- `localParentNotSampled(Sampler)` (default: `AlwaysOff`)

|Parent| parent.isRemote() | parent.isSampled()| Invoke sampler|
|--|--|--|--|
|absent| n/a | n/a |`root()`|
|present|true|true|`remoteParentSampled()`|
|present|true|false|`remoteParentNotSampled()`|
|present|false|true|`localParentSampled()`|
|present|false|false|`localParentNotSampled()`|

```js
const { NodeTracerProvider } = require("@opentelemetry/sdk-trace-node");
const { ParentBasedSampler, AlwaysOffSampler, TraceIdRatioBasedSampler } = require("@opentelemetry/core");

const tracerProvider = new NodeTracerProvider({
sampler: new ParentBasedSampler({
// By default, the ParentBasedSampler will respect the parent span's sampling
// decision. This is configurable by providing a different sampler to use
// based on the situation. See configuration details above.
//
// This will delegate the sampling decision of all root traces (no parent)
// to the TraceIdRatioBasedSampler.
// See details of TraceIdRatioBasedSampler above.
root: new TraceIdRatioBasedSampler(0.5)
})
});
```

## Example

See [examples/basic-tracer-node](https://github.com/open-telemetry/opentelemetry-js/tree/main/examples/basic-tracer-node) for an end-to-end example, including exporting created spans.
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -31,7 +31,7 @@ import {
} from '@opentelemetry/core';
import { Resource } from '@opentelemetry/resources';
import { SpanProcessor, Tracer } from '.';
import { DEFAULT_CONFIG } from './config';
import { loadDefaultConfig } from './config';
import { MultiSpanProcessor } from './MultiSpanProcessor';
import { NoopSpanProcessor } from './export/NoopSpanProcessor';
import { SDKRegistrationConfig, TracerConfig } from './types';
Expand Down Expand Up @@ -74,7 +74,7 @@ export class BasicTracerProvider implements TracerProvider {
readonly resource: Resource;

constructor(config: TracerConfig = {}) {
const mergedConfig = merge({}, DEFAULT_CONFIG, reconfigureLimits(config));
const mergedConfig = merge({}, loadDefaultConfig(), reconfigureLimits(config));
this.resource = mergedConfig.resource ?? Resource.empty();
this.resource = Resource.default().merge(this.resource);
this._config = Object.assign({}, mergedConfig, {
Expand Down
Loading

0 comments on commit 355fc6a

Please sign in to comment.