Skip to content

Commit 517a31a

Browse files
pirgeodyladan
andauthored
feat: Rename Labels to Attributes (#2523)
Co-authored-by: Daniel Dyla <[email protected]>
1 parent 28c9e88 commit 517a31a

30 files changed

+278
-278
lines changed

experimental/packages/opentelemetry-api-metrics/src/NoopMeter.ts

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -17,7 +17,7 @@
1717
import { Meter } from './types/Meter';
1818
import {
1919
MetricOptions,
20-
Labels,
20+
Attributes,
2121
Counter,
2222
Histogram,
2323
ObservableGauge,
@@ -109,15 +109,15 @@ export class NoopMeter implements Meter {
109109
export class NoopMetric {}
110110

111111
export class NoopCounterMetric extends NoopMetric implements Counter {
112-
add(_value: number, _labels: Labels): void {}
112+
add(_value: number, _attributes: Attributes): void {}
113113
}
114114

115115
export class NoopUpDownCounterMetric extends NoopMetric implements UpDownCounter {
116-
add(_value: number, _labels: Labels): void {}
116+
add(_value: number, _attributes: Attributes): void {}
117117
}
118118

119119
export class NoopHistogramMetric extends NoopMetric implements Histogram {
120-
record(_value: number, _labels: Labels): void {}
120+
record(_value: number, _attributes: Attributes): void {}
121121
}
122122

123123
export class NoopObservableBaseMetric extends NoopMetric implements ObservableBase {

experimental/packages/opentelemetry-api-metrics/src/types/Meter.ts

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -39,7 +39,7 @@ export interface MeterOptions {
3939
* An interface to allow the recording metrics.
4040
*
4141
* {@link Metric}s are used for recording pre-defined aggregation (`Counter`),
42-
* or raw values (`Histogram`) in which the aggregation and labels
42+
* or raw values (`Histogram`) in which the aggregation and attributes
4343
* for the exported metric are deferred.
4444
*/
4545
export interface Meter {

experimental/packages/opentelemetry-api-metrics/src/types/Metric.ts

Lines changed: 9 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -37,8 +37,8 @@ export interface MetricOptions {
3737
*/
3838
unit?: string;
3939

40-
/** The map of constant labels for the Metric. */
41-
constantLabels?: Map<string, string>;
40+
/** The map of constant attributes for the Metric. */
41+
constantAttributes?: Map<string, string>;
4242

4343
/**
4444
* Indicates the metric is a verbose metric that is disabled by default
@@ -93,30 +93,30 @@ export enum AggregationTemporality {
9393
*/
9494
export interface Counter {
9595
/**
96-
* Adds the given value to the current value. Values cannot be negative.
96+
* Increment value of counter by the input. Inputs may not be negative.
9797
*/
98-
add(value: number, labels?: Labels): void;
98+
add(value: number, attributes?: Attributes): void;
9999
}
100100

101101
export interface UpDownCounter {
102102
/**
103-
* Adds the given value to the current value. Values can be negative.
103+
* Increment value of counter by the input. Inputs may be negative.
104104
*/
105-
add(value: number, labels?: Labels): void;
105+
add(value: number, attributes?: Attributes): void;
106106
}
107107

108108
export interface Histogram {
109109
/**
110110
* Records the given value to this histogram.
111111
*/
112-
record(value: number, labels?: Labels): void;
112+
record(value: number, attributes?: Attributes): void;
113113
}
114114

115115
/** Base interface for the Observable metrics. */
116116
export interface ObservableBase {
117117
observation: (
118118
value: number,
119-
labels?: Labels,
119+
attributes?: Attributes,
120120
) => Observation;
121121
}
122122

@@ -132,4 +132,4 @@ export type ObservableCounter = ObservableBase;
132132
/**
133133
* key-value pairs passed by the user.
134134
*/
135-
export type Labels = { [key: string]: string };
135+
export type Attributes = { [key: string]: string };

experimental/packages/opentelemetry-api-metrics/src/types/ObservableResult.ts

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -14,11 +14,11 @@
1414
* limitations under the License.
1515
*/
1616

17-
import { Labels } from './Metric';
17+
import { Attributes } from './Metric';
1818

1919
/**
2020
* Interface that is being used in callback function for Observable Metric
2121
*/
2222
export interface ObservableResult {
23-
observe(value: number, labels: Labels): void;
23+
observe(value: number, attributes: Attributes): void;
2424
}

experimental/packages/opentelemetry-api-metrics/test/noop-implementations/noop-meter.test.ts

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -25,16 +25,16 @@ describe('NoopMeter', () => {
2525
it('should not crash', () => {
2626
const meter = new NoopMeterProvider().getMeter('test-noop');
2727
const counter = meter.createCounter('some-name');
28-
const labels = {};
28+
const attributes = {};
2929

3030
// ensure NoopMetric does not crash.
31-
counter.add(1, labels);
31+
counter.add(1, attributes);
3232

3333
// ensure the correct noop const is returned
3434
assert.strictEqual(counter, NOOP_COUNTER_METRIC);
3535

3636
const histogram = meter.createHistogram('some-name');
37-
histogram.record(1, labels);
37+
histogram.record(1, attributes);
3838

3939
// ensure the correct noop const is returned
4040
assert.strictEqual(histogram, NOOP_HISTOGRAM_METRIC);

experimental/packages/opentelemetry-exporter-metrics-otlp-http/src/transformMetrics.ts

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -15,7 +15,7 @@
1515
*/
1616

1717
import { SpanAttributes, HrTime } from '@opentelemetry/api';
18-
import { Labels, ValueType } from '@opentelemetry/api-metrics';
18+
import { Attributes as Labels, ValueType } from '@opentelemetry/api-metrics';
1919
import * as core from '@opentelemetry/core';
2020
import {
2121
AggregatorKind,
@@ -54,7 +54,7 @@ export function toAggregationTemporality(
5454
}
5555

5656
/**
57-
* Returns an DataPoint which can have integers or doublle values
57+
* Returns an DataPoint which can have integers or double values
5858
* @param metric
5959
* @param startTime
6060
*/
@@ -63,7 +63,7 @@ export function toDataPoint(
6363
startTime: number
6464
): otlpTypes.opentelemetryProto.metrics.v1.DataPoint {
6565
return {
66-
labels: toCollectorLabels(metric.labels),
66+
labels: toCollectorLabels(metric.attributes),
6767
value: metric.aggregator.toPoint().value as number,
6868
startTimeUnixNano: startTime,
6969
timeUnixNano: core.hrTimeToNanoseconds(
@@ -86,7 +86,7 @@ export function toHistogramPoint(
8686
timestamp: HrTime;
8787
};
8888
return {
89-
labels: toCollectorLabels(metric.labels),
89+
labels: toCollectorLabels(metric.attributes),
9090
sum: value.sum,
9191
count: value.count,
9292
startTimeUnixNano: startTime,

experimental/packages/opentelemetry-exporter-metrics-otlp-http/test/common/transformMetrics.test.ts

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -158,7 +158,7 @@ describe('transformMetrics', () => {
158158
);
159159
});
160160

161-
it('should convert metric labels value to string', () => {
161+
it('should convert metric attributes value to string', () => {
162162
const metric = transform.toCollectorMetric(
163163
{
164164
descriptor: {
@@ -168,7 +168,7 @@ describe('transformMetrics', () => {
168168
metricKind: 0,
169169
valueType: 0,
170170
},
171-
labels: { foo: (1 as unknown) as string },
171+
attributes: { foo: (1 as unknown) as string },
172172
aggregator: new SumAggregator(),
173173
resource: new Resource({}),
174174
aggregationTemporality: 0,

experimental/packages/opentelemetry-exporter-prometheus/src/PrometheusLabelsBatcher.ts renamed to experimental/packages/opentelemetry-exporter-prometheus/src/PrometheusAttributesBatcher.ts

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -26,7 +26,7 @@ interface BatcherCheckpoint {
2626
records: Map<string, MetricRecord>;
2727
}
2828

29-
export class PrometheusLabelsBatcher {
29+
export class PrometheusAttributesBatcher {
3030
private _batchMap = new Map<string, BatcherCheckpoint>();
3131

3232
get hasMetric(): boolean {
@@ -45,10 +45,10 @@ export class PrometheusLabelsBatcher {
4545
this._batchMap.set(name, item);
4646
}
4747
const recordMap = item.records;
48-
const labels = Object.keys(record.labels)
49-
.map(k => `${k}=${record.labels[k]}`)
48+
const attributes = Object.keys(record.attributes)
49+
.map(k => `${k}=${record.attributes[k]}`)
5050
.join(',');
51-
recordMap.set(labels, record);
51+
recordMap.set(attributes, record);
5252
}
5353

5454
checkPointSet(): PrometheusCheckpoint[] {

experimental/packages/opentelemetry-exporter-prometheus/src/PrometheusExporter.ts

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -25,7 +25,7 @@ import { createServer, IncomingMessage, Server, ServerResponse } from 'http';
2525
import * as url from 'url';
2626
import { ExporterConfig } from './export/types';
2727
import { PrometheusSerializer } from './PrometheusSerializer';
28-
import { PrometheusLabelsBatcher } from './PrometheusLabelsBatcher';
28+
import { PrometheusAttributesBatcher } from './PrometheusAttributesBatcher';
2929

3030
export class PrometheusExporter implements MetricExporter {
3131
static readonly DEFAULT_OPTIONS = {
@@ -43,10 +43,10 @@ export class PrometheusExporter implements MetricExporter {
4343
private readonly _prefix?: string;
4444
private readonly _appendTimestamp: boolean;
4545
private _serializer: PrometheusSerializer;
46-
private _batcher = new PrometheusLabelsBatcher();
46+
private _batcher = new PrometheusAttributesBatcher();
4747

4848
// This will be required when histogram is implemented. Leaving here so it is not forgotten
49-
// Histogram cannot have a label named 'le'
49+
// Histogram cannot have a attribute named 'le'
5050
// private static readonly RESERVED_HISTOGRAM_LABEL = 'le';
5151

5252
/**

experimental/packages/opentelemetry-exporter-prometheus/src/PrometheusSerializer.ts

Lines changed: 25 additions & 25 deletions
Original file line numberDiff line numberDiff line change
@@ -19,7 +19,7 @@ import {
1919
MetricKind,
2020
} from '@opentelemetry/sdk-metrics-base';
2121
import { PrometheusCheckpoint } from './types';
22-
import { Labels } from '@opentelemetry/api-metrics';
22+
import { Attributes } from '@opentelemetry/api-metrics';
2323
import { hrTimeToMilliseconds } from '@opentelemetry/core';
2424

2525
type PrometheusDataTypeLiteral =
@@ -33,7 +33,7 @@ function escapeString(str: string) {
3333
return str.replace(/\\/g, '\\\\').replace(/\n/g, '\\n');
3434
}
3535

36-
function escapeLabelValue(str: string) {
36+
function escapeAttributeValue(str: string) {
3737
if (typeof str !== 'string') {
3838
str = String(str);
3939
}
@@ -45,7 +45,7 @@ const invalidCharacterRegex = /[^a-z0-9_]/gi;
4545
* Ensures metric names are valid Prometheus metric names by removing
4646
* characters allowed by OpenTelemetry but disallowed by Prometheus.
4747
*
48-
* https://prometheus.io/docs/concepts/data_model/#metric-names-and-labels
48+
* https://prometheus.io/docs/concepts/data_model/#metric-names-and-attributes
4949
*
5050
* 1. Names must match `[a-zA-Z_:][a-zA-Z0-9_:]*`
5151
*
@@ -123,33 +123,33 @@ function toPrometheusType(
123123

124124
function stringify(
125125
metricName: string,
126-
labels: Labels,
126+
attributes: Attributes,
127127
value: number,
128128
timestamp?: number,
129-
additionalLabels?: Labels
129+
additionalAttributes?: Attributes
130130
) {
131-
let hasLabel = false;
132-
let labelsStr = '';
131+
let hasAttribute = false;
132+
let attributesStr = '';
133133

134-
for (const [key, val] of Object.entries(labels)) {
135-
const sanitizedLabelName = sanitizePrometheusMetricName(key);
136-
hasLabel = true;
137-
labelsStr += `${
138-
labelsStr.length > 0 ? ',' : ''
139-
}${sanitizedLabelName}="${escapeLabelValue(val)}"`;
134+
for (const [key, val] of Object.entries(attributes)) {
135+
const sanitizedAttributeName = sanitizePrometheusMetricName(key);
136+
hasAttribute = true;
137+
attributesStr += `${
138+
attributesStr.length > 0 ? ',' : ''
139+
}${sanitizedAttributeName}="${escapeAttributeValue(val)}"`;
140140
}
141-
if (additionalLabels) {
142-
for (const [key, val] of Object.entries(additionalLabels)) {
143-
const sanitizedLabelName = sanitizePrometheusMetricName(key);
144-
hasLabel = true;
145-
labelsStr += `${
146-
labelsStr.length > 0 ? ',' : ''
147-
}${sanitizedLabelName}="${escapeLabelValue(val)}"`;
141+
if (additionalAttributes) {
142+
for (const [key, val] of Object.entries(additionalAttributes)) {
143+
const sanitizedAttributeName = sanitizePrometheusMetricName(key);
144+
hasAttribute = true;
145+
attributesStr += `${
146+
attributesStr.length > 0 ? ',' : ''
147+
}${sanitizedAttributeName}="${escapeAttributeValue(val)}"`;
148148
}
149149
}
150150

151-
if (hasLabel) {
152-
metricName += `{${labelsStr}}`;
151+
if (hasAttribute) {
152+
metricName += `{${attributesStr}}`;
153153
}
154154

155155
return `${metricName} ${valueString(value)}${
@@ -219,7 +219,7 @@ export class PrometheusSerializer {
219219
const timestamp = hrTimeToMilliseconds(hrtime);
220220
results += stringify(
221221
name,
222-
record.labels,
222+
record.attributes,
223223
value,
224224
this._appendTimestamp ? timestamp : undefined,
225225
undefined
@@ -233,7 +233,7 @@ export class PrometheusSerializer {
233233
for (const key of ['count', 'sum'] as ('count' | 'sum')[]) {
234234
results += stringify(
235235
name + '_' + key,
236-
record.labels,
236+
record.attributes,
237237
value[key],
238238
this._appendTimestamp ? timestamp : undefined,
239239
undefined
@@ -260,7 +260,7 @@ export class PrometheusSerializer {
260260
}
261261
results += stringify(
262262
name + '_bucket',
263-
record.labels,
263+
record.attributes,
264264
cumulativeSum,
265265
this._appendTimestamp ? timestamp : undefined,
266266
{

0 commit comments

Comments
 (0)