Skip to content
Draft
30 changes: 30 additions & 0 deletions api/src/common/AnyValue.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,30 @@
/*
* Copyright The OpenTelemetry Authors
* SPDX-License-Identifier: Apache-2.0
*/

/**
* AnyValue can be:
* - a string, number, boolean value
* - a byte array (Uint8Array)
* - array of any value
* - map from string to any value
* - empty value
*
* https://opentelemetry.io/docs/specs/otel/common/#anyvalue
*
* @since 1.3.0
*/
export type AnyValue =
| string
| number
| boolean
| Uint8Array
| Array<AnyValue>
| AnyValueMap
| null
| undefined;

interface AnyValueMap {
[attributeKey: string]: AnyValue;
}
23 changes: 7 additions & 16 deletions api/src/common/Attributes.ts
Original file line number Diff line number Diff line change
Expand Up @@ -3,28 +3,19 @@
* SPDX-License-Identifier: Apache-2.0
*/

import type { AnyValue } from './AnyValue';

/**
* Attributes is a map from string to attribute values.
*
* Note: only the own enumerable keys are counted as valid attribute keys.
* Attributes is a mapping from string to attribute values.
* https://opentelemetry.io/docs/specs/otel/common/#attribute-collections
*
* @since 1.3.0
*/
export interface Attributes {
[attributeKey: string]: AttributeValue | undefined;
[attributeKey: string]: AnyValue;
}

