Skip to content

feat: logger exporter ConsoleLogRecordExporter and InMemoryLogRecordExporter #211

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

Open
wants to merge 53 commits into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
Show all changes
53 commits
Select commit Hold shift + click to select a range
dbda356
feat: LogRecord limits
yuzurihaaa Jan 16, 2025
630f2cf
feat: LogRecord
yuzurihaaa Jan 16, 2025
7119f8e
chore: fix comment
yuzurihaaa Jan 16, 2025
6fb8422
chore: log record limit test
yuzurihaaa Jan 16, 2025
69e4e18
feat: LogRecordProcessor
yuzurihaaa Jan 16, 2025
79640fb
feat: NoopLogRecordProcessor
yuzurihaaa Jan 16, 2025
8ff6c71
feat: LoggerConfig
yuzurihaaa Jan 16, 2025
d3d98e6
feat: Logger
yuzurihaaa Jan 16, 2025
f4b05aa
feat: LoggerProvider
yuzurihaaa Jan 16, 2025
ff5d1e3
feat: exports
yuzurihaaa Jan 16, 2025
1e050b4
wip: change processors initialization from `const`
yuzurihaaa Jan 16, 2025
5eed62e
wip: fix test
yuzurihaaa Jan 16, 2025
99e374f
wip: fix import arrange
yuzurihaaa Jan 20, 2025
38b2137
wip: attach copyright
yuzurihaaa Jan 20, 2025
88eb916
wip: change from `api.SpanContext?` and `api.Context?` to non-null pr…
yuzurihaaa Jan 31, 2025
014f087
wip: remove unused `includeTraceContext`
yuzurihaaa Jan 31, 2025
96938b8
wip: opentelemetry as defaultLoggerName
yuzurihaaa Jan 31, 2025
2a9a80a
wip: make log processors immutable
yuzurihaaa Jan 31, 2025
f0206c6
wip: make all variables private
yuzurihaaa Jan 31, 2025
01636f9
wip: update limit to only create if value changes.
yuzurihaaa Jan 31, 2025
aae1c3e
wip: change from sdk.Attributes to List<api.Attribute>
yuzurihaaa Jan 31, 2025
464a636
wip: change to `DateTime`
yuzurihaaa Feb 10, 2025
2ea0fcf
wip: remove DateTime initialization
yuzurihaaa Feb 10, 2025
078bd4a
wip: fix test
yuzurihaaa Feb 10, 2025
90ecfdc
wip: remove `processors`
yuzurihaaa Feb 12, 2025
4ab30a2
wip: remove `async` `await`
yuzurihaaa Feb 12, 2025
b3f744e
wip: changed to `FutureOr`
yuzurihaaa Feb 12, 2025
9978329
wip: change forceFlush and shutdown to `void`
yuzurihaaa Feb 13, 2025
849e0b0
wip: fix test
yuzurihaaa Feb 13, 2025
bebc35e
wip: change LogRecordLimits from abstract class to class
yuzurihaaa Mar 3, 2025
032fd65
wip: avoid nullable values
yuzurihaaa Mar 3, 2025
442b466
wip: mark protected
yuzurihaaa Mar 3, 2025
3fc382e
wip: remove nullable and pass processors instead of callback.
yuzurihaaa Mar 3, 2025
f0e65ab
fix: failing unit test
yuzurihaaa Mar 3, 2025
b97de95
wip: remove NoopLogRecordProcessor
yuzurihaaa Mar 4, 2025
d9c0429
wip: inline
yuzurihaaa Mar 4, 2025
37fae26
wip: limit list
yuzurihaaa Mar 4, 2025
b9598de
wip: onEmit ReadableLogRecord -> ReadWriteLogRecord
yuzurihaaa Mar 5, 2025
741ed89
wip: update DateTimeProvider
yuzurihaaa Mar 18, 2025
4cb0cbb
wip: add ExportResult
yuzurihaaa Jan 18, 2025
6912ab9
wip: add LogRecordExporter
yuzurihaaa Jan 18, 2025
20c3edd
wip: add BatchLogRecordProcessor
yuzurihaaa Jan 18, 2025
813f433
wip: unit test BatchLogRecordProcessor
yuzurihaaa Jan 18, 2025
4ac0c9a
wip: add SimpleLogRecordProcessor
yuzurihaaa Jan 18, 2025
d3de3f2
wip: unit test SimpleLogRecordProcessor
yuzurihaaa Jan 18, 2025
4b32579
feat: rebase from logger-provider and fix error
yuzurihaaa Mar 3, 2025
9eed89d
wip: add ConsoleLogRecordExporter
yuzurihaaa Jan 18, 2025
2b7a52a
wip: unit test ConsoleLogRecordExporter
yuzurihaaa Jan 18, 2025
3f8ed8f
wip: add InMemoryLogRecordExporter
yuzurihaaa Jan 18, 2025
1f8ee91
wip: unit test InMemoryLogRecordExporter
yuzurihaaa Jan 18, 2025
1f6b041
wip: fix test error
yuzurihaaa Mar 3, 2025
f7d9433
wip: fix lint
yuzurihaaa Mar 3, 2025
efb46dc
fix: test
yuzurihaaa Mar 3, 2025
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
3 changes: 1 addition & 2 deletions lib/src/api/logs/logger.dart
Original file line number Diff line number Diff line change
Expand Up @@ -2,12 +2,11 @@
// Licensed under the Apache License, Version 2.0. Please see https://github.com/Workiva/opentelemetry-dart/blob/master/LICENSE for more information

