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: finalize logger #213

Open
wants to merge 32 commits into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
Show all changes
32 commits
Select commit Hold shift + click to select a range
a817817
feat: LogRecord limits
yuzurihaaa Jan 16, 2025
b652823
feat: LogRecord
yuzurihaaa Jan 16, 2025
a48d1ad
chore: fix comment
yuzurihaaa Jan 16, 2025
35bbc03
chore: log record limit test
yuzurihaaa Jan 16, 2025
84388a1
feat: LogRecordProcessor
yuzurihaaa Jan 16, 2025
7b60065
feat: NoopLogRecordProcessor
yuzurihaaa Jan 16, 2025
60a5ac3
feat: LoggerConfig
yuzurihaaa Jan 16, 2025
1b7664c
feat: Logger
yuzurihaaa Jan 16, 2025
8dcd67c
feat: LoggerProvider
yuzurihaaa Jan 16, 2025
93867d8
feat: exports
yuzurihaaa Jan 16, 2025
381345b
wip: change processors initialization from `const`
yuzurihaaa Jan 16, 2025
06c67c0
wip: fix test
yuzurihaaa Jan 16, 2025
8ae3118
wip: fix import arrange
yuzurihaaa Jan 20, 2025
f9c8428
wip: add ExportResult
yuzurihaaa Jan 18, 2025
1c1d423
wip: add LogRecordExporter
yuzurihaaa Jan 18, 2025
b748342
wip: add BatchLogRecordProcessor
yuzurihaaa Jan 18, 2025
94918c7
wip: unit test BatchLogRecordProcessor
yuzurihaaa Jan 18, 2025
6c0cc67
wip: add SimpleLogRecordProcessor
yuzurihaaa Jan 18, 2025
956c832
wip: unit test SimpleLogRecordProcessor
yuzurihaaa Jan 18, 2025
129cd69
wip: fix import arrange
yuzurihaaa Jan 20, 2025
2a69d7f
wip: add LogRecordExporter
yuzurihaaa Jan 18, 2025
298bfa6
wip: add ConsoleLogRecordExporter
yuzurihaaa Jan 18, 2025
8621347
wip: unit test ConsoleLogRecordExporter
yuzurihaaa Jan 18, 2025
9d64739
wip: add InMemoryLogRecordExporter
yuzurihaaa Jan 18, 2025
4a85342
wip: unit test InMemoryLogRecordExporter
yuzurihaaa Jan 18, 2025
e1bf474
wip: fix import arrange
yuzurihaaa Jan 20, 2025
08888bd
feat: OTLPLogExporter
yuzurihaaa Jan 20, 2025
6ed93bf
wip: fix import arrange
yuzurihaaa Jan 20, 2025
674917d
wip: move away from experimental
yuzurihaaa Jan 20, 2025
ce475fc
wip: more export
yuzurihaaa Jan 20, 2025
55aa148
wip: update README
yuzurihaaa Jan 20, 2025
940030c
wip: fix lint
yuzurihaaa Jan 20, 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
42 changes: 41 additions & 1 deletion README.md
Original file line number Diff line number Diff line change
Expand Up @@ -8,12 +8,13 @@ This repository is the Dart implementation of the [OpenTelemetry project](https:
| - | - |
| Traces | Beta |
| Metrics | Alpha |
| Logs | Unimplemented |
| Logs | Beta |

## Getting Started

This section will show you how to initialize the OpenTelemetry SDK, capture a span, and propagate context.

### Tacers
### Initialize the OpenTelemetry SDK

```dart
Expand Down Expand Up @@ -62,6 +63,45 @@ void main(List<String> args) {
}
```

### Logs
#### Initialize the OpenTelemetry SDK

```dart
import 'package:opentelemetry/sdk.dart'
show
BatchLogRecordProcessor,
OTLPLogExporter,
ConsoleExporter,
SimpleLogRecordProcessor,
TracerProviderBase;
import 'package:opentelemetry/api.dart'
show registerGlobalLogProvider, globalLogProvider;

void main(List<String> args) {
final logProvider = TracerProviderBase(processors: [
BatchLogRecordProcessor(
OTLPLogExporter(Uri.parse('https://my-collector.com/v1/traces'))),
SimpleLogRecordProcessor(ConsoleLogRecordExporter())
]);

registerGlobalLogProvider(logProvider);
final tracer = globalLogProvider.get('logger-name');
}
```

### Capture a Log
```dart
import 'package:opentelemetry/api.dart' show StatusCode, globalLogProvider;

void main(List<String> args) {
final logger = globalLogProvider.get('logger-name');

logger.emit(body: "Hello World!");
}
```



### Propagate Context

### Intra-process
Expand Down
22 changes: 10 additions & 12 deletions lib/api.dart
Original file line number Diff line number Diff line change
Expand Up @@ -5,22 +5,21 @@ export 'src/api/common/attribute.dart' show Attribute;
export 'src/api/common/resource_attributes.dart' show ResourceAttributes;
export 'src/api/common/semantic_attributes.dart' show SemanticAttributes;
export 'src/api/context/context.dart'
show
Context,
ContextKey,
contextWithSpan,
contextWithSpanContext,
spanContextFromContext,
spanFromContext,
zone;
export 'src/api/context/context_manager.dart'
show globalContextManager, registerGlobalContextManager;
show Context, ContextKey, contextWithSpan, contextWithSpanContext, spanContextFromContext, spanFromContext, zone;
export 'src/api/context/context_manager.dart' show globalContextManager, registerGlobalContextManager;
export 'src/api/exporters/span_exporter.dart' show SpanExporter;
export 'src/api/instrumentation_library.dart' show InstrumentationLibrary;
export 'src/api/logs/log_record.dart' show Severity;
export 'src/api/logs/logger.dart' show Logger;
export 'src/api/logs/logger_provider.dart' show LoggerProvider;
export 'src/api/logs/noop/noop_logger.dart' show NoopLogger;
export 'src/api/logs/noop/noop_logger_provider.dart' show NoopLoggerProvider;
export 'src/api/open_telemetry.dart'
show
globalLogProvider,
globalTextMapPropagator,
globalTracerProvider,
registerGlobalLogProvider,
registerGlobalTextMapPropagator,
registerGlobalTracerProvider,
trace,
Expand All @@ -30,8 +29,7 @@ export 'src/api/open_telemetry.dart'
export 'src/api/propagation/extractors/text_map_getter.dart' show TextMapGetter;
export 'src/api/propagation/injectors/text_map_setter.dart' show TextMapSetter;
export 'src/api/propagation/text_map_propagator.dart' show TextMapPropagator;
export 'src/api/propagation/w3c_trace_context_propagator.dart'
show W3CTraceContextPropagator;
export 'src/api/propagation/w3c_trace_context_propagator.dart' show W3CTraceContextPropagator;
export 'src/api/span_processors/span_processor.dart' show SpanProcessor;
export 'src/api/trace/id_generator.dart' show IdGenerator;
export 'src/api/trace/span.dart' show Span, SpanKind;
Expand Down
36 changes: 22 additions & 14 deletions lib/sdk.dart
Original file line number Diff line number Diff line change
@@ -1,30 +1,38 @@
// 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

export 'src/sdk/instrumentation_library.dart' show InstrumentationLibrary;
export 'src/sdk/common/attributes.dart' show Attributes;
export 'src/sdk/common/instrumentation_scope.dart' show InstrumentationScope;
export 'src/sdk/instrumentation_library.dart' show InstrumentationLibrary;
export 'src/sdk/logs/export_result.dart' show ExportResult, ExportResultCode;
export 'src/sdk/logs/exporters/console_log_record_exporter.dart' show ConsoleLogRecordExporter;
export 'src/sdk/logs/exporters/inmemory_log_record_exporter.dart' show InMemoryLogRecordExporter;
export 'src/sdk/logs/exporters/log_record_exporter.dart' show LogRecordExporter;
export 'src/sdk/logs/exporters/otlp_log_exporter.dart' show OTLPLogExporter;
export 'src/sdk/logs/log_record.dart' show ReadableLogRecord, ReadWriteLogRecord, LogRecord;
export 'src/sdk/logs/log_record_limit.dart' show LogRecordLimits;
export 'src/sdk/logs/logger.dart' show Logger;
export 'src/sdk/logs/logger_provider.dart' show LoggerProvider;
export 'src/sdk/logs/processors/batch_log_record_processor.dart' show BatchLogRecordProcessor;
export 'src/sdk/logs/processors/log_record_processor.dart' show LogRecordProcessor;
export 'src/sdk/logs/processors/noop_log_processor.dart' show NoopLogRecordProcessor;
export 'src/sdk/logs/processors/simple_log_record_processor.dart' show SimpleLogRecordProcessor;
export 'src/sdk/resource/resource.dart' show Resource;
export 'src/sdk/time_providers/datetime_time_provider.dart'
show DateTimeTimeProvider;
export 'src/sdk/time_providers/datetime_time_provider.dart' show DateTimeTimeProvider;
export 'src/sdk/time_providers/time_provider.dart' show TimeProvider;
export 'src/sdk/trace/exporters/span_exporter.dart' show SpanExporter;
export 'src/sdk/trace/exporters/collector_exporter.dart' show CollectorExporter;
export 'src/sdk/trace/exporters/console_exporter.dart' show ConsoleExporter;
export 'src/sdk/trace/exporters/span_exporter.dart' show SpanExporter;
export 'src/sdk/trace/id_generator.dart' show IdGenerator;
export 'src/sdk/trace/read_only_span.dart' show ReadOnlySpan;
export 'src/sdk/trace/read_write_span.dart' show ReadWriteSpan;
export 'src/sdk/trace/sampling/always_off_sampler.dart' show AlwaysOffSampler;
export 'src/sdk/trace/sampling/always_on_sampler.dart' show AlwaysOnSampler;
export 'src/sdk/trace/sampling/parent_based_sampler.dart'
show ParentBasedSampler;
export 'src/sdk/trace/sampling/parent_based_sampler.dart' show ParentBasedSampler;
export 'src/sdk/trace/sampling/sampler.dart' show Sampler;
export 'src/sdk/trace/sampling/sampling_result.dart'
show Decision, SamplingResult;
export 'src/sdk/trace/read_only_span.dart' show ReadOnlySpan;
export 'src/sdk/trace/read_write_span.dart' show ReadWriteSpan;
export 'src/sdk/trace/sampling/sampling_result.dart' show Decision, SamplingResult;
export 'src/sdk/trace/span_limits.dart' show SpanLimits;
export 'src/sdk/trace/span_processors/batch_processor.dart' show BatchSpanProcessor;
export 'src/sdk/trace/span_processors/simple_processor.dart' show SimpleSpanProcessor;
export 'src/sdk/trace/span_processors/span_processor.dart' show SpanProcessor;
export 'src/sdk/trace/span_processors/batch_processor.dart'
show BatchSpanProcessor;
export 'src/sdk/trace/span_processors/simple_processor.dart'
show SimpleSpanProcessor;
export 'src/sdk/trace/tracer_provider.dart' show TracerProviderBase;
2 changes: 1 addition & 1 deletion lib/src/api/context/context.dart
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,7 @@ import 'package:collection/collection.dart';
import 'package:logging/logging.dart';
import 'package:meta/meta.dart';

import '../../../api.dart';
import '../../../api.dart' hide Logger;
import '../trace/nonrecording_span.dart' show NonRecordingSpan;

final Logger _log = Logger('opentelemetry');
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
9 changes: 4 additions & 5 deletions lib/src/api/open_telemetry.dart
Original file line number Diff line number Diff line change
Expand Up @@ -6,20 +6,19 @@ import 'dart:async';
import 'package:meta/meta.dart';

import '../../api.dart' as api;
import '../experimental_api.dart';
import 'propagation/noop_text_map_propagator.dart';
import 'trace/noop_tracer_provider.dart';

final api.TracerProvider _noopTracerProvider = NoopTracerProvider();
final api.TextMapPropagator _noopTextMapPropagator = NoopTextMapPropagator();
final LoggerProvider _noopLoggerProvider = NoopLoggerProvider();
LoggerProvider _logProvider = _noopLoggerProvider;
final api.LoggerProvider _noopLoggerProvider = api.NoopLoggerProvider();
api.LoggerProvider _logProvider = _noopLoggerProvider;
api.TracerProvider _tracerProvider = _noopTracerProvider;
api.TextMapPropagator _textMapPropagator = _noopTextMapPropagator;

api.TracerProvider get globalTracerProvider => _tracerProvider;

LoggerProvider get globalLogProvider => _logProvider;
api.LoggerProvider get globalLogProvider => _logProvider;

api.TextMapPropagator get globalTextMapPropagator => _textMapPropagator;

Expand All @@ -33,7 +32,7 @@ void registerGlobalTracerProvider(api.TracerProvider tracerProvider) {
_tracerProvider = tracerProvider;
}

void registerGlobalLogProvider(LoggerProvider logProvider) {
void registerGlobalLogProvider(api.LoggerProvider logProvider) {
if (_logProvider != _noopLoggerProvider) {
throw StateError('A global LoggerProvider has already been created. '
'registerGlobalLoggerProvider must be called only once before any '
Expand Down
7 changes: 1 addition & 6 deletions lib/src/experimental_api.dart
Original file line number Diff line number Diff line change
Expand Up @@ -11,12 +11,7 @@ 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/meter_provider.dart' show MeterProvider;
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_provider.dart' show LoggerProvider;
export 'api/logs/noop/noop_logger.dart' show NoopLogger;
export 'api/logs/noop/noop_logger_provider.dart' show NoopLoggerProvider;
2 changes: 1 addition & 1 deletion lib/src/experimental_sdk.dart
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,6 @@ library experimental_sdk;
import 'package:meta/meta.dart';

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;
19 changes: 19 additions & 0 deletions lib/src/sdk/common/limits.dart
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@ import 'package:meta/meta.dart';

import '../../../api.dart' as api;
import '../../../sdk.dart' as sdk;
import '../logs/log_record_limit.dart';

/// Applies given [sdk.SpanLimits] to a list of [api.SpanLink]s.
@protected
Expand Down Expand Up @@ -77,6 +78,24 @@ api.Attribute applyAttributeLimits(api.Attribute attr, sdk.SpanLimits limits) {
return attr;
}

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

if (attr.value is String) {
attr = api.Attribute.fromString(
attr.key, applyAttributeLengthLimit(attr.value as String, limits.attributeValueLengthLimit));
} 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.attributeValueLengthLimit);
}
attr = api.Attribute.fromStringList(attr.key, listString);
}
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,
}
64 changes: 64 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,64 @@
// 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/api.dart' as api;
import 'package:opentelemetry/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 = {};
if (log.spanContext != null) {
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 ?? <String>[])
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 ?? <api.Attribute>[]) attribute.key: attribute.value,
}
},
'timestamp': log.timeStamp,
'severityText': log.severityText,
'severityNumber': log.severityNumber,
'body': log.body,
'attributes': {
for (final attribute in log.attributes?.keys ?? <String>[]) 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/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/sdk.dart' as sdk;

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

Future<void> shutdown();
}
Loading
Loading