/**
* Attribute values may be any non-nullish primitive value except an object.
*
* null or undefined attribute values are invalid and will result in undefined behavior.
*
* @since 1.3.0
* @deprecated use AnyValue
*/
export type AttributeValue =
| string
| number
| boolean
| Array<null | undefined | string>
| Array<null | undefined | number>
| Array<null | undefined | boolean>;
export type AttributeValue = AnyValue;
13 changes: 8 additions & 5 deletions api/src/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -15,6 +15,7 @@ export type {
export { baggageEntryMetadataFromString } from './baggage/utils';
export type { Exception } from './common/Exception';
export type { HrTime, TimeInput } from './common/Time';
export type { AnyValue } from './common/AnyValue';
export type { Attributes, AttributeValue } from './common/Attributes';

// Context APIs
Expand Down Expand Up @@ -71,13 +72,8 @@ export type {
export type { PropagationAPI } from './api/propagation';

// Trace APIs
export type { SpanAttributes, SpanAttributeValue } from './trace/attributes';
export type { Link } from './trace/link';
export { ProxyTracer, type TracerDelegator } from './trace/ProxyTracer';
// TODO: Remove ProxyTracerProvider export in the next major version.
export { ProxyTracerProvider } from './trace/ProxyTracerProvider';
export type { Sampler } from './trace/Sampler';
export { SamplingDecision, type SamplingResult } from './trace/SamplingResult';
export type { SpanContext } from './trace/span_context';
export { SpanKind } from './trace/span_kind';
export type { Span } from './trace/span';
Expand All @@ -101,6 +97,13 @@ export {
} from './trace/invalid-span-constants';
export type { TraceAPI } from './api/trace';

// Deprecated Trace APIs
export type { SpanAttributes, SpanAttributeValue } from './trace/attributes';
// TODO: Remove ProxyTracerProvider export in the next major version.
export { ProxyTracerProvider } from './trace/ProxyTracerProvider';
export type { Sampler } from './trace/Sampler';
export { SamplingDecision, type SamplingResult } from './trace/SamplingResult';
Copy link
Copy Markdown
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

This belongs in a separate PR, if at all. I did this as I was working through the full API to grok usage of Attributes/AnyValue.


// Split module-level variable definition into separate files to allow
// tree-shaking on each api instance.
import { context } from './context-api';
Expand Down
20 changes: 10 additions & 10 deletions api/src/metrics/Meter.ts
Original file line number Diff line number Diff line change
Expand Up @@ -3,12 +3,12 @@
* SPDX-License-Identifier: Apache-2.0
*/

import type { Attributes } from '../common/Attributes';
import type {
BatchObservableCallback,
Counter,
Gauge,
Histogram,
MetricAttributes,
MetricOptions,
Observable,
ObservableCounter,
Expand Down Expand Up @@ -44,7 +44,7 @@ export interface Meter {
* @param name the name of the metric.
* @param [options] the metric options.
*/
createGauge<AttributesTypes extends MetricAttributes = MetricAttributes>(
createGauge<AttributesTypes extends Attributes = Attributes>(
Copy link
Copy Markdown
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Q: What is the <AttributesTypes extends ...> generic on Instruments for?

name: string,
options?: MetricOptions
): Gauge<AttributesTypes>;
Expand All @@ -54,7 +54,7 @@ export interface Meter {
* @param name the name of the metric.
* @param [options] the metric options.
*/
createHistogram<AttributesTypes extends MetricAttributes = MetricAttributes>(
createHistogram<AttributesTypes extends Attributes = Attributes>(
Copy link
Copy Markdown
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

There are a lot of s/{Metric,Span}Attributes/Attributes/ changes in the PR that could be done in a separate PR to make it smaller. I may end up separating them to help reviews.

name: string,
options?: MetricOptions
): Histogram<AttributesTypes>;
Expand All @@ -66,7 +66,7 @@ export interface Meter {
* @param name the name of the metric.
* @param [options] the metric options.
*/
createCounter<AttributesTypes extends MetricAttributes = MetricAttributes>(
createCounter<AttributesTypes extends Attributes = Attributes>(
name: string,
options?: MetricOptions
): Counter<AttributesTypes>;
Expand All @@ -89,7 +89,7 @@ export interface Meter {
* @param [options] the metric options.
*/
createUpDownCounter<
AttributesTypes extends MetricAttributes = MetricAttributes,
AttributesTypes extends Attributes = Attributes,
>(
name: string,
options?: MetricOptions
Expand All @@ -104,7 +104,7 @@ export interface Meter {
* @param [options] the metric options.
*/
createObservableGauge<
AttributesTypes extends MetricAttributes = MetricAttributes,
AttributesTypes extends Attributes = Attributes,
>(
name: string,
options?: MetricOptions
Expand All @@ -119,7 +119,7 @@ export interface Meter {
* @param [options] the metric options.
*/
createObservableCounter<
AttributesTypes extends MetricAttributes = MetricAttributes,
AttributesTypes extends Attributes = Attributes,
>(
name: string,
options?: MetricOptions
Expand All @@ -134,7 +134,7 @@ export interface Meter {
* @param [options] the metric options.
*/
createObservableUpDownCounter<
AttributesTypes extends MetricAttributes = MetricAttributes,
AttributesTypes extends Attributes = Attributes,
>(
name: string,
options?: MetricOptions
Expand All @@ -155,7 +155,7 @@ export interface Meter {
* @param observables the observables associated with this batch observable callback
*/
addBatchObservableCallback<
AttributesTypes extends MetricAttributes = MetricAttributes,
AttributesTypes extends Attributes = Attributes,
>(
callback: BatchObservableCallback<AttributesTypes>,
observables: Observable<AttributesTypes>[]
Expand All @@ -171,7 +171,7 @@ export interface Meter {
* @param observables the observables associated with this batch observable callback
*/
removeBatchObservableCallback<
AttributesTypes extends MetricAttributes = MetricAttributes,
AttributesTypes extends Attributes = Attributes,
>(
callback: BatchObservableCallback<AttributesTypes>,
observables: Observable<AttributesTypes>[]
Expand Down
29 changes: 15 additions & 14 deletions api/src/metrics/Metric.ts
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,8 @@
* SPDX-License-Identifier: Apache-2.0
*/

import type { Attributes, AttributeValue } from '../common/Attributes';
import type { AnyValue } from '../common/AnyValue';
import type { Attributes } from '../common/Attributes';
import type { Context } from '../context/types';

/**
Expand Down Expand Up @@ -80,7 +81,7 @@ export enum ValueType {
* @since 1.3.0
*/
export interface Counter<
AttributesTypes extends MetricAttributes = MetricAttributes,
AttributesTypes extends Attributes = Attributes,
> {
/**
* Increment value of counter by the input. Inputs must not be negative.
Expand All @@ -92,7 +93,7 @@ export interface Counter<
* @since 1.3.0
*/
export interface UpDownCounter<
AttributesTypes extends MetricAttributes = MetricAttributes,
AttributesTypes extends Attributes = Attributes,
> {
/**
* Increment value of counter by the input. Inputs may be negative.
Expand All @@ -104,7 +105,7 @@ export interface UpDownCounter<
* @since 1.9.0
*/
export interface Gauge<
AttributesTypes extends MetricAttributes = MetricAttributes,
AttributesTypes extends Attributes = Attributes,
> {
/**
* Records a measurement.
Expand All @@ -116,7 +117,7 @@ export interface Gauge<
* @since 1.3.0
*/
export interface Histogram<
AttributesTypes extends MetricAttributes = MetricAttributes,
AttributesTypes extends Attributes = Attributes,
> {
/**
* Records a measurement. Value of the measurement must not be negative.
Expand All @@ -134,15 +135,15 @@ export type MetricAttributes = Attributes;
* @deprecated please use {@link AttributeValue}
* @since 1.3.0
*/
export type MetricAttributeValue = AttributeValue;
export type MetricAttributeValue = AnyValue;

/**
* Interface that is being used in callback function for Observable Metric.
*
* @since 1.3.0
*/
export interface ObservableResult<
AttributesTypes extends MetricAttributes = MetricAttributes,
AttributesTypes extends Attributes = Attributes,
> {
/**
* Observe a measurement of the value associated with the given attributes.
Expand All @@ -163,7 +164,7 @@ export interface ObservableResult<
* Interface that is being used in batch observable callback function.
*/
export interface BatchObservableResult<
AttributesTypes extends MetricAttributes = MetricAttributes,
AttributesTypes extends Attributes = Attributes,
> {
/**
* Observe a measurement of the value associated with the given attributes.
Expand All @@ -188,7 +189,7 @@ export interface BatchObservableResult<
* @since 1.3.0
*/
export type ObservableCallback<
AttributesTypes extends MetricAttributes = MetricAttributes,
AttributesTypes extends Attributes = Attributes,
> = (
observableResult: ObservableResult<AttributesTypes>
) => void | Promise<void>;
Expand All @@ -199,7 +200,7 @@ export type ObservableCallback<
* @since 1.3.0
*/
export type BatchObservableCallback<
AttributesTypes extends MetricAttributes = MetricAttributes,
AttributesTypes extends Attributes = Attributes,
> = (
observableResult: BatchObservableResult<AttributesTypes>
) => void | Promise<void>;
Expand All @@ -208,7 +209,7 @@ export type BatchObservableCallback<
* @since 1.3.0
*/
export interface Observable<
AttributesTypes extends MetricAttributes = MetricAttributes,
AttributesTypes extends Attributes = Attributes,
> {
/**
* Sets up a function that will be called whenever a metric collection is initiated.
Expand All @@ -227,17 +228,17 @@ export interface Observable<
* @since 1.3.0
*/
export type ObservableCounter<
AttributesTypes extends MetricAttributes = MetricAttributes,
AttributesTypes extends Attributes = Attributes,
> = Observable<AttributesTypes>;
/**
* @since 1.3.0
*/
export type ObservableUpDownCounter<
AttributesTypes extends MetricAttributes = MetricAttributes,
AttributesTypes extends Attributes = Attributes,
> = Observable<AttributesTypes>;
/**
* @since 1.3.0
*/
export type ObservableGauge<
AttributesTypes extends MetricAttributes = MetricAttributes,
AttributesTypes extends Attributes = Attributes,
> = Observable<AttributesTypes>;
10 changes: 5 additions & 5 deletions api/src/metrics/NoopMeter.ts
Original file line number Diff line number Diff line change
Expand Up @@ -3,13 +3,13 @@
* SPDX-License-Identifier: Apache-2.0
*/

import type { Attributes } from '../common/Attributes';
import type { Meter } from './Meter';
import type {
BatchObservableCallback,
Counter,
Gauge,
Histogram,
MetricAttributes,
MetricOptions,
Observable,
ObservableCallback,
Expand Down Expand Up @@ -101,22 +101,22 @@ export class NoopMeter implements Meter {
export class NoopMetric {}

export class NoopCounterMetric extends NoopMetric implements Counter {
add(_value: number, _attributes: MetricAttributes): void {}
add(_value: number, _attributes: Attributes): void {}
}

export class NoopUpDownCounterMetric
extends NoopMetric
implements UpDownCounter
{
add(_value: number, _attributes: MetricAttributes): void {}
add(_value: number, _attributes: Attributes): void {}
}

export class NoopGaugeMetric extends NoopMetric implements Gauge {
record(_value: number, _attributes: MetricAttributes): void {}
record(_value: number, _attributes: Attributes): void {}
}

export class NoopHistogramMetric extends NoopMetric implements Histogram {
record(_value: number, _attributes: MetricAttributes): void {}
record(_value: number, _attributes: Attributes): void {}
}

export class NoopObservableMetric {
Expand Down
6 changes: 3 additions & 3 deletions api/src/trace/NonRecordingSpan.ts
Original file line number Diff line number Diff line change
Expand Up @@ -3,9 +3,9 @@
* SPDX-License-Identifier: Apache-2.0
*/

import type { Attributes } from '../common/Attributes';
import type { Exception } from '../common/Exception';
import type { TimeInput } from '../common/Time';
import type { SpanAttributes } from './attributes';
import { INVALID_SPAN_CONTEXT } from './invalid-span-constants';
import type { Span } from './span';
import type { SpanContext } from './span_context';
Expand Down Expand Up @@ -35,12 +35,12 @@ export class NonRecordingSpan implements Span {
}

// By default does nothing
setAttributes(_attributes: SpanAttributes): this {
setAttributes(_attributes: Attributes): this {
return this;
}

// By default does nothing
addEvent(_name: string, _attributes?: SpanAttributes): this {
addEvent(_name: string, _attributes?: Attributes): this {
return this;
}

Expand Down
Loading
Loading