import '../../../api.dart' as api;
import '../../../sdk.dart' as sdk;
import 'log_record.dart';

abstract class Logger {
void emit({
sdk.Attributes? attributes,
List<api.Attribute> attributes = const <api.Attribute>[],
api.Context? context,
dynamic body,
DateTime? observedTimestamp,
Expand Down
2 changes: 1 addition & 1 deletion lib/src/api/logs/logger_provider.dart
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,7 @@ import 'package:opentelemetry/src/api/logs/logger.dart';
abstract class LoggerProvider {
/// Gets or creates a [Logger] instance.
///
/// The meter is identified by the combination of [name], [version],
/// The logger is identified by the combination of [name], [version],
/// [schemaUrl] and [attributes]. The [name] SHOULD uniquely identify the
/// instrumentation scope, such as the instrumentation library
/// (e.g. io.opentelemetry.contrib.mongodb), package, module or class name.
Expand Down
4 changes: 2 additions & 2 deletions lib/src/api/logs/noop/noop_logger.dart
Original file line number Diff line number Diff line change
@@ -1,17 +1,17 @@
// Copyright 2021-2022 Workiva.
// Licensed under the Apache License, Version 2.0. Please see https://github.com/Workiva/opentelemetry-dart/blob/master/LICENSE for more information

import 'package:opentelemetry/src/api/common/attribute.dart';
import 'package:opentelemetry/src/api/context/context.dart';
import 'package:opentelemetry/src/api/logs/log_record.dart';
import 'package:opentelemetry/src/api/logs/logger.dart';
import 'package:opentelemetry/src/sdk/common/attributes.dart';

class NoopLogger implements Logger {
const NoopLogger();

@override
void emit({
Attributes? attributes,
List<Attribute> attributes = const <Attribute>[],
Context? context,
dynamic body,
DateTime? observedTimestamp,
Expand Down
12 changes: 6 additions & 6 deletions lib/src/experimental_api.dart
Original file line number Diff line number Diff line change
Expand Up @@ -10,13 +10,13 @@ export 'api/context/context_manager.dart' show ContextManager;
export 'api/context/noop_context_manager.dart' show NoopContextManager;
export 'api/context/zone_context.dart' show ZoneContext;
export 'api/context/zone_context_manager.dart' show ZoneContextManager;
export 'api/metrics/counter.dart' show Counter;
export 'api/metrics/meter_provider.dart' show MeterProvider;
export 'api/metrics/meter.dart' show Meter;
export 'api/metrics/noop/noop_meter.dart' show NoopMeter;
export 'api/trace/nonrecording_span.dart' show NonRecordingSpan;
export 'api/logs/logger.dart' show Logger;
export 'api/logs/log_record.dart' show Severity;
export 'api/logs/logger.dart' show Logger;
export 'api/logs/logger_provider.dart' show LoggerProvider;
export 'api/logs/noop/noop_logger.dart' show NoopLogger;
export 'api/logs/noop/noop_logger_provider.dart' show NoopLoggerProvider;
export 'api/metrics/counter.dart' show Counter;
export 'api/metrics/meter.dart' show Meter;
export 'api/metrics/meter_provider.dart' show MeterProvider;
export 'api/metrics/noop/noop_meter.dart' show NoopMeter;
export 'api/trace/nonrecording_span.dart' show NonRecordingSpan;
12 changes: 11 additions & 1 deletion lib/src/experimental_sdk.dart
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,17 @@ library experimental_sdk;

import 'package:meta/meta.dart';

export 'sdk/logs/export_result.dart' show ExportResult, ExportResultCode;
export 'sdk/logs/exporters/console_log_record_exporter.dart' show ConsoleLogRecordExporter;
export 'sdk/logs/exporters/inmemory_log_record_exporter.dart' show InMemoryLogRecordExporter;
export 'sdk/logs/exporters/log_record_exporter.dart' show LogRecordExporter;
export 'sdk/logs/log_record.dart' show ReadableLogRecord, ReadWriteLogRecord, LogRecord;
export 'sdk/logs/log_record_limit.dart' show LogRecordLimits;
export 'sdk/logs/logger.dart' show Logger;
export 'sdk/logs/logger_provider.dart' show LoggerProvider;
export 'sdk/logs/processors/batch_log_record_processor.dart' show BatchLogRecordProcessor;
export 'sdk/logs/processors/log_record_processor.dart' show LogRecordProcessor;
export 'sdk/metrics/counter.dart' show Counter;
export 'sdk/metrics/meter_provider.dart' show MeterProvider;
export 'sdk/metrics/meter.dart' show Meter;
export 'sdk/metrics/meter_provider.dart' show MeterProvider;
export 'sdk/resource/resource.dart' show Resource;
46 changes: 35 additions & 11 deletions lib/src/sdk/common/limits.dart
Original file line number Diff line number Diff line change
Expand Up @@ -4,11 +4,11 @@ import 'package:meta/meta.dart';

import '../../../api.dart' as api;
import '../../../sdk.dart' as sdk;
import '../../experimental_sdk.dart' as sdk;

/// Applies given [sdk.SpanLimits] to a list of [api.SpanLink]s.
@protected
List<api.SpanLink> applyLinkLimits(
List<api.SpanLink> links, sdk.SpanLimits limits) {
List<api.SpanLink> applyLinkLimits(List<api.SpanLink> links, sdk.SpanLimits limits) {
final spanLink = <api.SpanLink>[];

for (final link in links) {
Expand All @@ -27,8 +27,7 @@ List<api.SpanLink> applyLinkLimits(
for (final attr in link.attributes) {
// if attributes num is already greater than maxNumAttributesPerLink
// and this key doesn't exist in the list, drop it.
if (attributeMap.length >= limits.maxNumAttributesPerLink &&
!attributeMap.containsKey(attr.key)) {
if (attributeMap.length >= limits.maxNumAttributesPerLink && !attributeMap.containsKey(attr.key)) {
droppedAttributes++;
continue;
}
Expand All @@ -49,8 +48,7 @@ List<api.SpanLink> applyLinkLimits(
}
}

spanLink.add(api.SpanLink(link.context,
attributes: linkAttributes, droppedAttributes: droppedAttributes));
spanLink.add(api.SpanLink(link.context, attributes: linkAttributes, droppedAttributes: droppedAttributes));
}
return spanLink;
}
Expand All @@ -63,20 +61,46 @@ api.Attribute applyAttributeLimits(api.Attribute attr, sdk.SpanLimits limits) {

if (attr.value is String) {
attr = api.Attribute.fromString(
attr.key,
applyAttributeLengthLimit(
attr.value as String, limits.maxNumAttributeLength));
attr.key, applyAttributeLengthLimit(attr.value as String, limits.maxNumAttributeLength));
} else if (attr.value is List<String>) {
final listString = attr.value as List<String>;
for (var j = 0; j < listString.length; j++) {
listString[j] = applyAttributeLengthLimit(
listString[j], limits.maxNumAttributeLength);
listString[j] = applyAttributeLengthLimit(listString[j], limits.maxNumAttributeLength);
}
attr = api.Attribute.fromStringList(attr.key, listString);
}
return attr;
}

@protected
api.Attribute applyAttributeLimitsForLog(
api.Attribute attr,
sdk.LogRecordLimits limits,
) {
// if maxNumAttributeLength is less than zero, then it has unlimited length.
if (limits.attributeValueLengthLimit < 0) return attr;

if (attr.value is String) {
return (attr.value as String).length > limits.attributeValueLengthLimit
? api.Attribute.fromString(attr.key, (attr.value as String).substring(0, limits.attributeValueLengthLimit))
: attr;
} else if (attr.value is List<String>) {
final list = (attr.value as List<String>);
List<String>? truncated;
for (int i = 0; i < list.length; i++) {
final s = list[i];
if (s.length > limits.attributeValueLengthLimit) {
truncated ??= List<String>.from(list, growable: false);
truncated[i] = s.substring(0, limits.attributeValueLengthLimit);
}
}
if (truncated != null) {
return api.Attribute.fromStringList(attr.key, truncated);
}
}
return attr;
}

/// Truncate just strings which length is longer than configuration.
/// Reference: https://github.com/open-telemetry/opentelemetry-java/blob/14ffacd1cdd22f5aa556eeda4a569c7f144eadf2/sdk/common/src/main/java/io/opentelemetry/sdk/internal/AttributeUtil.java#L80
@protected
Expand Down
15 changes: 15 additions & 0 deletions lib/src/sdk/logs/export_result.dart
Original file line number Diff line number Diff line change
@@ -0,0 +1,15 @@
// Copyright 2021-2022 Workiva.
// Licensed under the Apache License, Version 2.0. Please see https://github.com/Workiva/opentelemetry-dart/blob/master/LICENSE for more information

class ExportResult {
final ExportResultCode code;
final Exception? error;
final StackTrace? stackTrace;

ExportResult({required this.code, this.error, this.stackTrace});
}

enum ExportResultCode {
success,
failed,
}
61 changes: 61 additions & 0 deletions lib/src/sdk/logs/exporters/console_log_record_exporter.dart
Original file line number Diff line number Diff line change
@@ -0,0 +1,61 @@
// Copyright 2021-2022 Workiva.
// Licensed under the Apache License, Version 2.0. Please see https://github.com/Workiva/opentelemetry-dart/blob/master/LICENSE for more information

import 'package:opentelemetry/src/experimental_sdk.dart' as sdk;

/// This is implementation of [sdk.ReadWriteLogRecordExporter] that prints LogRecords to the
/// console. This class can be used for diagnostic purposes.
///
/// NOTE: This [sdk.LogRecordExporter] is intended for diagnostics use only, output rendered to the console may change at any time.
class ConsoleLogRecordExporter implements sdk.LogRecordExporter {
@override
Future<sdk.ExportResult> export(List<sdk.ReadableLogRecord> logs) async {
return _sendLogRecords(logs);
}

/// Shutdown the exporter.
@override
Future<void> shutdown() async {}

/// Showing logs in console
sdk.ExportResult _sendLogRecords(List<sdk.ReadableLogRecord> logs) {
for (final log in logs) {
print(_makeObject(log));
}
return sdk.ExportResult(code: sdk.ExportResultCode.success);
}

/// converts logRecord info into more readable format
Map<String, dynamic> _makeObject(sdk.ReadableLogRecord log) {
final contextInfo = {};
contextInfo.addAll({
'traceId': log.spanContext.traceId,
'spanId': log.spanContext.spanId,
'traceFlags': log.spanContext.traceFlags,
});
return {
'resource': {
'attributes': {
for (final attribute in log.resource.attributes.keys)
attribute: log.resource.attributes.get(attribute),
},
},
'instrumentationScope': {
'name': log.instrumentationScope.name,
'version': log.instrumentationScope.version,
'schemaUrl': log.instrumentationScope.schemaUrl,
'attributes': {
for (final attribute in log.instrumentationScope.attributes) attribute.key: attribute.value,
}
},
'timestamp': log.timeStamp,
'severityText': log.severityText,
'severityNumber': log.severityNumber,
'body': log.body,
'attributes': {
for (final attribute in log.attributes.keys) attribute: log.resource.attributes.get(attribute),
},
...contextInfo,
};
}
}
43 changes: 43 additions & 0 deletions lib/src/sdk/logs/exporters/inmemory_log_record_exporter.dart
Original file line number Diff line number Diff line change
@@ -0,0 +1,43 @@
// Copyright 2021-2022 Workiva.
// Licensed under the Apache License, Version 2.0. Please see https://github.com/Workiva/opentelemetry-dart/blob/master/LICENSE for more information

import 'package:meta/meta.dart';
import 'package:opentelemetry/src/experimental_sdk.dart' as sdk;

/// This class can be used for testing purposes. It stores the exported LogRecords
/// in a list in memory that can be retrieved using the `getFinishedLogRecords()`
/// method.
class InMemoryLogRecordExporter implements sdk.LogRecordExporter {
var _finishedLogRecords = <sdk.ReadableLogRecord>[];

/// Indicates if the exporter has been "shutdown."
/// When false, exported log records will not be stored in-memory.
@protected
bool _stopped = false;

@override
Future<sdk.ExportResult> export(List<sdk.ReadableLogRecord> logs) async {
if (_stopped) {
return sdk.ExportResult(
code: sdk.ExportResultCode.failed,
error: Exception('Exporter has been stopped'),
stackTrace: StackTrace.current,
);
}
_finishedLogRecords.addAll(logs);

return sdk.ExportResult(code: sdk.ExportResultCode.success);
}

@override
Future<void> shutdown() async {
_stopped = true;
reset();
}

List<sdk.ReadableLogRecord> get finishedLogRecords => List.unmodifiable(_finishedLogRecords);

void reset() {
_finishedLogRecords = <sdk.ReadableLogRecord>[];
}
}
10 changes: 10 additions & 0 deletions lib/src/sdk/logs/exporters/log_record_exporter.dart
Original file line number Diff line number Diff line change
@@ -0,0 +1,10 @@
// Copyright 2021-2022 Workiva.
// Licensed under the Apache License, Version 2.0. Please see https://github.com/Workiva/opentelemetry-dart/blob/master/LICENSE for more information

import 'package:opentelemetry/src/experimental_sdk.dart' as sdk;

abstract class LogRecordExporter {
Future<sdk.ExportResult> export(List<sdk.ReadableLogRecord> logs);

Future<void> shutdown();
}
Loading