Skip to content

Commit

Permalink
Add support for API diagnostics (addresses #616)
Browse files Browse the repository at this point in the history
  • Loading branch information
Guy Fankam committed Aug 14, 2024
1 parent c014cca commit 20775c5
Show file tree
Hide file tree
Showing 14 changed files with 1,258 additions and 34 deletions.
9 changes: 6 additions & 3 deletions tools/code/common.tests/Api.cs
Original file line number Diff line number Diff line change
Expand Up @@ -16,8 +16,8 @@ public sealed record Soap : ApiType;
public static Gen<ApiType> Generate() =>
Gen.OneOfConst<ApiType>(new GraphQl(),
new Http());
//new WebSocket(),
//new Soap());
//new WebSocket(),
//new Soap());
}

public record ApiRevision
Expand Down Expand Up @@ -106,19 +106,22 @@ public record ApiModel
public required ApiType Type { get; init; }
public required string Path { get; init; }
public required FrozenSet<ApiRevision> Revisions { get; init; }
public required FrozenSet<ApiDiagnosticModel> Diagnostics { get; init; }
public Option<ApiVersion> Version { get; init; } = Option<ApiVersion>.None;

public static Gen<ApiModel> Generate() =>
from type in ApiType.Generate()
from name in GenerateName(type)
from path in GeneratePath()
from revisions in ApiRevision.GenerateSet(type, name)
from diagnostics in ApiDiagnosticModel.GenerateSet()
select new ApiModel
{
Name = name,
Type = type,
Path = path,
Revisions = revisions
Revisions = revisions,
Diagnostics = diagnostics
};

public static Gen<ApiName> GenerateName(ApiType type) =>
Expand Down
86 changes: 86 additions & 0 deletions tools/code/common.tests/ApiDiagnostic.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,86 @@
using CsCheck;
using LanguageExt;
using Microsoft.Extensions.Logging;
using Microsoft.OpenApi.Readers.Interface;
using Nito.Comparers;
using System.Collections;
using System.Collections.Frozen;
using System.Collections.Generic;
using System.Linq;

namespace common.tests;

public sealed record ApiDiagnosticSampling
{
public required string Type { get; init; }
public required float Percentage { get; init; }

public static Gen<ApiDiagnosticSampling> Generate() =>
from type in Gen.Const("fixed")
from percentage in Gen.Int[0, 100]
select new ApiDiagnosticSampling
{
Type = type,
Percentage = percentage
};
}

public record ApiDiagnosticModel
{
public required ApiDiagnosticName Name { get; init; }
public required LoggerName LoggerName { get; init; }
public Option<string> AlwaysLog { get; init; }
public Option<ApiDiagnosticSampling> Sampling { get; init; }

public static Gen<ApiDiagnosticModel> Generate() =>
from loggerType in LoggerType.Generate()
from loggerName in LoggerModel.GenerateName(loggerType)
from name in GenerateName(loggerType)
from alwaysLog in Gen.Const("allErrors").OptionOf()
from sampling in ApiDiagnosticSampling.Generate().OptionOf()
select new ApiDiagnosticModel
{
Name = name,
LoggerName = loggerName,
AlwaysLog = alwaysLog,
Sampling = sampling
};

public static Gen<ApiDiagnosticName> GenerateName() =>
from name in Generator.AlphaNumericStringBetween(10, 20)
select ApiDiagnosticName.From(name);

private static Gen<ApiDiagnosticName> GenerateName(LoggerType loggerType) =>
loggerType switch
{
LoggerType.AzureMonitor => Gen.Const(ApiDiagnosticName.From("azuremonitor")),
_ => GenerateName()
};

public static Gen<FrozenSet<ApiDiagnosticModel>> GenerateSet() =>
Generate().FrozenSetOf(0, 20, Comparer);

public static Gen<FrozenSet<ApiDiagnosticModel>> GenerateSet(FrozenSet<ApiDiagnosticModel> originalSet, ICollection<LoggerModel> loggers)
{
if (loggers.Count == 0)
{
Gen.Const(Enumerable.Empty<ApiDiagnosticModel>().ToFrozenSet(Comparer));
}

var loggersArray = loggers.ToArray();

return from updates in originalSet.Select(diagnostic => from logger in Gen.OneOfConst(loggersArray)
select diagnostic with
{
// Diagnostic name must be "azuremonitor" if the logger type is AzureMonitor
Name = logger.Type is LoggerType.AzureMonitor ? ApiDiagnosticName.From("azuremonitor") : diagnostic.Name,
LoggerName = logger.Name
})
.SequenceToFrozenSet(originalSet.Comparer)
select updates;
}

private static IEqualityComparer<ApiDiagnosticModel> Comparer { get; } =
EqualityComparerBuilder.For<ApiDiagnosticModel>()
.EquateBy(x => x.Name);
}
6 changes: 4 additions & 2 deletions tools/code/common.tests/Service.cs
Original file line number Diff line number Diff line change
Expand Up @@ -36,7 +36,7 @@ select updatedDiagnostics
from policyFragments in PolicyFragmentModel.GenerateSet()
from servicePolicies in ServicePolicyModel.GenerateSet()
from apis in from originalApis in ApiModel.GenerateSet()
from updatedApis in UpdateApis(originalApis, versionSets, tags)
from updatedApis in UpdateApis(originalApis, versionSets, tags, loggers)
select updatedApis
from groups in GroupModel.GenerateSet()
from products in from originalProducts in ProductModel.GenerateSet()
Expand Down Expand Up @@ -67,9 +67,11 @@ select updatedSubscriptions

public static Gen<FrozenSet<ApiModel>> UpdateApis(FrozenSet<ApiModel> apis,
ICollection<VersionSetModel> versionSets,
ICollection<TagModel> tags) =>
ICollection<TagModel> tags,
ICollection<LoggerModel> loggers) =>
apis.Select(api => from version in UpdateApiVersion(versionSets)
from revisions in UpdateApiRevisions(api.Revisions, tags)
from diagnostics in ApiDiagnosticModel.GenerateSet(api.Diagnostics, loggers)
select api with
{
Version = version,
Expand Down
Loading

0 comments on commit 20775c5

Please sign in to comment.