Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

feat(api-logs): add ObservedTimestamp to LogRecord #3787

Merged
merged 8 commits into from
May 19, 2023
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();
llc1123 marked this conversation as resolved.
Show resolved Hide resolved
this.hrTime = timestamp ? timeInputToHrTime(timestamp) : now;
this.hrTimeObserved = observedTimestamp
? timeInputToHrTime(observedTimestamp)
: now;
}

llc1123 marked this conversation as resolved.
Show resolved Hide 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
Loading