-
Notifications
You must be signed in to change notification settings - Fork 863
[OTLP] Add property-based fuzz testing for ProtobufOtlp serializers #6774
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
Merged
Merged
Changes from all commits
Commits
Show all changes
5 commits
Select commit
Hold shift + click to select a range
3e601ff
[OTLP] Add fuzz testing for OTLP serializer
martincostello 94e4659
[OTLP] Remove redundant items
martincostello c7a6b59
[OLTP] Add missing IVT
martincostello b1c422f
[OTLP] Address review comments
martincostello b91f739
[OTLP] Address feedback
martincostello File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
236 changes: 236 additions & 0 deletions
236
test/OpenTelemetry.Exporter.OpenTelemetryProtocol.FuzzTests/Generators.cs
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,236 @@ | ||
| // Copyright The OpenTelemetry Authors | ||
| // SPDX-License-Identifier: Apache-2.0 | ||
|
|
||
| using System.Diagnostics; | ||
| using System.Diagnostics.Metrics; | ||
| using FsCheck; | ||
| using FsCheck.Fluent; | ||
| using OpenTelemetry.Exporter.OpenTelemetryProtocol.Implementation; | ||
| using OpenTelemetry.Logs; | ||
| using OpenTelemetry.Metrics; | ||
| using OpenTelemetry.Resources; | ||
|
|
||
| namespace OpenTelemetry.Exporter.OpenTelemetryProtocol.FuzzTests; | ||
|
|
||
| internal static class Generators | ||
| { | ||
| public static readonly ActivitySource TestActivitySource = new("Fuzz.ActivitySource", "1.0.0"); | ||
|
|
||
| private static readonly Gen<ActivityStatusCode> ActivityStatusCodes = Gen.Elements( | ||
| [ | ||
| ActivityStatusCode.Unset, | ||
| ActivityStatusCode.Ok, | ||
| ActivityStatusCode.Error, | ||
| ]); | ||
martincostello marked this conversation as resolved.
Show resolved
Hide resolved
|
||
|
|
||
| private static readonly Gen<LogRecordSeverity> LogRecordSeverities = Gen.Elements( | ||
| [ | ||
| LogRecordSeverity.Debug, | ||
| LogRecordSeverity.Error, | ||
| LogRecordSeverity.Fatal, | ||
| LogRecordSeverity.Info, | ||
| LogRecordSeverity.Trace, | ||
| LogRecordSeverity.Unspecified, | ||
| LogRecordSeverity.Warn, | ||
| ]); | ||
martincostello marked this conversation as resolved.
Show resolved
Hide resolved
|
||
|
|
||
| public static Arbitrary<SdkLimitOptions> SdkLimitOptionsArbitrary() | ||
| { | ||
| var gen = from spanAttributesLimit in Gen.Choose(0, 1000).Select(x => (int?)x) | ||
| from spanEventsLimit in Gen.Choose(0, 1000).Select(x => (int?)x) | ||
| from spanLinksLimit in Gen.Choose(0, 1000).Select(x => (int?)x) | ||
| from spanEventAttributesLimit in Gen.Choose(0, 1000).Select(x => (int?)x) | ||
| from spanLinkAttributesLimit in Gen.Choose(0, 1000).Select(x => (int?)x) | ||
| from attributeValueLimit in Gen.Choose(0, 10000).Select(x => (int?)x) | ||
| from logAttributesLimit in Gen.Choose(0, 1000).Select(x => (int?)x) | ||
| select new SdkLimitOptions | ||
| { | ||
| AttributeValueLengthLimit = attributeValueLimit, | ||
| LogRecordAttributeCountLimit = logAttributesLimit, | ||
| SpanAttributeCountLimit = spanAttributesLimit, | ||
| SpanEventAttributeCountLimit = spanEventAttributesLimit, | ||
| SpanEventCountLimit = spanEventsLimit, | ||
| SpanLinkAttributeCountLimit = spanLinkAttributesLimit, | ||
| SpanLinkCountLimit = spanLinksLimit, | ||
| }; | ||
|
|
||
| return gen.ToArbitrary(); | ||
| } | ||
|
|
||
| public static Arbitrary<Activity> ActivityArbitrary(ActivityStatusCode? status = default) | ||
| { | ||
| var gen = Gen.Sized(size => | ||
| { | ||
| var activity = TestActivitySource.StartActivity($"TestActivity_{Guid.NewGuid():N}"); | ||
| if (activity == null) | ||
| { | ||
| return Gen.Constant(new Activity("Fallback")); | ||
martincostello marked this conversation as resolved.
Show resolved
Hide resolved
|
||
| } | ||
|
|
||
| // Generate tags | ||
| var tagCount = Math.Min(size, 50); | ||
| for (int i = 0; i < tagCount; i++) | ||
| { | ||
| activity.SetTag($"tag.{i}", $"value_{i}_{Guid.NewGuid():N}"); | ||
| } | ||
|
|
||
| // Generate events | ||
| var eventCount = Math.Min(size / 10, 10); | ||
| for (int i = 0; i < eventCount; i++) | ||
| { | ||
| var eventTags = new ActivityTagsCollection | ||
| { | ||
| { $"event.tag.{i}", $"event_value_{i}" }, | ||
| }; | ||
| activity.AddEvent(new ActivityEvent($"Event_{i}", DateTimeOffset.UtcNow, eventTags)); | ||
| } | ||
|
|
||
| // Generate links | ||
| var linkCount = Math.Min(size / 10, 10); | ||
| for (int i = 0; i < linkCount; i++) | ||
| { | ||
| var linkTags = new ActivityTagsCollection | ||
| { | ||
| { $"link.tag.{i}", $"link_value_{i}" }, | ||
| }; | ||
|
|
||
| var context = new ActivityContext( | ||
| ActivityTraceId.CreateRandom(), | ||
| ActivitySpanId.CreateRandom(), | ||
| ActivityTraceFlags.Recorded); | ||
|
|
||
| activity.AddLink(new ActivityLink(context, linkTags)); | ||
| } | ||
|
|
||
| activity.SetStatus(status ?? ActivityStatusCodes.Sample(size, 1).First()); | ||
|
|
||
| return Gen.Constant(activity); | ||
| }); | ||
|
|
||
| return gen.ToArbitrary(); | ||
| } | ||
|
|
||
| public static Arbitrary<Resource> ResourceArbitrary() | ||
| { | ||
| var gen = Gen.Sized(size => | ||
| { | ||
| var count = Math.Min(size, 20); | ||
| var attributes = new Dictionary<string, object>(count); | ||
|
|
||
| for (int i = 0; i < count; i++) | ||
| { | ||
| attributes[$"resource.attr.{i}"] = $"value_{i}"; | ||
| } | ||
|
|
||
| return Gen.Constant(Resource.Empty.Merge(new Resource(attributes))); | ||
| }); | ||
|
|
||
| return gen.ToArbitrary(); | ||
| } | ||
|
|
||
| /// <summary> | ||
| /// Generates valid Metric instances for different metric types. | ||
| /// </summary> | ||
| public static Arbitrary<Batch<Metric>> BatchMetricArbitrary() | ||
| { | ||
| var gen = Gen.Sized(size => | ||
| { | ||
| var metrics = new List<Metric>(); | ||
|
|
||
| var builder = Sdk.CreateMeterProviderBuilder() | ||
| .AddMeter("FuzzTest.Meter") | ||
| .AddInMemoryExporter(metrics); | ||
|
|
||
| if (size % 7 == 0) | ||
| { | ||
| var boundaries = new double[Math.Min(size % 10, 10)]; | ||
| for (int i = 0; i < boundaries.Length; i++) | ||
| { | ||
| boundaries[i] = (i * 50.0) + (size % 50); | ||
| } | ||
|
|
||
| builder.AddView("test.histogram", new ExplicitBucketHistogramConfiguration { Boundaries = boundaries }); | ||
| } | ||
|
|
||
| using (var meterProvider = builder.Build()) | ||
| { | ||
| using var meter = new Meter("FuzzTest.Meter", "1.0.0"); | ||
|
|
||
| var counter = meter.CreateCounter<long>("test.counter"); | ||
|
|
||
| for (int i = 0; i < size; i++) | ||
| { | ||
| counter.Add(100 * i); | ||
| } | ||
|
|
||
| var gauge = meter.CreateGauge<double>("test.gauge"); | ||
|
|
||
| for (int i = 0; i < size; i++) | ||
| { | ||
| gauge.Record(0.1 * i); | ||
| } | ||
|
|
||
| var histogram = meter.CreateHistogram<int>("test.histogram"); | ||
|
|
||
| for (int i = 0; i < size; i++) | ||
| { | ||
| histogram.Record(300 * i); | ||
| } | ||
|
|
||
| meterProvider.ForceFlush(); | ||
| } | ||
|
|
||
| var batch = new Batch<Metric>([.. metrics], metrics.Count); | ||
|
|
||
| return Gen.Constant(batch); | ||
| }); | ||
|
|
||
| return gen.ToArbitrary(); | ||
| } | ||
|
|
||
| /// <summary> | ||
| /// Generates valid LogRecord instances. | ||
| /// </summary> | ||
| public static Arbitrary<LogRecord> LogRecordArbitrary(LogRecordSeverity? severity = default) | ||
| { | ||
| var gen = Gen.Sized(size => | ||
| { | ||
| var logRecord = LogRecordSharedPool.Current.Rent(); | ||
|
|
||
| logRecord.Severity = severity ?? LogRecordSeverities.Sample(size, 1).First(); | ||
| logRecord.Timestamp = DateTime.UtcNow; | ||
|
|
||
| // Add attributes | ||
| var count = Math.Min(size, 50); | ||
| var attributes = new List<KeyValuePair<string, object?>>(count); | ||
| for (int i = 0; i < count; i++) | ||
| { | ||
| attributes.Add(new KeyValuePair<string, object?>($"log.attribute.{i}", $"value_{i}")); | ||
| } | ||
|
|
||
| if (attributes.Count > 0) | ||
| { | ||
| logRecord.Attributes = attributes; | ||
| } | ||
|
|
||
| return Gen.Constant(logRecord); | ||
| }); | ||
|
|
||
| return gen.ToArbitrary(); | ||
| } | ||
|
|
||
| public static Arbitrary<Activity[]> ActivityBatchArbitrary() | ||
| { | ||
| var gen = Gen.Sized(size => | ||
| { | ||
| var batchSize = Math.Min(size, 100); | ||
| return Gen.ArrayOf(ActivityArbitrary().Generator, batchSize); | ||
| }); | ||
|
|
||
| return gen.ToArbitrary(); | ||
| } | ||
|
|
||
| public static Arbitrary<int> BufferSizeArbitrary() => Gen.Choose(64, 10 * 1024 * 1024).ToArbitrary(); | ||
|
|
||
| public static Arbitrary<LogRecordSeverity> LogRecordSeverityArbitrary() => LogRecordSeverities.ToArbitrary(); | ||
| } | ||
27 changes: 27 additions & 0 deletions
27
...TelemetryProtocol.FuzzTests/OpenTelemetry.Exporter.OpenTelemetryProtocol.FuzzTests.csproj
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,27 @@ | ||
| <Project Sdk="Microsoft.NET.Sdk"> | ||
|
|
||
| <PropertyGroup> | ||
| <TargetFrameworks>$(TargetFrameworksForTests)</TargetFrameworks> | ||
| </PropertyGroup> | ||
|
|
||
| <ItemGroup> | ||
| <PackageReference Include="FsCheck.Xunit" /> | ||
| <PackageReference Include="Grpc" /> | ||
| <PackageReference Include="Grpc.AspNetCore.Server" Condition="'$(TargetFrameworkIdentifier)' == '.NETCoreApp'" /> | ||
| <PackageReference Include="Grpc.Net.Client" /> | ||
| <PackageReference Include="Google.Protobuf" /> | ||
| <PackageReference Include="Grpc.Tools" /> | ||
| </ItemGroup> | ||
|
|
||
| <ItemGroup> | ||
| <ProjectReference Include="$(RepoRoot)\src\OpenTelemetry.Exporter.InMemory\OpenTelemetry.Exporter.InMemory.csproj" /> | ||
| <ProjectReference Include="$(RepoRoot)\src\OpenTelemetry.Exporter.OpenTelemetryProtocol\OpenTelemetry.Exporter.OpenTelemetryProtocol.csproj" /> | ||
| </ItemGroup> | ||
|
|
||
| <ItemGroup> | ||
| <Protobuf Include="$(RepoRoot)\src\Shared\Proto\**\*.proto" Link="Proto\%(RecursiveDir)%(Filename)%(Extension)" Access="internal"> | ||
| <ProtoRoot>$(RepoRoot)\src\Shared\Proto</ProtoRoot> | ||
| </Protobuf> | ||
| </ItemGroup> | ||
|
|
||
| </Project> |
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
Uh oh!
There was an error while loading. Please reload this page.