Skip to content

Commit 8eb9bf3

Browse files
feat/logattributes support map type (#3821)
Co-authored-by: Marc Pichler <[email protected]>
1 parent a4f0b42 commit 8eb9bf3

File tree

10 files changed

+37
-17
lines changed

10 files changed

+37
-17
lines changed

experimental/CHANGELOG.md

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -13,6 +13,7 @@ All notable changes to experimental packages in this project will be documented
1313

1414
### :rocket: (Enhancement)
1515

16+
* feat(api-logs): support map in log attributes. [#3821](https://github.com/open-telemetry/opentelemetry-js/pull/3821) @Abinet18
1617
* feat(instrumentation): add ESM support for instrumentation. [#3698](https://github.com/open-telemetry/opentelemetry-js/pull/3698) @JamieDanielson, @pkanal, @vmarchaud, @lizthegrey, @bengl
1718
* feat(exporter-logs-otlp-http): otlp-http exporter for logs. [#3764](https://github.com/open-telemetry/opentelemetry-js/pull/3764/) @fuaiyi
1819
* feat(otlp-trace-exporters): Add User-Agent header to OTLP trace exporters. [#3790](https://github.com/open-telemetry/opentelemetry-js/pull/3790) @JamieDanielson

experimental/packages/api-logs/src/types/LogRecord.ts

Lines changed: 7 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -14,7 +14,12 @@
1414
* limitations under the License.
1515
*/
1616

17-
import { Attributes, Context } from '@opentelemetry/api';
17+
import { AttributeValue, Context } from '@opentelemetry/api';
18+
19+
export type LogAttributeValue = AttributeValue | LogAttributes;
20+
export interface LogAttributes {
21+
[attributeKey: string]: LogAttributeValue | undefined;
22+
}
1823

1924
export enum SeverityNumber {
2025
UNSPECIFIED = 0,
@@ -73,7 +78,7 @@ export interface LogRecord {
7378
/**
7479
* Attributes that define the log record.
7580
*/
76-
attributes?: Attributes;
81+
attributes?: LogAttributes;
7782

7883
/**
7984
* The Context associated with the LogRecord.

experimental/packages/api-logs/src/types/LoggerOptions.ts

Lines changed: 0 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -13,9 +13,7 @@
1313
* See the License for the specific language governing permissions and
1414
* limitations under the License.
1515
*/
16-
1716
import { Attributes } from '@opentelemetry/api';
18-
1917
export interface LoggerOptions {
2018
/**
2119
* The schemaUrl of the tracer or instrumentation library

experimental/packages/otlp-transformer/package.json

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -57,6 +57,7 @@
5757
"devDependencies": {
5858
"@opentelemetry/api": "1.4.1",
5959
"@opentelemetry/api-logs": "0.39.1",
60+
"@opentelemetry/sdk-logs": "0.39.1",
6061
"@types/mocha": "10.0.0",
6162
"@types/webpack-env": "1.16.3",
6263
"codecov": "3.8.3",

experimental/packages/otlp-transformer/src/common/internal.ts

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -13,8 +13,8 @@
1313
* See the License for the specific language governing permissions and
1414
* limitations under the License.
1515
*/
16-
import type { Attributes } from '@opentelemetry/api';
1716
import type { IAnyValue, IKeyValue } from './types';
17+
import { Attributes } from '@opentelemetry/api';
1818

1919
export function toAttributes(attributes: Attributes): IKeyValue[] {
2020
return Object.keys(attributes).map(key => toKeyValue(key, attributes[key]));

experimental/packages/otlp-transformer/src/logs/index.ts

Lines changed: 8 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -22,9 +22,11 @@ import {
2222
IResourceLogs,
2323
} from './types';
2424
import { IResource } from '@opentelemetry/resources';
25-
import { toAnyValue, toAttributes } from '../common/internal';
25+
import { toAnyValue, toAttributes, toKeyValue } from '../common/internal';
2626
import { hexToBase64, hrTimeToNanoseconds } from '@opentelemetry/core';
2727
import { SeverityNumber } from '@opentelemetry/api-logs';
28+
import { IKeyValue } from '../common/types';
29+
import { LogAttributes } from '@opentelemetry/api-logs';
2830

2931
export function createExportLogsServiceRequest(
3032
logRecords: ReadableLogRecord[],
@@ -97,7 +99,7 @@ function toLogRecord(log: ReadableLogRecord, useHex?: boolean): ILogRecord {
9799
severityNumber: toSeverityNumber(log.severityNumber),
98100
severityText: log.severityText,
99101
body: toAnyValue(log.body),
100-
attributes: toAttributes(log.attributes),
102+
attributes: toLogAttributes(log.attributes),
101103
droppedAttributesCount: 0,
102104
flags: log.spanContext?.traceFlags,
103105
traceId: useHex
@@ -119,3 +121,7 @@ function optionalHexToBase64(str: string | undefined): string | undefined {
119121
if (str === undefined) return undefined;
120122
return hexToBase64(str);
121123
}
124+
125+
export function toLogAttributes(attributes: LogAttributes): IKeyValue[] {
126+
return Object.keys(attributes).map(key => toKeyValue(key, attributes[key]));
127+
}

experimental/packages/sdk-logs/package.json

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -69,7 +69,7 @@
6969
"sideEffects": false,
7070
"peerDependencies": {
7171
"@opentelemetry/api": ">=1.4.0 <1.5.0",
72-
"@opentelemetry/api-logs": ">=0.38.0"
72+
"@opentelemetry/api-logs": ">=0.39.1"
7373
},
7474
"devDependencies": {
7575
"@opentelemetry/api": ">=1.4.0 <1.5.0",

experimental/packages/sdk-logs/src/LogRecord.ts

Lines changed: 12 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -14,7 +14,7 @@
1414
* limitations under the License.
1515
*/
1616

17-
import { Attributes, AttributeValue, diag } from '@opentelemetry/api';
17+
import { AttributeValue, diag } from '@opentelemetry/api';
1818
import type * as logsAPI from '@opentelemetry/api-logs';
1919
import * as api from '@opentelemetry/api';
2020
import {
@@ -27,14 +27,15 @@ import type { IResource } from '@opentelemetry/resources';
2727
import type { ReadableLogRecord } from './export/ReadableLogRecord';
2828
import type { LogRecordLimits } from './types';
2929
import { Logger } from './Logger';
30+
import { LogAttributes } from '@opentelemetry/api-logs';
3031

3132
export class LogRecord implements ReadableLogRecord {
3233
readonly hrTime: api.HrTime;
3334
readonly hrTimeObserved: api.HrTime;
3435
readonly spanContext?: api.SpanContext;
3536
readonly resource: IResource;
3637
readonly instrumentationScope: InstrumentationScope;
37-
readonly attributes: Attributes = {};
38+
readonly attributes: logsAPI.LogAttributes = {};
3839
private _severityText?: string;
3940
private _severityNumber?: logsAPI.SeverityNumber;
4041
private _body?: string;
@@ -102,13 +103,20 @@ export class LogRecord implements ReadableLogRecord {
102103
this.setAttributes(attributes);
103104
}
104105

105-
public setAttribute(key: string, value?: AttributeValue) {
106+
public setAttribute(key: string, value?: LogAttributes | AttributeValue) {
106107
if (this._isLogRecordReadonly()) {
107108
return this;
108109
}
109110
if (value === null) {
110111
return this;
111112
}
113+
if (
114+
typeof value === 'object' &&
115+
!Array.isArray(value) &&
116+
Object.keys(value).length > 0
117+
) {
118+
this.attributes[key] = value;
119+
}
112120
if (key.length === 0) {
113121
api.diag.warn(`Invalid attribute key: ${key}`);
114122
return this;
@@ -128,7 +136,7 @@ export class LogRecord implements ReadableLogRecord {
128136
return this;
129137
}
130138

131-
public setAttributes(attributes: Attributes) {
139+
public setAttributes(attributes: LogAttributes) {
132140
for (const [k, v] of Object.entries(attributes)) {
133141
this.setAttribute(k, v);
134142
}

experimental/packages/sdk-logs/src/export/ReadableLogRecord.ts

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -15,9 +15,9 @@
1515
*/
1616

1717
import type { IResource } from '@opentelemetry/resources';
18-
import type { Attributes, HrTime, SpanContext } from '@opentelemetry/api';
18+
import type { HrTime, SpanContext } from '@opentelemetry/api';
1919
import type { InstrumentationScope } from '@opentelemetry/core';
20-
import type { SeverityNumber } from '@opentelemetry/api-logs';
20+
import type { LogAttributes, SeverityNumber } from '@opentelemetry/api-logs';
2121

2222
export interface ReadableLogRecord {
2323
readonly hrTime: HrTime;
@@ -28,5 +28,5 @@ export interface ReadableLogRecord {
2828
readonly body?: string;
2929
readonly resource: IResource;
3030
readonly instrumentationScope: InstrumentationScope;
31-
readonly attributes: Attributes;
31+
readonly attributes: LogAttributes;
3232
}

experimental/packages/sdk-logs/test/common/utils.ts

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -21,11 +21,12 @@ export const validAttributes = {
2121
'array<string>': ['str1', 'str2'],
2222
'array<number>': [1, 2],
2323
'array<bool>': [true, false],
24+
object: { bar: 'foo' },
2425
};
2526

2627
export const invalidAttributes = {
27-
// invalid attribute type object
28-
object: { foo: 'bar' },
28+
// invalid attribute empty object
29+
object: {},
2930
// invalid attribute inhomogeneous array
3031
'non-homogeneous-array': [0, ''],
3132
// This empty length attribute should not be set

0 commit comments

Comments
 (0)