Skip to content
Open
Show file tree
Hide file tree
Changes from 4 commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
1 change: 1 addition & 0 deletions experimental/CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -16,6 +16,7 @@ For notes on migrating to 2.x / 0.200.x see [the upgrade guide](doc/upgrade-to-2

* feat(otlp-transformer): add custom protobuf logs serializer [#6228](https://github.com/open-telemetry/opentelemetry-js/pull/6228) @pichlermarc
* feat(otlp-transformer): add custom protobuf logs export response deserializer [#6530](https://github.com/open-telemetry/opentelemetry-js/pull/6530) @pichlermarc
* feat(sdk-logs): add support for scopeAttributes in LoggerOptions [#6573](https://github.com/open-telemetry/opentelemetry-js/pull/6573) @pichlermarc

### :bug: Bug Fixes

Expand Down
12 changes: 10 additions & 2 deletions experimental/packages/api-logs/src/api/logs.ts
Original file line number Diff line number Diff line change
Expand Up @@ -58,9 +58,17 @@ export class LogsAPI {
}

/**
* Returns a logger from the global logger provider.
* Returns a Logger, creating one if one with the given name, version,
* schemaUrl, and scopeAttributes is not already created.
*
* @returns Logger
* Getting a Logger may be expensive, especially when `scopeAttributes` are
* provided. Reuse Logger instances where possible instead of calling
* `getLogger()` on hot paths.
*
* @param name The name of the logger or instrumentation library.
* @param version The version of the logger or instrumentation library.
* @param options The options of the logger or instrumentation library.
* @returns {@link Logger} A Logger with the given name and version
*/
public getLogger(
name: string,
Expand Down
3 changes: 2 additions & 1 deletion experimental/packages/api-logs/src/types/LoggerOptions.ts
Original file line number Diff line number Diff line change
Expand Up @@ -12,7 +12,8 @@ export interface LoggerOptions {
schemaUrl?: string;

/**
* The instrumentation scope attributes to associate with emitted telemetry
* The instrumentation scope attributes to associate with emitted telemetry.
* These attributes also participate in logger identity.
*/
scopeAttributes?: LogAttributes;
Comment thread
trentm marked this conversation as resolved.
Outdated
}
10 changes: 7 additions & 3 deletions experimental/packages/api-logs/src/types/LoggerProvider.ts
Original file line number Diff line number Diff line change
Expand Up @@ -11,13 +11,17 @@ import type { LoggerOptions } from './LoggerOptions';
*/
export interface LoggerProvider {
/**
* Returns a Logger, creating one if one with the given name, version, and
* schemaUrl pair is not already created.
* Returns a Logger, creating one if one with the given name, version,
* schemaUrl, and scopeAttributes is not already created.
*
* Getting a Logger may be expensive, especially when `scopeAttributes` are
* provided. Reuse Logger instances where possible instead of calling
* `getLogger()` on hot paths.
*
* @param name The name of the logger or instrumentation library.
* @param version The version of the logger or instrumentation library.
* @param options The options of the logger or instrumentation library.
* @returns Logger A Logger with the given name and version
* @returns {@link Logger} A Logger with the given name and version
Comment thread
pichlermarc marked this conversation as resolved.
Outdated
*/
getLogger(name: string, version?: string, options?: LoggerOptions): Logger;
}
19 changes: 15 additions & 4 deletions experimental/packages/otlp-transformer/src/common/internal.ts
Original file line number Diff line number Diff line change
Expand Up @@ -8,10 +8,10 @@ import type {
IKeyValue,
Resource,
} from './internal-types';
import type { Attributes } from '@opentelemetry/api';
import type { InstrumentationScope } from '@opentelemetry/core';
import type { Resource as ISdkResource } from '@opentelemetry/resources';
import type { Encoder } from './utils';
import type { LogAttributes } from '@opentelemetry/api-logs';

export function createResource(
resource: ISdkResource,
Expand All @@ -29,16 +29,27 @@ export function createResource(
}

export function createInstrumentationScope(
scope: InstrumentationScope
scope: InstrumentationScope & {
attributes?: LogAttributes;
droppedAttributesCount?: number;
},
encoder: Encoder
): IInstrumentationScope {
return {
const result: IInstrumentationScope = {
name: scope.name,
version: scope.version,
};

if (scope.attributes && Object.keys(scope.attributes).length > 0) {
result.attributes = toAttributes(scope.attributes, encoder);
result.droppedAttributesCount = scope.droppedAttributesCount ?? 0;
}

return result;
}

export function toAttributes(
attributes: Attributes,
attributes: LogAttributes,
encoder: Encoder
): IKeyValue[] {
return Object.keys(attributes).map(key =>
Expand Down
37 changes: 14 additions & 23 deletions experimental/packages/otlp-transformer/src/logs/internal.ts
Original file line number Diff line number Diff line change
Expand Up @@ -16,11 +16,9 @@ import {
createInstrumentationScope,
createResource,
toAnyValue,
toKeyValue,
toAttributes,
} from '../common/internal';
import type { SeverityNumber } from '@opentelemetry/api-logs';
import type { IKeyValue } from '../common/internal-types';
import type { LogAttributes } from '@opentelemetry/api-logs';

export function createExportLogsServiceRequest(
logRecords: ReadableLogRecord[],
Expand All @@ -33,29 +31,28 @@ export function createExportLogsServiceRequest(

function createResourceMap(
logRecords: ReadableLogRecord[]
): Map<Resource, Map<string, ReadableLogRecord[]>> {
): Map<
Resource,
Map<ReadableLogRecord['instrumentationScope'], ReadableLogRecord[]>
> {
const resourceMap: Map<
Resource,
Map<string, ReadableLogRecord[]>
Map<ReadableLogRecord['instrumentationScope'], ReadableLogRecord[]>
> = new Map();

for (const record of logRecords) {
const {
resource,
instrumentationScope: { name, version = '', schemaUrl = '' },
} = record;
const { resource, instrumentationScope } = record;

let ismMap = resourceMap.get(resource);
if (!ismMap) {
ismMap = new Map();
resourceMap.set(resource, ismMap);
}

const ismKey = `${name}@${version}:${schemaUrl}`;
let records = ismMap.get(ismKey);
let records = ismMap.get(instrumentationScope);
Copy link
Copy Markdown
Member Author

Choose a reason for hiding this comment

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

note for reviewers: by avoding this string operation we get a significant performance boost for JSON serialization. we already do this in the protobuf serializer for performance reasons. Having to the string operation for each log record with scopeAttributes would be quite expensive.

Old:

transform 512 logs (json) x 734 ops/sec ±0.33% (97 runs sampled)

New:

transform 512 logs (json) x 770 ops/sec ±0.78% (92 runs sampled)

if (!records) {
records = [];
ismMap.set(ismKey, records);
ismMap.set(instrumentationScope, records);
}
records.push(record);
}
Expand All @@ -73,7 +70,10 @@ function logRecordsToResourceLogs(
resource: processedResource,
scopeLogs: Array.from(ismMap, ([, scopeLogs]) => {
return {
scope: createInstrumentationScope(scopeLogs[0].instrumentationScope),
scope: createInstrumentationScope(
scopeLogs[0].instrumentationScope,
encoder
),
logRecords: scopeLogs.map(log => toLogRecord(log, encoder)),
schemaUrl: scopeLogs[0].instrumentationScope.schemaUrl,
};
Expand All @@ -91,7 +91,7 @@ function toLogRecord(log: ReadableLogRecord, encoder: Encoder): ILogRecord {
severityText: log.severityText,
body: toAnyValue(log.body, encoder),
eventName: log.eventName,
attributes: toLogAttributes(log.attributes, encoder),
attributes: toAttributes(log.attributes, encoder),
droppedAttributesCount: log.droppedAttributesCount,
flags: log.spanContext?.traceFlags,
traceId: encoder.encodeOptionalSpanContext(log.spanContext?.traceId),
Expand All @@ -104,12 +104,3 @@ function toSeverityNumber(
): ESeverityNumber | undefined {
return severityNumber as number | undefined as ESeverityNumber | undefined;
}

export function toLogAttributes(
attributes: LogAttributes,
encoder: Encoder
): IKeyValue[] {
return Object.keys(attributes).map(key =>
toKeyValue(key, attributes[key], encoder)
);
}
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,7 @@ import { ProtobufWriter } from '../../common/protobuf/protobuf-writer';
import { hexToBinary } from '../../common/hex-to-binary';
import type { Resource } from '@opentelemetry/resources';
import type { InstrumentationScope } from '@opentelemetry/core';
import { SeverityNumber } from '@opentelemetry/api-logs';
import { type LogAttributes, SeverityNumber } from '@opentelemetry/api-logs';
import {
writeAnyValue,
writeAttributes,
Expand Down Expand Up @@ -99,7 +99,10 @@ function serializeLogRecord(
*/
function serializeScopeLogs(
writer: IProtobufWriter,
scope: InstrumentationScope,
scope: InstrumentationScope & {
attributes?: LogAttributes;
droppedAttributesCount?: number;
},
logRecords: ReadableLogRecord[]
): void {
const scopeLogsStart = writer.startLengthDelimited();
Expand All @@ -119,6 +122,17 @@ function serializeScopeLogs(
writer.writeString(scope.version);
}

if (scope.attributes) {
// attributes (field 3, repeated KeyValue)
writeAttributes(writer, scope.attributes, 3);
}

if (scope.droppedAttributesCount) {
// dropped_attributes_count (field 4, uint32)
writer.writeTag(4, 0);
writer.writeVarint(scope.droppedAttributesCount);
}

writer.finishLengthDelimited(scopeStart, writer.pos - scopeStartPos);

// log_records (field 2, repeated LogRecord)
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -50,7 +50,7 @@ export function toScopeMetrics(
): IScopeMetrics[] {
return Array.from(
scopeMetrics.map(metrics => ({
scope: createInstrumentationScope(metrics.scope),
scope: createInstrumentationScope(metrics.scope, encoder),
metrics: metrics.metrics.map(metricData => toMetric(metricData, encoder)),
schemaUrl: metrics.scope.schemaUrl,
}))
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -152,7 +152,10 @@ function spanRecordsToResourceSpans(
);

scopeResourceSpans.push({
scope: createInstrumentationScope(scopeSpans[0].instrumentationScope),
scope: createInstrumentationScope(
scopeSpans[0].instrumentationScope,
encoder
),
spans: spans,
schemaUrl: scopeSpans[0].instrumentationScope.schemaUrl,
});
Expand Down
Loading
Loading