|
| 1 | +// Copyright 2021-2022 Workiva. |
| 2 | +// Licensed under the Apache License, Version 2.0. Please see https://github.com/Workiva/opentelemetry-dart/blob/master/LICENSE for more information |
| 3 | + |
| 4 | +@TestOn('vm') |
| 5 | +import 'package:fixnum/fixnum.dart'; |
| 6 | +import 'package:mocktail/mocktail.dart'; |
| 7 | +import 'package:opentelemetry/api.dart' as api; |
| 8 | +import 'package:opentelemetry/sdk.dart' as sdk; |
| 9 | +import 'package:opentelemetry/src/experimental_api.dart' as api; |
| 10 | +import 'package:opentelemetry/src/experimental_sdk.dart' as sdk; |
| 11 | +import 'package:opentelemetry/src/sdk/logs/log_record_limit.dart'; |
| 12 | +import 'package:test/test.dart'; |
| 13 | + |
| 14 | +import '../../../mocks.dart'; |
| 15 | + |
| 16 | +void main() { |
| 17 | + test('test processor', () async { |
| 18 | + final exporter = MockLogRecordExporter(); |
| 19 | + when(() => exporter.export(any())).thenAnswer((_) async => sdk.ExportResult(code: sdk.ExportResultCode.success)); |
| 20 | + final processor = sdk.BatchLogRecordProcessor( |
| 21 | + exporter: exporter, |
| 22 | + scheduledDelayMillis: Duration.zero.inMilliseconds, |
| 23 | + ); |
| 24 | + |
| 25 | + final tracer = sdk.TracerProviderBase().getTracer('test'); |
| 26 | + final parent = tracer.startSpan('parent'); |
| 27 | + final context = api.contextWithSpan(api.Context.current, parent); |
| 28 | + final logRecord = sdk.LogRecord( |
| 29 | + instrumentationScope: sdk.InstrumentationScope('library_name', 'library_version', 'url://schema', []), |
| 30 | + logRecordLimits: LogRecordLimitsImpl(), |
| 31 | + context: context, |
| 32 | + timeProvider: FakeTimeProvider(now: Int64(123))); |
| 33 | + final logRecordA = logRecord |
| 34 | + ..body = 'test log' |
| 35 | + ..severityNumber = api.Severity.fatal3; |
| 36 | + processor.onEmit(logRecordA); |
| 37 | + |
| 38 | + await Future.delayed(const Duration(milliseconds: 100)); |
| 39 | + verify(() => exporter.export(any<List<sdk.ReadableLogRecord>>( |
| 40 | + that: predicate<List<sdk.ReadableLogRecord>>((a) { |
| 41 | + final first = a.first; |
| 42 | + return first.body == 'test log' && |
| 43 | + first.spanContext?.spanId == parent.spanContext.spanId && |
| 44 | + first.severityNumber == api.Severity.fatal3; |
| 45 | + }), |
| 46 | + ))).called(1); |
| 47 | + }); |
| 48 | + |
| 49 | + test('processor shut down', () async { |
| 50 | + final exporter = MockLogRecordExporter(); |
| 51 | + when(exporter.shutdown).thenAnswer((_) async => sdk.ExportResult(code: sdk.ExportResultCode.success)); |
| 52 | + |
| 53 | + final processor = sdk.BatchLogRecordProcessor( |
| 54 | + exporter: exporter, |
| 55 | + scheduledDelayMillis: Duration.zero.inMilliseconds, |
| 56 | + ); |
| 57 | + |
| 58 | + await processor.shutdown(); |
| 59 | + |
| 60 | + verify(exporter.shutdown).called(1); |
| 61 | + }); |
| 62 | + |
| 63 | + test('processor shut down will not emit log', () async { |
| 64 | + final exporter = MockLogRecordExporter(); |
| 65 | + when(exporter.shutdown).thenAnswer((_) async => sdk.ExportResult(code: sdk.ExportResultCode.success)); |
| 66 | + |
| 67 | + final processor = sdk.BatchLogRecordProcessor( |
| 68 | + exporter: exporter, |
| 69 | + scheduledDelayMillis: Duration.zero.inMilliseconds, |
| 70 | + ); |
| 71 | + |
| 72 | + await processor.shutdown(); |
| 73 | + final logRecord = sdk.LogRecord( |
| 74 | + instrumentationScope: sdk.InstrumentationScope('library_name', 'library_version', 'url://schema', []), |
| 75 | + logRecordLimits: LogRecordLimitsImpl(), |
| 76 | + ); |
| 77 | + final logRecordA = logRecord |
| 78 | + ..body = 'test log' |
| 79 | + ..severityNumber = api.Severity.fatal3; |
| 80 | + processor.onEmit(logRecordA); |
| 81 | + |
| 82 | + await Future.delayed(const Duration(milliseconds: 100)); |
| 83 | + |
| 84 | + verifyNever(() => exporter.export(any())); |
| 85 | + }); |
| 86 | + |
| 87 | + test('processor force flush', () async { |
| 88 | + final exporter = MockLogRecordExporter(); |
| 89 | + when(() => exporter.export(any())).thenAnswer((_) async => sdk.ExportResult(code: sdk.ExportResultCode.success)); |
| 90 | + |
| 91 | + final processor = sdk.BatchLogRecordProcessor( |
| 92 | + exporter: exporter, |
| 93 | + scheduledDelayMillis: Duration(seconds: 5).inMilliseconds, |
| 94 | + ); |
| 95 | + |
| 96 | + final logRecord = sdk.LogRecord( |
| 97 | + instrumentationScope: sdk.InstrumentationScope('library_name', 'library_version', 'url://schema', []), |
| 98 | + logRecordLimits: LogRecordLimitsImpl(), |
| 99 | + ); |
| 100 | + final logRecordA = logRecord |
| 101 | + ..body = 'test log' |
| 102 | + ..severityNumber = api.Severity.fatal3; |
| 103 | + processor.onEmit(logRecordA); |
| 104 | + |
| 105 | + await processor.forceFlush(); |
| 106 | + |
| 107 | + verify(() => exporter.export(any<List<sdk.ReadableLogRecord>>( |
| 108 | + that: predicate<List<sdk.ReadableLogRecord>>((a) { |
| 109 | + final first = a.first; |
| 110 | + return first.body == 'test log' && first.severityNumber == api.Severity.fatal3; |
| 111 | + }), |
| 112 | + ))).called(1); |
| 113 | + }); |
| 114 | +} |
0 commit comments