Skip to content

Commit

Permalink
feat/logattributes support map type (#3821)
Browse files Browse the repository at this point in the history
Co-authored-by: Marc Pichler <marc.pichler@dynatrace.com>
  • Loading branch information
Abinet18 and pichlermarc committed Jun 2, 2023
1 parent a4f0b42 commit 8eb9bf3
Show file tree
Hide file tree
Showing 10 changed files with 37 additions and 17 deletions.
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

### :rocket: (Enhancement)

* feat(api-logs): support map in log attributes. [#3821](https://github.com/open-telemetry/opentelemetry-js/pull/3821) @Abinet18
* feat(instrumentation): add ESM support for instrumentation. [#3698](https://github.com/open-telemetry/opentelemetry-js/pull/3698) @JamieDanielson, @pkanal, @vmarchaud, @lizthegrey, @bengl
* feat(exporter-logs-otlp-http): otlp-http exporter for logs. [#3764](https://github.com/open-telemetry/opentelemetry-js/pull/3764/) @fuaiyi
* feat(otlp-trace-exporters): Add User-Agent header to OTLP trace exporters. [#3790](https://github.com/open-telemetry/opentelemetry-js/pull/3790) @JamieDanielson
Expand Down
9 changes: 7 additions & 2 deletions experimental/packages/api-logs/src/types/LogRecord.ts
Original file line number Diff line number Diff line change
Expand Up @@ -14,7 +14,12 @@
* limitations under the License.
*/

import { Attributes, Context } from '@opentelemetry/api';
import { AttributeValue, Context } from '@opentelemetry/api';

export type LogAttributeValue = AttributeValue | LogAttributes;
export interface LogAttributes {
[attributeKey: string]: LogAttributeValue | undefined;
}

export enum SeverityNumber {
UNSPECIFIED = 0,
Expand Down Expand Up @@ -73,7 +78,7 @@ export interface LogRecord {
/**
* Attributes that define the log record.
*/
attributes?: Attributes;
attributes?: LogAttributes;

/**
* The Context associated with the LogRecord.
Expand Down
2 changes: 0 additions & 2 deletions experimental/packages/api-logs/src/types/LoggerOptions.ts
Original file line number Diff line number Diff line change
Expand Up @@ -13,9 +13,7 @@
* See the License for the specific language governing permissions and
* limitations under the License.
*/

import { Attributes } from '@opentelemetry/api';

export interface LoggerOptions {
/**
* The schemaUrl of the tracer or instrumentation library
Expand Down
1 change: 1 addition & 0 deletions experimental/packages/otlp-transformer/package.json
Original file line number Diff line number Diff line change
Expand Up @@ -57,6 +57,7 @@
"devDependencies": {
"@opentelemetry/api": "1.4.1",
"@opentelemetry/api-logs": "0.39.1",
"@opentelemetry/sdk-logs": "0.39.1",
"@types/mocha": "10.0.0",
"@types/webpack-env": "1.16.3",
"codecov": "3.8.3",
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -13,8 +13,8 @@
* See the License for the specific language governing permissions and
* limitations under the License.
*/
import type { Attributes } from '@opentelemetry/api';
import type { IAnyValue, IKeyValue } from './types';
import { Attributes } from '@opentelemetry/api';

export function toAttributes(attributes: Attributes): IKeyValue[] {
return Object.keys(attributes).map(key => toKeyValue(key, attributes[key]));
Expand Down
10 changes: 8 additions & 2 deletions experimental/packages/otlp-transformer/src/logs/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -22,9 +22,11 @@ import {
IResourceLogs,
} from './types';
import { IResource } from '@opentelemetry/resources';
import { toAnyValue, toAttributes } from '../common/internal';
import { toAnyValue, toAttributes, toKeyValue } from '../common/internal';
import { hexToBase64, hrTimeToNanoseconds } from '@opentelemetry/core';
import { SeverityNumber } from '@opentelemetry/api-logs';
import { IKeyValue } from '../common/types';
import { LogAttributes } from '@opentelemetry/api-logs';

export function createExportLogsServiceRequest(
logRecords: ReadableLogRecord[],
Expand Down Expand Up @@ -97,7 +99,7 @@ function toLogRecord(log: ReadableLogRecord, useHex?: boolean): ILogRecord {
severityNumber: toSeverityNumber(log.severityNumber),
severityText: log.severityText,
body: toAnyValue(log.body),
attributes: toAttributes(log.attributes),
attributes: toLogAttributes(log.attributes),
droppedAttributesCount: 0,
flags: log.spanContext?.traceFlags,
traceId: useHex
Expand All @@ -119,3 +121,7 @@ function optionalHexToBase64(str: string | undefined): string | undefined {
if (str === undefined) return undefined;
return hexToBase64(str);
}

export function toLogAttributes(attributes: LogAttributes): IKeyValue[] {
return Object.keys(attributes).map(key => toKeyValue(key, attributes[key]));
}
2 changes: 1 addition & 1 deletion experimental/packages/sdk-logs/package.json
Original file line number Diff line number Diff line change
Expand Up @@ -69,7 +69,7 @@
"sideEffects": false,
"peerDependencies": {
"@opentelemetry/api": ">=1.4.0 <1.5.0",
"@opentelemetry/api-logs": ">=0.38.0"
"@opentelemetry/api-logs": ">=0.39.1"
},
"devDependencies": {
"@opentelemetry/api": ">=1.4.0 <1.5.0",
Expand Down
16 changes: 12 additions & 4 deletions experimental/packages/sdk-logs/src/LogRecord.ts
Original file line number Diff line number Diff line change
Expand Up @@ -14,7 +14,7 @@
* limitations under the License.
*/

import { Attributes, AttributeValue, diag } from '@opentelemetry/api';
import { AttributeValue, diag } from '@opentelemetry/api';
import type * as logsAPI from '@opentelemetry/api-logs';
import * as api from '@opentelemetry/api';
import {
Expand All @@ -27,14 +27,15 @@ import type { IResource } from '@opentelemetry/resources';
import type { ReadableLogRecord } from './export/ReadableLogRecord';
import type { LogRecordLimits } from './types';
import { Logger } from './Logger';
import { LogAttributes } from '@opentelemetry/api-logs';

export class LogRecord implements ReadableLogRecord {
readonly hrTime: api.HrTime;
readonly hrTimeObserved: api.HrTime;
readonly spanContext?: api.SpanContext;
readonly resource: IResource;
readonly instrumentationScope: InstrumentationScope;
readonly attributes: Attributes = {};
readonly attributes: logsAPI.LogAttributes = {};
private _severityText?: string;
private _severityNumber?: logsAPI.SeverityNumber;
private _body?: string;
Expand Down Expand Up @@ -102,13 +103,20 @@ export class LogRecord implements ReadableLogRecord {
this.setAttributes(attributes);
}

public setAttribute(key: string, value?: AttributeValue) {
public setAttribute(key: string, value?: LogAttributes | AttributeValue) {
if (this._isLogRecordReadonly()) {
return this;
}
if (value === null) {
return this;
}
if (
typeof value === 'object' &&
!Array.isArray(value) &&
Object.keys(value).length > 0
) {
this.attributes[key] = value;
}
if (key.length === 0) {
api.diag.warn(`Invalid attribute key: ${key}`);
return this;
Expand All @@ -128,7 +136,7 @@ export class LogRecord implements ReadableLogRecord {
return this;
}

public setAttributes(attributes: Attributes) {
public setAttributes(attributes: LogAttributes) {
for (const [k, v] of Object.entries(attributes)) {
this.setAttribute(k, v);
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -15,9 +15,9 @@
*/

import type { IResource } from '@opentelemetry/resources';
import type { Attributes, HrTime, SpanContext } from '@opentelemetry/api';
import type { HrTime, SpanContext } from '@opentelemetry/api';
import type { InstrumentationScope } from '@opentelemetry/core';
import type { SeverityNumber } from '@opentelemetry/api-logs';
import type { LogAttributes, SeverityNumber } from '@opentelemetry/api-logs';

export interface ReadableLogRecord {
readonly hrTime: HrTime;
Expand All @@ -28,5 +28,5 @@ export interface ReadableLogRecord {
readonly body?: string;
readonly resource: IResource;
readonly instrumentationScope: InstrumentationScope;
readonly attributes: Attributes;
readonly attributes: LogAttributes;
}
5 changes: 3 additions & 2 deletions experimental/packages/sdk-logs/test/common/utils.ts
Original file line number Diff line number Diff line change
Expand Up @@ -21,11 +21,12 @@ export const validAttributes = {
'array<string>': ['str1', 'str2'],
'array<number>': [1, 2],
'array<bool>': [true, false],
object: { bar: 'foo' },
};

export const invalidAttributes = {
// invalid attribute type object
object: { foo: 'bar' },
// invalid attribute empty object
object: {},
// invalid attribute inhomogeneous array
'non-homogeneous-array': [0, ''],
// This empty length attribute should not be set
Expand Down

0 comments on commit 8eb9bf3

Please sign in to comment.