Skip to content

Commit 2dbeef5

Browse files
committed
update callback signature, always pass hint
1 parent dc7f3f0 commit 2dbeef5

File tree

8 files changed

+82
-15
lines changed

8 files changed

+82
-15
lines changed

CHANGELOG.md

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -6,6 +6,10 @@
66

77
- Add Hint support to `beforeSendLog` and pass LogRecord from sentry_logging ([#3549](https://github.com/getsentry/sentry-dart/pull/3549))
88

9+
### Breaking Changes
10+
11+
- `BeforeSendLogCallback` now takes `Hint` as a required positional parameter (matching `BeforeSendCallback` and `BeforeSendTransactionCallback`) ([#3549](https://github.com/getsentry/sentry-dart/pull/3549))
12+
913
## 9.16.0
1014

1115
### Dependencies

packages/dart/lib/src/sentry_options.dart

Lines changed: 4 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -710,8 +710,10 @@ typedef BeforeBreadcrumbCallback = Breadcrumb? Function(
710710

711711
/// This function is called right before a log is about to be sent.
712712
/// Can return a modified log or null to drop the log.
713-
typedef BeforeSendLogCallback = FutureOr<SentryLog?> Function(SentryLog log,
714-
{Hint? hint});
713+
typedef BeforeSendLogCallback = FutureOr<SentryLog?> Function(
714+
SentryLog log,
715+
Hint hint,
716+
);
715717

716718
/// This function is called right before a metric is about to be emitted.
717719
/// Can return true to emit the metric, or false to drop it.

packages/dart/lib/src/telemetry/log/default_logger.dart

Lines changed: 18 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -126,13 +126,15 @@ final class _DefaultSentryLoggerFormatter implements SentryLoggerFormatter {
126126
String templateBody,
127127
List<dynamic> arguments, {
128128
Map<String, SentryAttribute>? attributes,
129+
Hint? hint,
129130
}) {
130131
return _format(
131132
templateBody,
132133
arguments,
133134
attributes,
134135
(formattedBody, allAttributes) {
135-
return _logger.trace(formattedBody, attributes: allAttributes);
136+
return _logger.trace(formattedBody,
137+
attributes: allAttributes, hint: hint);
136138
},
137139
);
138140
}
@@ -142,13 +144,15 @@ final class _DefaultSentryLoggerFormatter implements SentryLoggerFormatter {
142144
String templateBody,
143145
List<dynamic> arguments, {
144146
Map<String, SentryAttribute>? attributes,
147+
Hint? hint,
145148
}) {
146149
return _format(
147150
templateBody,
148151
arguments,
149152
attributes,
150153
(formattedBody, allAttributes) {
151-
return _logger.debug(formattedBody, attributes: allAttributes);
154+
return _logger.debug(formattedBody,
155+
attributes: allAttributes, hint: hint);
152156
},
153157
);
154158
}
@@ -158,13 +162,15 @@ final class _DefaultSentryLoggerFormatter implements SentryLoggerFormatter {
158162
String templateBody,
159163
List<dynamic> arguments, {
160164
Map<String, SentryAttribute>? attributes,
165+
Hint? hint,
161166
}) {
162167
return _format(
163168
templateBody,
164169
arguments,
165170
attributes,
166171
(formattedBody, allAttributes) {
167-
return _logger.info(formattedBody, attributes: allAttributes);
172+
return _logger.info(formattedBody,
173+
attributes: allAttributes, hint: hint);
168174
},
169175
);
170176
}
@@ -174,13 +180,15 @@ final class _DefaultSentryLoggerFormatter implements SentryLoggerFormatter {
174180
String templateBody,
175181
List<dynamic> arguments, {
176182
Map<String, SentryAttribute>? attributes,
183+
Hint? hint,
177184
}) {
178185
return _format(
179186
templateBody,
180187
arguments,
181188
attributes,
182189
(formattedBody, allAttributes) {
183-
return _logger.warn(formattedBody, attributes: allAttributes);
190+
return _logger.warn(formattedBody,
191+
attributes: allAttributes, hint: hint);
184192
},
185193
);
186194
}
@@ -190,13 +198,15 @@ final class _DefaultSentryLoggerFormatter implements SentryLoggerFormatter {
190198
String templateBody,
191199
List<dynamic> arguments, {
192200
Map<String, SentryAttribute>? attributes,
201+
Hint? hint,
193202
}) {
194203
return _format(
195204
templateBody,
196205
arguments,
197206
attributes,
198207
(formattedBody, allAttributes) {
199-
return _logger.error(formattedBody, attributes: allAttributes);
208+
return _logger.error(formattedBody,
209+
attributes: allAttributes, hint: hint);
200210
},
201211
);
202212
}
@@ -206,13 +216,15 @@ final class _DefaultSentryLoggerFormatter implements SentryLoggerFormatter {
206216
String templateBody,
207217
List<dynamic> arguments, {
208218
Map<String, SentryAttribute>? attributes,
219+
Hint? hint,
209220
}) {
210221
return _format(
211222
templateBody,
212223
arguments,
213224
attributes,
214225
(formattedBody, allAttributes) {
215-
return _logger.fatal(formattedBody, attributes: allAttributes);
226+
return _logger.fatal(formattedBody,
227+
attributes: allAttributes, hint: hint);
216228
},
217229
);
218230
}

packages/dart/lib/src/telemetry/log/log_capture_pipeline.dart

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -40,7 +40,7 @@ class LogCapturePipeline {
4040
SentryLog? processedLog = log;
4141
if (beforeSendLog != null) {
4242
try {
43-
final callbackResult = beforeSendLog(log, hint: hint);
43+
final callbackResult = beforeSendLog(log, hint ?? Hint());
4444

4545
if (callbackResult is Future<SentryLog?>) {
4646
processedLog = await callbackResult;

packages/dart/lib/src/telemetry/log/logger.dart

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -63,40 +63,46 @@ abstract interface class SentryLoggerFormatter {
6363
String templateBody,
6464
List<dynamic> arguments, {
6565
Map<String, SentryAttribute>? attributes,
66+
Hint? hint,
6667
});
6768

6869
/// Logs a formatted message at DEBUG level.
6970
FutureOr<void> debug(
7071
String templateBody,
7172
List<dynamic> arguments, {
7273
Map<String, SentryAttribute>? attributes,
74+
Hint? hint,
7375
});
7476

7577
/// Logs a formatted message at INFO level.
7678
FutureOr<void> info(
7779
String templateBody,
7880
List<dynamic> arguments, {
7981
Map<String, SentryAttribute>? attributes,
82+
Hint? hint,
8083
});
8184

8285
/// Logs a formatted message at WARN level.
8386
FutureOr<void> warn(
8487
String templateBody,
8588
List<dynamic> arguments, {
8689
Map<String, SentryAttribute>? attributes,
90+
Hint? hint,
8791
});
8892

8993
/// Logs a formatted message at ERROR level.
9094
FutureOr<void> error(
9195
String templateBody,
9296
List<dynamic> arguments, {
9397
Map<String, SentryAttribute>? attributes,
98+
Hint? hint,
9499
});
95100

96101
/// Logs a formatted message at FATAL level.
97102
FutureOr<void> fatal(
98103
String templateBody,
99104
List<dynamic> arguments, {
100105
Map<String, SentryAttribute>? attributes,
106+
Hint? hint,
101107
});
102108
}

packages/dart/lib/src/telemetry/log/noop_logger.dart

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -63,40 +63,46 @@ final class _NoOpSentryLoggerFormatter implements SentryLoggerFormatter {
6363
String templateBody,
6464
List<dynamic> arguments, {
6565
Map<String, SentryAttribute>? attributes,
66+
Hint? hint,
6667
}) {}
6768

6869
@override
6970
FutureOr<void> debug(
7071
String templateBody,
7172
List<dynamic> arguments, {
7273
Map<String, SentryAttribute>? attributes,
74+
Hint? hint,
7375
}) {}
7476

7577
@override
7678
FutureOr<void> info(
7779
String templateBody,
7880
List<dynamic> arguments, {
7981
Map<String, SentryAttribute>? attributes,
82+
Hint? hint,
8083
}) {}
8184

8285
@override
8386
FutureOr<void> warn(
8487
String templateBody,
8588
List<dynamic> arguments, {
8689
Map<String, SentryAttribute>? attributes,
90+
Hint? hint,
8791
}) {}
8892

8993
@override
9094
FutureOr<void> error(
9195
String templateBody,
9296
List<dynamic> arguments, {
9397
Map<String, SentryAttribute>? attributes,
98+
Hint? hint,
9499
}) {}
95100

96101
@override
97102
FutureOr<void> fatal(
98103
String templateBody,
99104
List<dynamic> arguments, {
100105
Map<String, SentryAttribute>? attributes,
106+
Hint? hint,
101107
}) {}
102108
}

packages/dart/test/telemetry/log/log_capture_pipeline_test.dart

Lines changed: 37 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -98,7 +98,7 @@ void main() {
9898
event.log.attributes.containsKey('scope-attr');
9999
});
100100

101-
fixture.options.beforeSendLog = (log, {Hint? hint}) {
101+
fixture.options.beforeSendLog = (log, hint) {
102102
operations.add('beforeSendLog');
103103
return log;
104104
};
@@ -145,7 +145,7 @@ void main() {
145145

146146
group('when beforeSendLog is configured', () {
147147
test('returning null drops the log', () async {
148-
fixture.options.beforeSendLog = (_, {Hint? hint}) => null;
148+
fixture.options.beforeSendLog = (_, hint) => null;
149149

150150
final log = givenLog();
151151

@@ -155,7 +155,7 @@ void main() {
155155
});
156156

157157
test('returning null records lost event in client report', () async {
158-
fixture.options.beforeSendLog = (_, {Hint? hint}) => null;
158+
fixture.options.beforeSendLog = (_, hint) => null;
159159

160160
final log = givenLog();
161161

@@ -169,7 +169,7 @@ void main() {
169169
});
170170

171171
test('can mutate the log', () async {
172-
fixture.options.beforeSendLog = (log, {Hint? hint}) {
172+
fixture.options.beforeSendLog = (log, hint) {
173173
log.body = 'modified-body';
174174
log.attributes['added-key'] = SentryAttribute.string('added');
175175
return log;
@@ -186,7 +186,7 @@ void main() {
186186
});
187187

188188
test('async callback is awaited', () async {
189-
fixture.options.beforeSendLog = (log, {Hint? hint}) async {
189+
fixture.options.beforeSendLog = (log, hint) async {
190190
await Future.delayed(Duration(milliseconds: 10));
191191
log.body = 'async-modified';
192192
return log;
@@ -201,10 +201,41 @@ void main() {
201201
expect(captured.body, 'async-modified');
202202
});
203203

204+
test('forwards hint to beforeSendLog callback', () async {
205+
Hint? receivedHint;
206+
fixture.options.beforeSendLog = (log, hint) {
207+
receivedHint = hint;
208+
return log;
209+
};
210+
211+
final log = givenLog();
212+
final hint = Hint.withMap({'custom-key': 'custom-value'});
213+
214+
await fixture.pipeline
215+
.captureLog(log, scope: fixture.scope, hint: hint);
216+
217+
expect(receivedHint, isNotNull);
218+
expect(receivedHint!.get('custom-key'), 'custom-value');
219+
});
220+
221+
test('provides empty Hint when no hint is passed', () async {
222+
Hint? receivedHint;
223+
fixture.options.beforeSendLog = (log, hint) {
224+
receivedHint = hint;
225+
return log;
226+
};
227+
228+
final log = givenLog();
229+
230+
await fixture.pipeline.captureLog(log, scope: fixture.scope);
231+
232+
expect(receivedHint, isNotNull);
233+
});
234+
204235
test('exception in callback is caught and log is still captured',
205236
() async {
206237
fixture.options.automatedTestMode = false;
207-
fixture.options.beforeSendLog = (log, {Hint? hint}) {
238+
fixture.options.beforeSendLog = (log, hint) {
208239
throw Exception('test');
209240
};
210241

packages/dart/test/telemetry/log/logger_setup_integration_test.dart

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -135,40 +135,46 @@ class _CustomSentryLoggerFormatter implements SentryLoggerFormatter {
135135
String templateBody,
136136
List<dynamic> arguments, {
137137
Map<String, SentryAttribute>? attributes,
138+
Hint? hint,
138139
}) {}
139140

140141
@override
141142
FutureOr<void> debug(
142143
String templateBody,
143144
List<dynamic> arguments, {
144145
Map<String, SentryAttribute>? attributes,
146+
Hint? hint,
145147
}) {}
146148

147149
@override
148150
FutureOr<void> info(
149151
String templateBody,
150152
List<dynamic> arguments, {
151153
Map<String, SentryAttribute>? attributes,
154+
Hint? hint,
152155
}) {}
153156

154157
@override
155158
FutureOr<void> warn(
156159
String templateBody,
157160
List<dynamic> arguments, {
158161
Map<String, SentryAttribute>? attributes,
162+
Hint? hint,
159163
}) {}
160164

161165
@override
162166
FutureOr<void> error(
163167
String templateBody,
164168
List<dynamic> arguments, {
165169
Map<String, SentryAttribute>? attributes,
170+
Hint? hint,
166171
}) {}
167172

168173
@override
169174
FutureOr<void> fatal(
170175
String templateBody,
171176
List<dynamic> arguments, {
172177
Map<String, SentryAttribute>? attributes,
178+
Hint? hint,
173179
}) {}
174180
}

0 commit comments

Comments
 (0)