Skip to content

Commit

Permalink
Merge branch 'main' into rename-event-api
Browse files Browse the repository at this point in the history
  • Loading branch information
martinkuba committed Mar 25, 2024
2 parents c1e17d2 + 97af8e6 commit 80901be
Show file tree
Hide file tree
Showing 21 changed files with 280 additions and 222 deletions.
2 changes: 2 additions & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,8 @@ For experimental package changes, see the [experimental CHANGELOG](experimental/

### :rocket: (Enhancement)

* perf(sdk-trace-base): do not allocate arrays if resource has no pending async attributes

### :bug: (Bug Fix)

* fix(sdk-metrics): increase the depth of the output to the console such that objects in the metric are printed fully to the console [#4522](https://github.com/open-telemetry/opentelemetry-js/pull/4522) @JacksonWeber
Expand Down
4 changes: 4 additions & 0 deletions experimental/CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -19,10 +19,14 @@ All notable changes to experimental packages in this project will be documented
* was used internally to keep track of the service client used by the exporter, as a side effect it allowed end-users to modify the gRPC service client that was used
* `compression`
* was used internally to keep track of the compression to use but was unintentionally exposed to the users. It allowed to read and write the value, writing, however, would have no effect.
* feat(api-events)!: removed domain from the Events API [#4569](https://github.com/open-telemetry/opentelemetry-js/pull/4569)
* fix(events-api)!: renamed EventEmitter to EventLogger in the Events API [#4569](https://github.com/open-telemetry/opentelemetry-js/pull/4568)

### :rocket: (Enhancement)

* refactor(instr-http): use exported strings for semconv. [#4573](https://github.com/open-telemetry/opentelemetry-js/pull/4573/) @JamieDanielson
* feat(sdk-node): add `HostDetector` as default resource detector

### :bug: (Bug Fix)

* fix(exporter-*-otlp-*): use parseHeaders() to ensure header-values are not 'undefined' #4540
Expand Down
2 changes: 1 addition & 1 deletion experimental/packages/api-events/README.md
Original file line number Diff line number Diff line change
Expand Up @@ -39,7 +39,7 @@ api.events.getEventLoggerProvider();
const eventLogger = api.events.getEventLogger(name, version);

// logging an event in an instrumentation library
eventLogger.emit({ name: 'event-name', domain: 'event-domain' });
eventLogger.emit({ name: 'event-name' });
```

## Useful links
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -22,7 +22,6 @@ import { NoopEventLogger } from './NoopEventLogger';
export class NoopEventLoggerProvider implements EventLoggerProvider {
getEventLogger(
_name: string,
_domain: string,
_version?: string | undefined,
_options?: EventLoggerOptions | undefined
): EventLogger {
Expand Down
2 changes: 0 additions & 2 deletions experimental/packages/api-events/src/api/events.ts
Original file line number Diff line number Diff line change
Expand Up @@ -73,13 +73,11 @@ export class EventsAPI {
*/
public getEventLogger(
name: string,
domain: string,
version?: string,
options?: EventLoggerOptions
): EventLogger {
return this.getEventLoggerProvider().getEventLogger(
name,
domain,
version,
options
);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -26,14 +26,12 @@ export interface EventLoggerProvider {
* schemaUrl pair is not already created.
*
* @param name The name of the event logger or instrumentation library.
* @param domain The domain for events created by the event logger.
* @param version The version of the event logger or instrumentation library.
* @param options The options of the event logger or instrumentation library.
* @returns EventLogger An event logger with the given name and version.
*/
getEventLogger(
name: string,
domain: string,
version?: string,
options?: EventLoggerOptions
): EventLogger;
Expand Down
4 changes: 2 additions & 2 deletions experimental/packages/api-events/test/api/api.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -37,7 +37,7 @@ describe('API', () => {
events.setGlobalEventLoggerProvider(new TestEventLoggerProvider());
const eventLogger = events
.getEventLoggerProvider()
.getEventLogger('name', 'domain');
.getEventLogger('name');
assert.deepStrictEqual(eventLogger, dummyEventLogger);
});

Expand All @@ -58,7 +58,7 @@ describe('API', () => {

it('should return a event logger instance from global provider', () => {
events.setGlobalEventLoggerProvider(new TestEventLoggerProvider());
const eventLogger = events.getEventLogger('myEventLogger', 'domain');
const eventLogger = events.getEventLogger('myEventLogger');
assert.deepStrictEqual(eventLogger, dummyEventLogger);
});
});
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -23,18 +23,17 @@ describe('NoopLoggerProvider', () => {
const eventLoggerProvider = new NoopEventLoggerProvider();

assert.ok(
eventLoggerProvider.getEventLogger('logger-name', 'domain') instanceof
eventLoggerProvider.getEventLogger('logger-name') instanceof
NoopEventLogger
);
assert.ok(
eventLoggerProvider.getEventLogger(
'logger-name',
'domain',
'v1'
) instanceof NoopEventLogger
);
assert.ok(
eventLoggerProvider.getEventLogger('logger-name', 'domain', 'v1', {
eventLoggerProvider.getEventLogger('logger-name', 'v1', {
schemaUrl: 'https://opentelemetry.io/schemas/1.7.0',
}) instanceof NoopEventLogger
);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -22,15 +22,13 @@ describe('NoopEventLogger', () => {
it('constructor should not crash', () => {
const logger = new NoopEventLoggerProvider().getEventLogger(
'test-noop',
'test-domain'
);
assert(logger instanceof NoopEventLogger);
});

it('calling emit should not crash', () => {
const logger = new NoopEventLoggerProvider().getEventLogger(
'test-noop',
'test-domain'
);
logger.emit({
name: 'event name',
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -58,7 +58,7 @@ import {
} from '@opentelemetry/instrumentation';
import { RPCMetadata, RPCType, setRPCMetadata } from '@opentelemetry/core';
import { errorMonitor } from 'events';
import { SemanticAttributes } from '@opentelemetry/semantic-conventions';
import { SEMATTRS_HTTP_ROUTE } from '@opentelemetry/semantic-conventions';

/**
* Http instrumentation instrumentation for Opentelemetry
Expand Down Expand Up @@ -746,7 +746,7 @@ export class HttpInstrumentation extends InstrumentationBase<Http> {
code: utils.parseResponseStatus(SpanKind.SERVER, response.statusCode),
});

const route = attributes[SemanticAttributes.HTTP_ROUTE];
const route = attributes[SEMATTRS_HTTP_ROUTE];
if (route) {
span.updateName(`${request.method || 'GET'} ${route}`);
}
Expand Down
135 changes: 74 additions & 61 deletions experimental/packages/opentelemetry-instrumentation-http/src/utils.ts
Original file line number Diff line number Diff line change
Expand Up @@ -22,8 +22,30 @@ import {
SpanKind,
} from '@opentelemetry/api';
import {
NetTransportValues,
SemanticAttributes,
NETTRANSPORTVALUES_IP_TCP,
NETTRANSPORTVALUES_IP_UDP,
SEMATTRS_HTTP_CLIENT_IP,
SEMATTRS_HTTP_FLAVOR,
SEMATTRS_HTTP_HOST,
SEMATTRS_HTTP_METHOD,
SEMATTRS_HTTP_REQUEST_CONTENT_LENGTH,
SEMATTRS_HTTP_REQUEST_CONTENT_LENGTH_UNCOMPRESSED,
SEMATTRS_HTTP_RESPONSE_CONTENT_LENGTH,
SEMATTRS_HTTP_RESPONSE_CONTENT_LENGTH_UNCOMPRESSED,
SEMATTRS_HTTP_ROUTE,
SEMATTRS_HTTP_SCHEME,
SEMATTRS_HTTP_SERVER_NAME,
SEMATTRS_HTTP_STATUS_CODE,
SEMATTRS_HTTP_TARGET,
SEMATTRS_HTTP_URL,
SEMATTRS_HTTP_USER_AGENT,
SEMATTRS_NET_HOST_IP,
SEMATTRS_NET_HOST_NAME,
SEMATTRS_NET_HOST_PORT,
SEMATTRS_NET_PEER_IP,
SEMATTRS_NET_PEER_NAME,
SEMATTRS_NET_PEER_PORT,
SEMATTRS_NET_TRANSPORT,
} from '@opentelemetry/semantic-conventions';
import {
IncomingHttpHeaders,
Expand Down Expand Up @@ -167,10 +189,9 @@ export const setRequestContentLengthAttribute = (
if (length === null) return;

if (isCompressed(request.headers)) {
attributes[SemanticAttributes.HTTP_REQUEST_CONTENT_LENGTH] = length;
attributes[SEMATTRS_HTTP_REQUEST_CONTENT_LENGTH] = length;
} else {
attributes[SemanticAttributes.HTTP_REQUEST_CONTENT_LENGTH_UNCOMPRESSED] =
length;
attributes[SEMATTRS_HTTP_REQUEST_CONTENT_LENGTH_UNCOMPRESSED] = length;
}
};

Expand All @@ -187,10 +208,9 @@ export const setResponseContentLengthAttribute = (
if (length === null) return;

if (isCompressed(response.headers)) {
attributes[SemanticAttributes.HTTP_RESPONSE_CONTENT_LENGTH] = length;
attributes[SEMATTRS_HTTP_RESPONSE_CONTENT_LENGTH] = length;
} else {
attributes[SemanticAttributes.HTTP_RESPONSE_CONTENT_LENGTH_UNCOMPRESSED] =
length;
attributes[SEMATTRS_HTTP_RESPONSE_CONTENT_LENGTH_UNCOMPRESSED] = length;
}
};

Expand Down Expand Up @@ -343,20 +363,19 @@ export const getOutgoingRequestAttributes = (
const headers = requestOptions.headers || {};
const userAgent = headers['user-agent'];
const attributes: SpanAttributes = {
[SemanticAttributes.HTTP_URL]: getAbsoluteUrl(
[SEMATTRS_HTTP_URL]: getAbsoluteUrl(
requestOptions,
headers,
`${options.component}:`
),
[SemanticAttributes.HTTP_METHOD]: method,
[SemanticAttributes.HTTP_TARGET]: requestOptions.path || '/',
[SemanticAttributes.NET_PEER_NAME]: hostname,
[SemanticAttributes.HTTP_HOST]:
requestOptions.headers?.host ?? `${hostname}:${port}`,
[SEMATTRS_HTTP_METHOD]: method,
[SEMATTRS_HTTP_TARGET]: requestOptions.path || '/',
[SEMATTRS_NET_PEER_NAME]: hostname,
[SEMATTRS_HTTP_HOST]: requestOptions.headers?.host ?? `${hostname}:${port}`,
};

if (userAgent !== undefined) {
attributes[SemanticAttributes.HTTP_USER_AGENT] = userAgent;
attributes[SEMATTRS_HTTP_USER_AGENT] = userAgent;
}
return Object.assign(attributes, options.hookAttributes);
};
Expand All @@ -369,10 +388,9 @@ export const getOutgoingRequestMetricAttributes = (
spanAttributes: SpanAttributes
): MetricAttributes => {
const metricAttributes: MetricAttributes = {};
metricAttributes[SemanticAttributes.HTTP_METHOD] =
spanAttributes[SemanticAttributes.HTTP_METHOD];
metricAttributes[SemanticAttributes.NET_PEER_NAME] =
spanAttributes[SemanticAttributes.NET_PEER_NAME];
metricAttributes[SEMATTRS_HTTP_METHOD] = spanAttributes[SEMATTRS_HTTP_METHOD];
metricAttributes[SEMATTRS_NET_PEER_NAME] =
spanAttributes[SEMATTRS_NET_PEER_NAME];
//TODO: http.url attribute, it should substitute any parameters to avoid high cardinality.
return metricAttributes;
};
Expand All @@ -384,11 +402,11 @@ export const getOutgoingRequestMetricAttributes = (
export const getAttributesFromHttpKind = (kind?: string): SpanAttributes => {
const attributes: SpanAttributes = {};
if (kind) {
attributes[SemanticAttributes.HTTP_FLAVOR] = kind;
attributes[SEMATTRS_HTTP_FLAVOR] = kind;
if (kind.toUpperCase() !== 'QUIC') {
attributes[SemanticAttributes.NET_TRANSPORT] = NetTransportValues.IP_TCP;
attributes[SEMATTRS_NET_TRANSPORT] = NETTRANSPORTVALUES_IP_TCP;
} else {
attributes[SemanticAttributes.NET_TRANSPORT] = NetTransportValues.IP_UDP;
attributes[SEMATTRS_NET_TRANSPORT] = NETTRANSPORTVALUES_IP_UDP;
}
}
return attributes;
Expand All @@ -406,13 +424,13 @@ export const getOutgoingRequestAttributesOnResponse = (
const attributes: SpanAttributes = {};
if (socket) {
const { remoteAddress, remotePort } = socket;
attributes[SemanticAttributes.NET_PEER_IP] = remoteAddress;
attributes[SemanticAttributes.NET_PEER_PORT] = remotePort;
attributes[SEMATTRS_NET_PEER_IP] = remoteAddress;
attributes[SEMATTRS_NET_PEER_PORT] = remotePort;
}
setResponseContentLengthAttribute(response, attributes);

if (statusCode) {
attributes[SemanticAttributes.HTTP_STATUS_CODE] = statusCode;
attributes[SEMATTRS_HTTP_STATUS_CODE] = statusCode;
attributes[AttributeNames.HTTP_STATUS_TEXT] = (
statusMessage || ''
).toUpperCase();
Expand All @@ -430,12 +448,11 @@ export const getOutgoingRequestMetricAttributesOnResponse = (
spanAttributes: SpanAttributes
): MetricAttributes => {
const metricAttributes: MetricAttributes = {};
metricAttributes[SemanticAttributes.NET_PEER_PORT] =
spanAttributes[SemanticAttributes.NET_PEER_PORT];
metricAttributes[SemanticAttributes.HTTP_STATUS_CODE] =
spanAttributes[SemanticAttributes.HTTP_STATUS_CODE];
metricAttributes[SemanticAttributes.HTTP_FLAVOR] =
spanAttributes[SemanticAttributes.HTTP_FLAVOR];
metricAttributes[SEMATTRS_NET_PEER_PORT] =
spanAttributes[SEMATTRS_NET_PEER_PORT];
metricAttributes[SEMATTRS_HTTP_STATUS_CODE] =
spanAttributes[SEMATTRS_HTTP_STATUS_CODE];
metricAttributes[SEMATTRS_HTTP_FLAVOR] = spanAttributes[SEMATTRS_HTTP_FLAVOR];
return metricAttributes;
};

Expand Down Expand Up @@ -465,31 +482,31 @@ export const getIncomingRequestAttributes = (
'localhost';
const serverName = options.serverName;
const attributes: SpanAttributes = {
[SemanticAttributes.HTTP_URL]: getAbsoluteUrl(
[SEMATTRS_HTTP_URL]: getAbsoluteUrl(
requestUrl,
headers,
`${options.component}:`
),
[SemanticAttributes.HTTP_HOST]: host,
[SemanticAttributes.NET_HOST_NAME]: hostname,
[SemanticAttributes.HTTP_METHOD]: method,
[SemanticAttributes.HTTP_SCHEME]: options.component,
[SEMATTRS_HTTP_HOST]: host,
[SEMATTRS_NET_HOST_NAME]: hostname,
[SEMATTRS_HTTP_METHOD]: method,
[SEMATTRS_HTTP_SCHEME]: options.component,
};

if (typeof ips === 'string') {
attributes[SemanticAttributes.HTTP_CLIENT_IP] = ips.split(',')[0];
attributes[SEMATTRS_HTTP_CLIENT_IP] = ips.split(',')[0];
}

if (typeof serverName === 'string') {
attributes[SemanticAttributes.HTTP_SERVER_NAME] = serverName;
attributes[SEMATTRS_HTTP_SERVER_NAME] = serverName;
}

if (requestUrl) {
attributes[SemanticAttributes.HTTP_TARGET] = requestUrl.path || '/';
attributes[SEMATTRS_HTTP_TARGET] = requestUrl.path || '/';
}

if (userAgent !== undefined) {
attributes[SemanticAttributes.HTTP_USER_AGENT] = userAgent;
attributes[SEMATTRS_HTTP_USER_AGENT] = userAgent;
}
setRequestContentLengthAttribute(request, attributes);

Expand All @@ -506,14 +523,11 @@ export const getIncomingRequestMetricAttributes = (
spanAttributes: SpanAttributes
): MetricAttributes => {
const metricAttributes: MetricAttributes = {};
metricAttributes[SemanticAttributes.HTTP_SCHEME] =
spanAttributes[SemanticAttributes.HTTP_SCHEME];
metricAttributes[SemanticAttributes.HTTP_METHOD] =
spanAttributes[SemanticAttributes.HTTP_METHOD];
metricAttributes[SemanticAttributes.NET_HOST_NAME] =
spanAttributes[SemanticAttributes.NET_HOST_NAME];
metricAttributes[SemanticAttributes.HTTP_FLAVOR] =
spanAttributes[SemanticAttributes.HTTP_FLAVOR];
metricAttributes[SEMATTRS_HTTP_SCHEME] = spanAttributes[SEMATTRS_HTTP_SCHEME];
metricAttributes[SEMATTRS_HTTP_METHOD] = spanAttributes[SEMATTRS_HTTP_METHOD];
metricAttributes[SEMATTRS_NET_HOST_NAME] =
spanAttributes[SEMATTRS_NET_HOST_NAME];
metricAttributes[SEMATTRS_HTTP_FLAVOR] = spanAttributes[SEMATTRS_HTTP_FLAVOR];
//TODO: http.target attribute, it should substitute any parameters to avoid high cardinality.
return metricAttributes;
};
Expand All @@ -535,18 +549,18 @@ export const getIncomingRequestAttributesOnResponse = (
const attributes: SpanAttributes = {};
if (socket) {
const { localAddress, localPort, remoteAddress, remotePort } = socket;
attributes[SemanticAttributes.NET_HOST_IP] = localAddress;
attributes[SemanticAttributes.NET_HOST_PORT] = localPort;
attributes[SemanticAttributes.NET_PEER_IP] = remoteAddress;
attributes[SemanticAttributes.NET_PEER_PORT] = remotePort;
attributes[SEMATTRS_NET_HOST_IP] = localAddress;
attributes[SEMATTRS_NET_HOST_PORT] = localPort;
attributes[SEMATTRS_NET_PEER_IP] = remoteAddress;
attributes[SEMATTRS_NET_PEER_PORT] = remotePort;
}
attributes[SemanticAttributes.HTTP_STATUS_CODE] = statusCode;
attributes[SEMATTRS_HTTP_STATUS_CODE] = statusCode;
attributes[AttributeNames.HTTP_STATUS_TEXT] = (
statusMessage || ''
).toUpperCase();

if (rpcMetadata?.type === RPCType.HTTP && rpcMetadata.route !== undefined) {
attributes[SemanticAttributes.HTTP_ROUTE] = rpcMetadata.route;
attributes[SEMATTRS_HTTP_ROUTE] = rpcMetadata.route;
}
return attributes;
};
Expand All @@ -559,13 +573,12 @@ export const getIncomingRequestMetricAttributesOnResponse = (
spanAttributes: SpanAttributes
): MetricAttributes => {
const metricAttributes: MetricAttributes = {};
metricAttributes[SemanticAttributes.HTTP_STATUS_CODE] =
spanAttributes[SemanticAttributes.HTTP_STATUS_CODE];
metricAttributes[SemanticAttributes.NET_HOST_PORT] =
spanAttributes[SemanticAttributes.NET_HOST_PORT];
if (spanAttributes[SemanticAttributes.HTTP_ROUTE] !== undefined) {
metricAttributes[SemanticAttributes.HTTP_ROUTE] =
spanAttributes[SemanticAttributes.HTTP_ROUTE];
metricAttributes[SEMATTRS_HTTP_STATUS_CODE] =
spanAttributes[SEMATTRS_HTTP_STATUS_CODE];
metricAttributes[SEMATTRS_NET_HOST_PORT] =
spanAttributes[SEMATTRS_NET_HOST_PORT];
if (spanAttributes[SEMATTRS_HTTP_ROUTE] !== undefined) {
metricAttributes[SEMATTRS_HTTP_ROUTE] = spanAttributes[SEMATTRS_HTTP_ROUTE];
}
return metricAttributes;
};
Expand Down
Loading

0 comments on commit 80901be

Please sign in to comment.