Skip to content

Commit

Permalink
Merge branch 'main' into cartermp/active-baggage
Browse files Browse the repository at this point in the history
  • Loading branch information
vmarchaud authored Nov 20, 2022
2 parents 75ca792 + b29deee commit aa9abcf
Show file tree
Hide file tree
Showing 26 changed files with 169 additions and 102 deletions.
2 changes: 1 addition & 1 deletion .github/workflows/close-stale.yml
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,7 @@ jobs:
stale:
runs-on: ubuntu-latest
steps:
- uses: actions/stale@v5
- uses: actions/stale@v6
with:
days-before-stale: 60
days-before-close: 14
Expand Down
7 changes: 7 additions & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -15,6 +15,13 @@ For experimental package changes, see the [experimental CHANGELOG](experimental/

### :bug: (Bug Fix)

* fix(sdk-metrics): use default Resource to comply with semantic conventions [#3411](https://github.com/open-telemetry/opentelemetry-js/pull/3411) @pichlermarc
* Metrics exported by the SDK now contain the following resource attributes by default:
* `service.name`
* `telemetry.sdk.name`
* `telemetry.sdk.language`
* `telemetry.sdk.version`

### :books: (Refine Doc)

### :house: (Internal)
Expand Down
5 changes: 5 additions & 0 deletions api/CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,11 @@

All notable changes to this project will be documented in this file.

## Unreleased

* fix(api): deprecate MetricAttributes and MetricAttributeValue [#3406](https://github.com/open-telemetry/opentelemetry-js/pull/3406) @blumamir
* test(api): disable module concatenation in tree-shaking test [#3409](https://github.com/open-telemetry/opentelemetry-js/pull/3409) @legendecas

## [1.3.0](https://www.github.com/open-telemetry/opentelemetry-js-api/compare/v1.2.0...v1.3.0)

* feat(api): merge api-metrics into api [#3374](https://github.com/open-telemetry/opentelemetry-js/pull/3374) @legendecas
Expand Down
10 changes: 2 additions & 8 deletions api/src/metrics/Metric.ts
Original file line number Diff line number Diff line change
Expand Up @@ -83,19 +83,13 @@ export interface Histogram<AttributesTypes extends MetricAttributes = MetricAttr
record(value: number, attributes?: AttributesTypes, context?: Context): void;
}

// api.SpanAttributes instead of api.Attributes is used here for api package backward compatibility.
/**
* Attributes is a map from string to attribute values.
*
* Note: only the own enumerable keys are counted as valid attribute keys.
* @deprecated please use {@link Attributes}
*/
export type MetricAttributes = Attributes;

// api.SpanAttributeValue instead of api.AttributeValue is used here for api package backward compatibility.
/**
* 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.
* @deprecated please use {@link AttributeValue}
*/
export type MetricAttributeValue = AttributeValue;

Expand Down
2 changes: 2 additions & 0 deletions api/test/tree-shaking/tree-shaking.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -74,6 +74,8 @@ describe('tree-shaking', () => {
optimization: {
// disable minimization so that we can inspect the output easily.
minimize: false,
// disable module concatenation so that variable names will not be mangled.
concatenateModules: false,
}
});

Expand Down
3 changes: 3 additions & 0 deletions experimental/CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,9 @@ All notable changes to experimental packages in this project will be documented

### :rocket: (Enhancement)

* feat(instrumentation-http): monitor error events with events.errorMonitor [#3402](https://github.com/open-telemetry/opentelemetry-js/pull/3402) @legendecas
* feat(instrumentation): add new `_setMeterInstruments` protected method that update the meter instruments every meter provider update.

### :bug: (Bug Fix)

### :books: (Refine Doc)
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -37,11 +37,11 @@ class TestMetricReader extends MetricReader {
}
}

const testResource = Resource.default().merge(new Resource({
const testResource = new Resource({
service: 'ui',
version: 1,
cost: 112.12,
}));
});

let meterProvider = new MeterProvider({ resource: testResource });

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -42,11 +42,11 @@ export class TestMetricReader extends MetricReader {
}
}

const testResource = Resource.default().merge(new Resource({
const testResource = new Resource({
service: 'ui',
version: 1,
cost: 112.12,
}));
});

let meterProvider = new MeterProvider({ resource: testResource });

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -44,6 +44,7 @@
},
"devDependencies": {
"@opentelemetry/api": "^1.3.0",
"@opentelemetry/semantic-conventions": "1.8.0",
"@types/mocha": "10.0.0",
"@types/node": "18.6.5",
"@types/sinon": "10.0.13",
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -26,14 +26,20 @@ import * as http from 'http';
import { PrometheusExporter } from '../src';
import {
mockedHrTimeMs,
mockHrTime
mockHrTime,
sdkLanguage,
sdkName,
sdkVersion,
serviceName
} from './util';
import { SinonStubbedInstance } from 'sinon';

const serializedEmptyResourceLines = [
const infoLine = `target_info{service_name="${serviceName}",telemetry_sdk_language="${sdkLanguage}",telemetry_sdk_name="${sdkName}",telemetry_sdk_version="${sdkVersion}"} 1`;

const serializedDefaultResourceLines = [
'# HELP target_info Target metadata',
'# TYPE target_info gauge',
'target_info 1'
infoLine
];

describe('PrometheusExporter', () => {
Expand Down Expand Up @@ -261,12 +267,12 @@ describe('PrometheusExporter', () => {
const lines = body.split('\n');

assert.strictEqual(
lines[serializedEmptyResourceLines.length],
lines[serializedDefaultResourceLines.length],
'# HELP counter_total a test description'
);

assert.deepStrictEqual(lines, [
...serializedEmptyResourceLines,
...serializedDefaultResourceLines,
'# HELP counter_total a test description',
'# TYPE counter_total counter',
`counter_total{key1="attributeValue1"} 10 ${mockedHrTimeMs}`,
Expand Down Expand Up @@ -296,7 +302,7 @@ describe('PrometheusExporter', () => {
const lines = body.split('\n');

assert.deepStrictEqual(lines, [
...serializedEmptyResourceLines,
...serializedDefaultResourceLines,
'# HELP metric_observable_gauge a test description',
'# TYPE metric_observable_gauge gauge',
`metric_observable_gauge{pid="123",core="1"} 0.999 ${mockedHrTimeMs}`,
Expand All @@ -316,7 +322,7 @@ describe('PrometheusExporter', () => {
const lines = body.split('\n');

assert.deepStrictEqual(lines, [
...serializedEmptyResourceLines,
...serializedDefaultResourceLines,
'# HELP counter_total a test description',
'# TYPE counter_total counter',
`counter_total{counterKey1="attributeValue1"} 10 ${mockedHrTimeMs}`,
Expand Down Expand Up @@ -351,7 +357,7 @@ describe('PrometheusExporter', () => {
const lines = body.split('\n');

assert.deepStrictEqual(lines, [
...serializedEmptyResourceLines,
...serializedDefaultResourceLines,
'# no registered metrics'
]);
});
Expand All @@ -365,7 +371,7 @@ describe('PrometheusExporter', () => {
const lines = body.split('\n');

assert.deepStrictEqual(lines, [
...serializedEmptyResourceLines,
...serializedDefaultResourceLines,
'# HELP counter_total description missing',
'# TYPE counter_total counter',
`counter_total{key1="attributeValue1"} 10 ${mockedHrTimeMs}`,
Expand All @@ -382,7 +388,7 @@ describe('PrometheusExporter', () => {
const lines = body.split('\n');

assert.deepStrictEqual(lines, [
...serializedEmptyResourceLines,
...serializedDefaultResourceLines,
'# HELP counter_bad_name_total description missing',
'# TYPE counter_bad_name_total counter',
`counter_bad_name_total{key1="attributeValue1"} 10 ${mockedHrTimeMs}`,
Expand All @@ -400,7 +406,7 @@ describe('PrometheusExporter', () => {
const body = await request('http://localhost:9464/metrics');
const lines = body.split('\n');
assert.deepStrictEqual(lines, [
...serializedEmptyResourceLines,
...serializedDefaultResourceLines,
'# HELP counter a test description',
'# TYPE counter gauge',
`counter{key1="attributeValue1"} 20 ${mockedHrTimeMs}`,
Expand Down Expand Up @@ -429,7 +435,7 @@ describe('PrometheusExporter', () => {
const lines = body.split('\n');

assert.deepStrictEqual(lines, [
...serializedEmptyResourceLines,
...serializedDefaultResourceLines,
'# HELP metric_observable_counter a test description',
'# TYPE metric_observable_counter counter',
`metric_observable_counter{key1="attributeValue1"} 20 ${mockedHrTimeMs}`,
Expand Down Expand Up @@ -458,7 +464,7 @@ describe('PrometheusExporter', () => {
const lines = body.split('\n');

assert.deepStrictEqual(lines, [
...serializedEmptyResourceLines,
...serializedDefaultResourceLines,
'# HELP metric_observable_up_down_counter a test description',
'# TYPE metric_observable_up_down_counter gauge',
`metric_observable_up_down_counter{key1="attributeValue1"} 20 ${mockedHrTimeMs}`,
Expand All @@ -477,7 +483,7 @@ describe('PrometheusExporter', () => {
const lines = body.split('\n');

assert.deepStrictEqual(lines, [
...serializedEmptyResourceLines,
...serializedDefaultResourceLines,
'# HELP test_histogram a test description',
'# TYPE test_histogram histogram',
`test_histogram_count{key1="attributeValue1"} 1 ${mockedHrTimeMs}`,
Expand Down Expand Up @@ -531,7 +537,7 @@ describe('PrometheusExporter', () => {
const lines = body.split('\n');

assert.deepStrictEqual(lines, [
...serializedEmptyResourceLines,
...serializedDefaultResourceLines,
'# HELP test_prefix_counter_total description missing',
'# TYPE test_prefix_counter_total counter',
`test_prefix_counter_total{key1="attributeValue1"} 10 ${mockedHrTimeMs}`,
Expand Down Expand Up @@ -560,7 +566,7 @@ describe('PrometheusExporter', () => {
const lines = body.split('\n');

assert.deepStrictEqual(lines, [
...serializedEmptyResourceLines,
...serializedDefaultResourceLines,
'# HELP counter_total description missing',
'# TYPE counter_total counter',
`counter_total{key1="attributeValue1"} 10 ${mockedHrTimeMs}`,
Expand Down Expand Up @@ -589,7 +595,7 @@ describe('PrometheusExporter', () => {
const lines = body.split('\n');

assert.deepStrictEqual(lines, [
...serializedEmptyResourceLines,
...serializedDefaultResourceLines,
'# HELP counter_total description missing',
'# TYPE counter_total counter',
`counter_total{key1="attributeValue1"} 10 ${mockedHrTimeMs}`,
Expand Down Expand Up @@ -618,7 +624,7 @@ describe('PrometheusExporter', () => {
const lines = body.split('\n');

assert.deepStrictEqual(lines, [
...serializedEmptyResourceLines,
...serializedDefaultResourceLines,
'# HELP counter_total description missing',
'# TYPE counter_total counter',
'counter_total{key1="attributeValue1"} 10',
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -36,7 +36,11 @@ import * as sinon from 'sinon';
import { PrometheusSerializer } from '../src';
import {
mockedHrTimeMs,
mockHrTime
mockHrTime,
sdkLanguage,
sdkName,
sdkVersion,
serviceName
} from './util';
import { Resource } from '@opentelemetry/resources';

Expand All @@ -45,10 +49,10 @@ const attributes = {
foo2: 'bar2',
};

const serializedEmptyResource =
const serializedDefaultResource =
'# HELP target_info Target metadata\n' +
'# TYPE target_info gauge\n' +
'target_info 1\n';
`target_info{service_name="${serviceName}",telemetry_sdk_language="${sdkLanguage}",telemetry_sdk_name="${sdkName}",telemetry_sdk_version="${sdkVersion}"} 1\n`;

class TestMetricReader extends MetricReader {
constructor() {
Expand Down Expand Up @@ -477,7 +481,7 @@ describe('PrometheusSerializer', () => {
const result = await getCounterResult('test', serializer, { unit: unitOfMetric, exportAll: true });
assert.strictEqual(
result,
serializedEmptyResource +
serializedDefaultResource +
'# HELP test_total description missing\n' +
`# UNIT test_total ${unitOfMetric}\n` +
'# TYPE test_total counter\n' +
Expand All @@ -491,7 +495,7 @@ describe('PrometheusSerializer', () => {
const result = await getCounterResult('test', serializer, { exportAll: true });
assert.strictEqual(
result,
serializedEmptyResource +
serializedDefaultResource +
'# HELP test_total description missing\n' +
'# TYPE test_total counter\n' +
`test_total 1 ${mockedHrTimeMs}\n`
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -16,6 +16,8 @@

import * as sinon from 'sinon';
import * as perf_hooks from 'perf_hooks';
import { Resource } from '@opentelemetry/resources';
import { SemanticResourceAttributes } from '@opentelemetry/semantic-conventions';

export const mockedHrTimeMs = 1586347902211;

Expand All @@ -25,3 +27,12 @@ export function mockHrTime() {
sinon.stub(perf_hooks.performance, 'timeOrigin').value(0);
sinon.stub(perf_hooks.performance, 'now').returns(mockedHrTimeMs);
}

export const serviceName = Resource.default().attributes[SemanticResourceAttributes.SERVICE_NAME]?.toString()
.replace(/\\/g, '\\\\').replace(/\n/g, '\\n');
export const sdkLanguage = Resource.default().attributes[SemanticResourceAttributes.TELEMETRY_SDK_LANGUAGE]?.toString()
.replace(/\\/g, '\\\\').replace(/\n/g, '\\n');
export const sdkName = Resource.default().attributes[SemanticResourceAttributes.TELEMETRY_SDK_NAME]?.toString()
.replace(/\\/g, '\\\\').replace(/\n/g, '\\n');
export const sdkVersion = Resource.default().attributes[SemanticResourceAttributes.TELEMETRY_SDK_VERSION]?.toString()
.replace(/\\/g, '\\\\').replace(/\n/g, '\\n');
Original file line number Diff line number Diff line change
Expand Up @@ -18,6 +18,9 @@
{
"path": "../../../packages/opentelemetry-resources"
},
{
"path": "../../../packages/opentelemetry-semantic-conventions"
},
{
"path": "../../../packages/sdk-metrics"
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -26,9 +26,8 @@ import {
SpanStatusCode,
trace,
Histogram,
MeterProvider,
MetricAttributes,
ValueType,
ValueType
} from '@opentelemetry/api';
import { hrTime, hrTimeDuration, hrTimeToMilliseconds, suppressTracing } from '@opentelemetry/core';
import type * as http from 'http';
Expand All @@ -54,6 +53,7 @@ import {
safeExecuteInTheMiddle,
} from '@opentelemetry/instrumentation';
import { RPCMetadata, RPCType, setRPCMetadata } from '@opentelemetry/core';
import { errorMonitor } from 'events';

/**
* Http instrumentation instrumentation for Opentelemetry
Expand All @@ -73,15 +73,9 @@ export class HttpInstrumentation extends InstrumentationBase<Http> {
config
);
this._headerCapture = this._createHeaderCapture();
this._updateMetricInstruments();
}

override setMeterProvider(meterProvider: MeterProvider) {
super.setMeterProvider(meterProvider);
this._updateMetricInstruments();
}

private _updateMetricInstruments() {
protected override _updateMetricInstruments() {
this._httpServerDurationHistogram = this.meter.createHistogram('http.server.duration', {
description: 'measures the duration of the inbound HTTP requests',
unit: 'ms',
Expand Down Expand Up @@ -361,7 +355,7 @@ export class HttpInstrumentation extends InstrumentationBase<Http> {

this._closeHttpSpan(span, SpanKind.CLIENT, startTime, metricAttributes);
});
response.on('error', (error: Err) => {
response.on(errorMonitor, (error: Err) => {
this._diag.debug('outgoingRequest on error()', error);
utils.setSpanWithError(span, error);
const code = utils.parseResponseStatus(SpanKind.CLIENT, response.statusCode);
Expand All @@ -376,7 +370,7 @@ export class HttpInstrumentation extends InstrumentationBase<Http> {
this._closeHttpSpan(span, SpanKind.CLIENT, startTime, metricAttributes);
}
});
request.on('error', (error: Err) => {
request.on(errorMonitor, (error: Err) => {
this._diag.debug('outgoingRequest on request error()', error);
utils.setSpanWithError(span, error);
this._closeHttpSpan(span, SpanKind.CLIENT, startTime, metricAttributes);
Expand Down
Loading

0 comments on commit aa9abcf

Please sign in to comment.