Skip to content

Commit

Permalink
feat(api-logs): add ObservedTimestamp to LogRecord
Browse files Browse the repository at this point in the history
  • Loading branch information
llc1123 committed May 8, 2023
1 parent 7255da9 commit 2d47386
Show file tree
Hide file tree
Showing 6 changed files with 27 additions and 5 deletions.
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();
this.hrTime = timestamp ? timeInputToHrTime(timestamp) : now;
this.hrTimeObserved = observedTimestamp
? timeInputToHrTime(observedTimestamp)
: now;
}

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

0 comments on commit 2d47386

Please sign in to comment.