Skip to content

Commit b91f739

Browse files
[OTLP] Address feedback
- Use unused arbitrary. - Add tests for arbitrary enum values.
1 parent b1c422f commit b91f739

File tree

3 files changed

+69
-49
lines changed

3 files changed

+69
-49
lines changed

test/OpenTelemetry.Exporter.OpenTelemetryProtocol.FuzzTests/Generators.cs

Lines changed: 5 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -57,7 +57,7 @@ from logAttributesLimit in Gen.Choose(0, 1000).Select(x => (int?)x)
5757
return gen.ToArbitrary();
5858
}
5959

60-
public static Arbitrary<Activity> ActivityArbitrary()
60+
public static Arbitrary<Activity> ActivityArbitrary(ActivityStatusCode? status = default)
6161
{
6262
var gen = Gen.Sized(size =>
6363
{
@@ -102,7 +102,7 @@ public static Arbitrary<Activity> ActivityArbitrary()
102102
activity.AddLink(new ActivityLink(context, linkTags));
103103
}
104104

105-
activity.SetStatus(ActivityStatusCodes.Sample(size, 1).First());
105+
activity.SetStatus(status ?? ActivityStatusCodes.Sample(size, 1).First());
106106

107107
return Gen.Constant(activity);
108108
});
@@ -191,21 +191,21 @@ public static Arbitrary<Batch<Metric>> BatchMetricArbitrary()
191191
/// <summary>
192192
/// Generates valid LogRecord instances.
193193
/// </summary>
194-
public static Arbitrary<LogRecord> LogRecordArbitrary()
194+
public static Arbitrary<LogRecord> LogRecordArbitrary(LogRecordSeverity? severity = default)
195195
{
196196
var gen = Gen.Sized(size =>
197197
{
198198
var logRecord = LogRecordSharedPool.Current.Rent();
199199

200-
logRecord.Severity = LogRecordSeverities.Sample(size, 1).First();
200+
logRecord.Severity = severity ?? LogRecordSeverities.Sample(size, 1).First();
201201
logRecord.Timestamp = DateTime.UtcNow;
202202

203203
// Add attributes
204204
var count = Math.Min(size, 50);
205205
var attributes = new List<KeyValuePair<string, object?>>(count);
206206
for (int i = 0; i < count; i++)
207207
{
208-
attributes.Add(new KeyValuePair<string, object?>($"log.attr.{i}", $"value_{i}"));
208+
attributes.Add(new KeyValuePair<string, object?>($"log.attribute.{i}", $"value_{i}"));
209209
}
210210

211211
if (attributes.Count > 0)

test/OpenTelemetry.Exporter.OpenTelemetryProtocol.FuzzTests/ProtobufOtlpLogSerializerTests.cs

Lines changed: 29 additions & 44 deletions
Original file line numberDiff line numberDiff line change
@@ -42,10 +42,6 @@ public Property SerializedDataNeverExceedsBufferSize() => Prop.ForAll(
4242
{
4343
return true;
4444
}
45-
finally
46-
{
47-
CleanupLogRecords(logRecords);
48-
}
4945
});
5046

5147
[Property(MaxTest = 100)]
@@ -77,10 +73,6 @@ public Property WriteLogsDataReturnsNonNegativePosition() => Prop.ForAll(
7773
{
7874
return true;
7975
}
80-
finally
81-
{
82-
CleanupLogRecords(logRecords);
83-
}
8476
});
8577

8678
[Property(MaxTest = 50)]
@@ -140,10 +132,6 @@ public Property BufferAutoResizesWhenNeeded() => Prop.ForAll(
140132
{
141133
return true;
142134
}
143-
finally
144-
{
145-
CleanupLogRecords(logRecords);
146-
}
147135
});
148136

149137
[Property(MaxTest = 50)]
@@ -184,10 +172,6 @@ public Property SerializedOutputCanBeDeserialized() => Prop.ForAll(
184172
{
185173
return true;
186174
}
187-
finally
188-
{
189-
CleanupLogRecords(logRecords);
190-
}
191175
});
192176

