Skip to content
Merged
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 @@ -13,6 +13,7 @@ All notable changes to experimental packages in this project will be documented
* feat(exporter-logs-otlp-grpc): otlp-grpc exporter for logs. [#3712](https://github.com/open-telemetry/opentelemetry-js/pull/3712/) @llc1123
* feat(otlp-grpc-exporter-base): use statically generated protobuf code [#3705](https://github.com/open-telemetry/opentelemetry-js/pull/3705) @pichlermarc
* refactor(otlp-transformer): refine metric transformers. [#3770](https://github.com/open-telemetry/opentelemetry-js/pull/3770/) @llc1123
* feat(api-logs): add `ObservedTimestamp` to `LogRecord`. [#3787](https://github.com/open-telemetry/opentelemetry-js/pull/3787/) @llc1123

### :bug: (Bug Fix)

Expand Down
5 changes: 5 additions & 0 deletions experimental/packages/api-logs/src/types/LogRecord.ts
Original file line number Diff line number Diff line change
Expand Up @@ -50,6 +50,11 @@ export interface LogRecord {
*/
timestamp?: number;

/**
* Time when the event was observed by the collection system.
*/
observedTimestamp?: number;

/**
* Numerical value of the severity.
*/
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -45,6 +45,7 @@ export const mockedReadableLogRecord: ReadableLogRecord = {
schemaUrl: 'http://url.to.schema',
},
hrTime: [1680253513, 123241635] as HrTime,
hrTimeObserved: [1683526948, 965142784] as HrTime,
attributes: {
'some-attribute': 'some attribute value',
},
Expand Down Expand Up @@ -92,7 +93,7 @@ export function ensureExportedLogRecordIsCorrect(logRecord: ILogRecord) {
);
assert.strictEqual(
logRecord.observedTimeUnixNano,
'1680253513123241728',
'1683526948965142784',
'observedTimeUnixNano is wrong'
);
assert.strictEqual(
Expand Down
2 changes: 1 addition & 1 deletion experimental/packages/otlp-transformer/src/logs/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -93,7 +93,7 @@ function logRecordsToResourceLogs(
function toLogRecord(log: ReadableLogRecord, useHex?: boolean): ILogRecord {
return {
timeUnixNano: hrTimeToNanoseconds(log.hrTime),
observedTimeUnixNano: hrTimeToNanoseconds(log.hrTime),
observedTimeUnixNano: hrTimeToNanoseconds(log.hrTimeObserved),
severityNumber: toSeverityNumber(log.severityNumber),
severityText: log.severityText,
body: toAnyValue(log.body),
Expand Down
4 changes: 3 additions & 1 deletion experimental/packages/otlp-transformer/test/logs.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -52,7 +52,7 @@ function createExpectedLogJson(useHex: boolean): IExportLogsServiceRequest {
// eslint-disable-next-line @typescript-eslint/no-loss-of-precision
timeUnixNano: 1680253513123241635,
// eslint-disable-next-line @typescript-eslint/no-loss-of-precision
observedTimeUnixNano: 1680253513123241635,
observedTimeUnixNano: 1683526948965142784,
severityNumber: ESeverityNumber.SEVERITY_NUMBER_ERROR,
severityText: 'error',
body: { stringValue: 'some_log_body' },
Expand Down Expand Up @@ -105,6 +105,7 @@ describe('Logs', () => {
};
const log_fragment_1 = {
hrTime: [1680253513, 123241635] as HrTime,
hrTimeObserved: [1683526948, 965142784] as HrTime,
attributes: {
'some-attribute': 'some attribute value',
},
Expand All @@ -119,6 +120,7 @@ describe('Logs', () => {
};
const log_fragment_2 = {
hrTime: [1680253797, 687038506] as HrTime,
hrTimeObserved: [1680253797, 687038506] as HrTime,
attributes: {
'another-attribute': 'another attribute value',
},
Expand Down
17 changes: 15 additions & 2 deletions experimental/packages/sdk-logs/src/LogRecord.ts
Original file line number Diff line number Diff line change
Expand Up @@ -21,6 +21,7 @@ import {
timeInputToHrTime,
isAttributeValue,
InstrumentationScope,
hrTime,
} from '@opentelemetry/core';
import type { IResource } from '@opentelemetry/resources';

Expand All @@ -30,6 +31,7 @@ import { Logger } from './Logger';

export class LogRecord implements ReadableLogRecord {
readonly hrTime: api.HrTime;
readonly hrTimeObserved: api.HrTime;
readonly spanContext?: api.SpanContext;
readonly resource: IResource;
readonly instrumentationScope: InstrumentationScope;
Expand Down Expand Up @@ -73,15 +75,26 @@ export class LogRecord implements ReadableLogRecord {

constructor(logger: Logger, logRecord: logsAPI.LogRecord) {
const {
timestamp = Date.now(),
timestamp,
observedTimestamp,
severityNumber,
severityText,
body,
attributes = {},
context,
} = logRecord;

this.hrTime = timeInputToHrTime(timestamp);
if (timestamp && observedTimestamp) {
this.hrTime = timeInputToHrTime(timestamp);
this.hrTimeObserved = timeInputToHrTime(observedTimestamp);
} else {
const now = hrTime();
Comment thread
llc1123 marked this conversation as resolved.
Outdated
this.hrTime = timestamp ? timeInputToHrTime(timestamp) : now;
this.hrTimeObserved = observedTimestamp
? timeInputToHrTime(observedTimestamp)
: now;
}

Comment thread
llc1123 marked this conversation as resolved.
if (context) {
const spanContext = api.trace.getSpanContext(context);
if (spanContext && api.isSpanContextValid(spanContext)) {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -21,6 +21,7 @@ import type { SeverityNumber } from '@opentelemetry/api-logs';

export interface ReadableLogRecord {
readonly hrTime: HrTime;
readonly hrTimeObserved: HrTime;
readonly spanContext?: SpanContext;
readonly severityText?: string;
readonly severityNumber?: SeverityNumber;
Expand Down