193177
[Property(MaxTest = 50)]
@@ -196,14 +180,12 @@ public Property WriteLogsDataHandlesVariousSeverityLevels() => Prop.ForAll(
196180
Generators.SdkLimitOptionsArbitrary(),
197181
(severity, sdkLimits) =>
198182
{
199-
var logRecord = LogRecordSharedPool.Current.Rent();
200-
logRecord.Severity = severity;
201-
logRecord.Timestamp = DateTime.UtcNow;
183+
var logRecord = Generators.LogRecordArbitrary(severity).Generator.Sample(1).ToArray();
202184

203185
try
204186
{
205187
var buffer = new byte[10 * 1024 * 1024];
206-
var batch = new Batch<LogRecord>([logRecord], 1);
188+
var batch = new Batch<LogRecord>(logRecord, 1);
207189
var experimentalOptions = new ExperimentalOptions();
208190

209191
var writePos = ProtobufOtlpLogSerializer.WriteLogsData(
@@ -220,37 +202,40 @@ public Property WriteLogsDataHandlesVariousSeverityLevels() => Prop.ForAll(
220202
{
221203
return true;
222204
}
223-
finally
224-
{
225-
LogRecordSharedPool.Current.Return(logRecord);
226-
}
227205
});
228206

229-
private static LogRecord[] CreateLogRecords(LogRecordSeverity severity)
230-
{
231-
var logRecords = new List<LogRecord>();
232-
233-
for (int i = 0; i < 10; i++)
207+
[Property(MaxTest = 50)]
208+
public Property WriteLogsDataHandlesInvalidSeverities() => Prop.ForAll(
209+
Gen.Choose(int.MinValue, int.MaxValue).ToArbitrary(),
210+
Generators.SdkLimitOptionsArbitrary(),
211+
(severity, sdkLimits) =>
234212
{
235-
var logRecord = LogRecordSharedPool.Current.Rent();
213+
var logRecord = Generators.LogRecordArbitrary((LogRecordSeverity)severity).Generator.Sample(1).ToArray();
236214

237-
logRecord.Attributes = [new($"log.attribute.{i}", $"value_{i}")];
238-
logRecord.Severity = severity;
239-
logRecord.Timestamp = DateTime.UtcNow;
215+
try
216+
{
217+
var buffer = new byte[10 * 1024 * 1024];
218+
var batch = new Batch<LogRecord>(logRecord, 1);
219+
var experimentalOptions = new ExperimentalOptions();
240220

241-
logRecords.Add(logRecord);
242-
}
221+
var writePos = ProtobufOtlpLogSerializer.WriteLogsData(
222+
ref buffer,
223+
0,
224+
sdkLimits,
225+
experimentalOptions,
226+
null,
227+
batch);
243228

244-
return [.. logRecords];
245-
}
229+
return writePos >= 0;
230+
}
231+
catch (Exception ex) when (IsAllowedException(ex))
232+
{
233+
return true;
234+
}
235+
});
246236

247-
private static void CleanupLogRecords(LogRecord[] logRecords)
248-
{
249-
foreach (var logRecord in logRecords)
250-
{
251-
LogRecordSharedPool.Current.Return(logRecord);
252-
}
253-
}
237+
private static LogRecord[] CreateLogRecords(LogRecordSeverity severity)
238+
=> Generators.LogRecordArbitrary(severity).Generator.ArrayOf().Sample(1, 10).First();
254239

255240
private static bool IsAllowedException(Exception ex)
256241
=> ex is IndexOutOfRangeException or ArgumentException;

test/OpenTelemetry.Exporter.OpenTelemetryProtocol.FuzzTests/ProtobufOtlpTraceSerializerTests.cs

Lines changed: 35 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -251,6 +251,41 @@ public Property MultipleSerializationCallsAreConsistent() => Prop.ForAll(
251251
}
252252
});
253253

254+
[Property(MaxTest = 200)]
255+
public Property HandlesArbitraryActivityStatusCodes() => Prop.ForAll(
256+
Gen.Choose(int.MinValue, int.MaxValue).ToArbitrary(),
257+
Generators.SdkLimitOptionsArbitrary(),
258+
(statusCode, sdkLimits) =>
259+
{
260+
var activities = Generators.ActivityArbitrary((ActivityStatusCode)statusCode).Generator.Sample(1).ToArray();
261+
262+
try
263+
{
264+
var buffer = new byte[10 * 1024 * 1024]; // 10MB buffer
265+
var batch = new Batch<Activity>(activities, activities.Length);
266+
267+
var writePos = ProtobufOtlpTraceSerializer.WriteTraceData(
268+
ref buffer,
269+
0,
270+
sdkLimits,
271+
null,
272+
batch);
273+
274+
return writePos >= 0 && writePos <= buffer.Length;
275+
}
276+
catch (Exception ex) when (IsAllowedException(ex))
277+
{
278+
return true;
279+
}
280+
finally
281+
{
282+
foreach (var activity in activities)
283+
{
284+
activity?.Dispose();
285+
}
286+
}
287+
});
288+
254289
private static bool IsAllowedException(Exception ex)
255290
=> ex is IndexOutOfRangeException or ArgumentException;
256291
}

0 commit comments

Comments
 (0)