Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
38 changes: 37 additions & 1 deletion tools/code/common.tests/ApiDiagnostic.cs
Original file line number Diff line number Diff line change
Expand Up @@ -25,25 +25,61 @@ from type in Gen.Const("fixed")
};
}

public sealed record ApiDiagnosticLargeLanguageModelMessages
{
public required string Messages { get; init; }
public required int MaxSizeInBytes { get; init; }

public static Gen<ApiDiagnosticLargeLanguageModelMessages> Generate() =>
from messages in Gen.OneOfConst("all", "errors")
from maxSize in Gen.Int[0, 65536]
select new ApiDiagnosticLargeLanguageModelMessages
{
Messages = messages,
MaxSizeInBytes = maxSize
};
}

public sealed record ApiDiagnosticLargeLanguageModel
{
public Option<string> Logs { get; init; }
public Option<ApiDiagnosticLargeLanguageModelMessages> Requests { get; init; }
public Option<ApiDiagnosticLargeLanguageModelMessages> Responses { get; init; }

public static Gen<ApiDiagnosticLargeLanguageModel> Generate() =>
from logs in Gen.OneOfConst("enabled", "disabled").OptionOf()
from requests in ApiDiagnosticLargeLanguageModelMessages.Generate().OptionOf()
from responses in ApiDiagnosticLargeLanguageModelMessages.Generate().OptionOf()
select new ApiDiagnosticLargeLanguageModel
{
Logs = logs,
Requests = requests,
Responses = responses
};
}

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 Option<ApiDiagnosticLargeLanguageModel> LargeLanguageModel { 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()
from largeLanguageModel in ApiDiagnosticLargeLanguageModel.Generate().OptionOf()
select new ApiDiagnosticModel
{
Name = name,
LoggerName = loggerName,
AlwaysLog = alwaysLog,
Sampling = sampling
Sampling = sampling,
LargeLanguageModel = largeLanguageModel
};

public static Gen<ApiDiagnosticName> GenerateName() =>
Expand Down
38 changes: 37 additions & 1 deletion tools/code/common.tests/Diagnostic.cs
Original file line number Diff line number Diff line change
Expand Up @@ -20,25 +20,61 @@ from type in Gen.Const("fixed")
};
}

public sealed record DiagnosticLargeLanguageModelMessages
{
public required string Messages { get; init; }
public required int MaxSizeInBytes { get; init; }

public static Gen<DiagnosticLargeLanguageModelMessages> Generate() =>
from messages in Gen.OneOfConst("all", "errors")
from maxSize in Gen.Int[0, 65536]
select new DiagnosticLargeLanguageModelMessages
{
Messages = messages,
MaxSizeInBytes = maxSize
};
}

public sealed record DiagnosticLargeLanguageModel
{
public Option<string> Logs { get; init; }
public Option<DiagnosticLargeLanguageModelMessages> Requests { get; init; }
public Option<DiagnosticLargeLanguageModelMessages> Responses { get; init; }

public static Gen<DiagnosticLargeLanguageModel> Generate() =>
from logs in Gen.OneOfConst("enabled", "disabled").OptionOf()
from requests in DiagnosticLargeLanguageModelMessages.Generate().OptionOf()
from responses in DiagnosticLargeLanguageModelMessages.Generate().OptionOf()
select new DiagnosticLargeLanguageModel
{
Logs = logs,
Requests = requests,
Responses = responses
};
}

public record DiagnosticModel
{
public required DiagnosticName Name { get; init; }
public required LoggerName LoggerName { get; init; }
public Option<string> AlwaysLog { get; init; }
public Option<DiagnosticSampling> Sampling { get; init; }
public Option<DiagnosticLargeLanguageModel> LargeLanguageModel { get; init; }

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

public static Gen<DiagnosticName> GenerateName() =>
Expand Down
30 changes: 30 additions & 0 deletions tools/code/common/ApiDiagnostic.cs
Original file line number Diff line number Diff line change
Expand Up @@ -147,6 +147,10 @@ public sealed record DiagnosticContract
[JsonIgnore(Condition = JsonIgnoreCondition.WhenWritingDefault)]
public string? HttpCorrelationProtocol { get; init; }

[JsonPropertyName("largeLanguageModel")]
[JsonIgnore(Condition = JsonIgnoreCondition.WhenWritingDefault)]
public LargeLanguageModelSettings? LargeLanguageModel { get; init; }

[JsonPropertyName("logClientIp")]
[JsonIgnore(Condition = JsonIgnoreCondition.WhenWritingDefault)]
public bool? LogClientIp { get; init; }
Expand Down Expand Up @@ -233,6 +237,32 @@ public sealed record SamplingSettings
[JsonIgnore(Condition = JsonIgnoreCondition.WhenWritingDefault)]
public string? SamplingType { get; init; }
}

public sealed record LargeLanguageModelSettings
{
[JsonPropertyName("logs")]
[JsonIgnore(Condition = JsonIgnoreCondition.WhenWritingDefault)]
public string? Logs { get; init; }

[JsonPropertyName("requests")]
[JsonIgnore(Condition = JsonIgnoreCondition.WhenWritingDefault)]
public LargeLanguageModelMessageSettings? Requests { get; init; }

[JsonPropertyName("responses")]
[JsonIgnore(Condition = JsonIgnoreCondition.WhenWritingDefault)]
public LargeLanguageModelMessageSettings? Responses { get; init; }
}

public sealed record LargeLanguageModelMessageSettings
{
[JsonPropertyName("messages")]
[JsonIgnore(Condition = JsonIgnoreCondition.WhenWritingDefault)]
public string? Messages { get; init; }

[JsonPropertyName("maxSizeInBytes")]
[JsonIgnore(Condition = JsonIgnoreCondition.WhenWritingDefault)]
public int? MaxSizeInBytes { get; init; }
}
}

public static class ApiDiagnosticModule
Expand Down
32 changes: 31 additions & 1 deletion tools/code/common/Diagnostic.cs
Original file line number Diff line number Diff line change
Expand Up @@ -144,6 +144,10 @@ public sealed record DiagnosticContract
[JsonIgnore(Condition = JsonIgnoreCondition.WhenWritingDefault)]
public string? HttpCorrelationProtocol { get; init; }

[JsonPropertyName("largeLanguageModel")]
[JsonIgnore(Condition = JsonIgnoreCondition.WhenWritingDefault)]
public LargeLanguageModelSettings? LargeLanguageModel { get; init; }

[JsonPropertyName("logClientIp")]
[JsonIgnore(Condition = JsonIgnoreCondition.WhenWritingDefault)]
public bool? LogClientIp { get; init; }
Expand Down Expand Up @@ -230,6 +234,32 @@ public sealed record SamplingSettings
[JsonIgnore(Condition = JsonIgnoreCondition.WhenWritingDefault)]
public string? SamplingType { get; init; }
}

public sealed record LargeLanguageModelSettings
{
[JsonPropertyName("logs")]
[JsonIgnore(Condition = JsonIgnoreCondition.WhenWritingDefault)]
public string? Logs { get; init; }

[JsonPropertyName("requests")]
[JsonIgnore(Condition = JsonIgnoreCondition.WhenWritingDefault)]
public LargeLanguageModelMessageSettings? Requests { get; init; }

[JsonPropertyName("responses")]
[JsonIgnore(Condition = JsonIgnoreCondition.WhenWritingDefault)]
public LargeLanguageModelMessageSettings? Responses { get; init; }
}

public sealed record LargeLanguageModelMessageSettings
{
[JsonPropertyName("messages")]
[JsonIgnore(Condition = JsonIgnoreCondition.WhenWritingDefault)]
public string? Messages { get; init; }

[JsonPropertyName("maxSizeInBytes")]
[JsonIgnore(Condition = JsonIgnoreCondition.WhenWritingDefault)]
public int? MaxSizeInBytes { get; init; }
}
}

public static class DiagnosticModule
Expand Down Expand Up @@ -306,4 +336,4 @@ public static Option<LoggerName> TryGetLoggerName(DiagnosticDto dto) =>
from loggerId in Prelude.Optional(dto.Properties.LoggerId)
from loggerNameString in loggerId.Split('/').LastOrNone()
select LoggerName.From(loggerNameString);
}
}
32 changes: 31 additions & 1 deletion tools/code/common/WorkspaceDiagnostic.cs
Original file line number Diff line number Diff line change
Expand Up @@ -139,6 +139,10 @@ public sealed record DiagnosticContract
[JsonIgnore(Condition = JsonIgnoreCondition.WhenWritingDefault)]
public string? HttpCorrelationProtocol { get; init; }

[JsonPropertyName("largeLanguageModel")]
[JsonIgnore(Condition = JsonIgnoreCondition.WhenWritingDefault)]
public LargeLanguageModelSettings? LargeLanguageModel { get; init; }

[JsonPropertyName("logClientIp")]
[JsonIgnore(Condition = JsonIgnoreCondition.WhenWritingDefault)]
public bool? LogClientIp { get; init; }
Expand Down Expand Up @@ -225,6 +229,32 @@ public sealed record SamplingSettings
[JsonIgnore(Condition = JsonIgnoreCondition.WhenWritingDefault)]
public string? SamplingType { get; init; }
}

public sealed record LargeLanguageModelSettings
{
[JsonPropertyName("logs")]
[JsonIgnore(Condition = JsonIgnoreCondition.WhenWritingDefault)]
public string? Logs { get; init; }

[JsonPropertyName("requests")]
[JsonIgnore(Condition = JsonIgnoreCondition.WhenWritingDefault)]
public LargeLanguageModelMessageSettings? Requests { get; init; }

[JsonPropertyName("responses")]
[JsonIgnore(Condition = JsonIgnoreCondition.WhenWritingDefault)]
public LargeLanguageModelMessageSettings? Responses { get; init; }
}

public sealed record LargeLanguageModelMessageSettings
{
[JsonPropertyName("messages")]
[JsonIgnore(Condition = JsonIgnoreCondition.WhenWritingDefault)]
public string? Messages { get; init; }

[JsonPropertyName("maxSizeInBytes")]
[JsonIgnore(Condition = JsonIgnoreCondition.WhenWritingDefault)]
public int? MaxSizeInBytes { get; init; }
}
}

public static class WorkspaceDiagnosticModule
Expand Down Expand Up @@ -269,4 +299,4 @@ public static async ValueTask<WorkspaceDiagnosticDto> ReadDto(this WorkspaceDiag
var content = await file.ToFileInfo().ReadAsBinaryData(cancellationToken);
return content.ToObjectFromJson<WorkspaceDiagnosticDto>();
}
}
}
82 changes: 50 additions & 32 deletions tools/code/integration.tests/ApiDiagnostic.cs
Original file line number Diff line number Diff line change
Expand Up @@ -60,25 +60,10 @@ async ValueTask put(ApiDiagnosticModel model, ApiName apiName, ManagementService
{
var serviceUri = getServiceUri(serviceName);
var diagnosticUri = ApiDiagnosticUri.From(model.Name, apiName, serviceUri);
var dto = getDto(model);
var dto = MapToDto(model);

await diagnosticUri.PutDto(dto, pipeline, cancellationToken);
}

static ApiDiagnosticDto getDto(ApiDiagnosticModel model) =>
new()
{
Properties = new ApiDiagnosticDto.DiagnosticContract
{
LoggerId = $"/loggers/{model.LoggerName}",
AlwaysLog = model.AlwaysLog.ValueUnsafe(),
Sampling = model.Sampling.Map(sampling => new ApiDiagnosticDto.SamplingSettings
{
SamplingType = sampling.Type,
Percentage = sampling.Percentage
}).ValueUnsafe()
}
};
}

public static void ConfigureValidateExtractedApiDiagnostics(IHostApplicationBuilder builder)
Expand Down Expand Up @@ -119,6 +104,7 @@ static string normalizeDto(ApiDiagnosticDto dto) =>
{
LoggerId = string.Join('/', dto.Properties.LoggerId?.Split('/')?.TakeLast(2)?.ToArray() ?? []),
AlwaysLog = dto.Properties.AlwaysLog ?? string.Empty,
LargeLanguageModel = NormalizeLargeLanguageModel(dto.Properties.LargeLanguageModel),
Sampling = new
{
Type = dto.Properties.Sampling?.SamplingType ?? string.Empty,
Expand Down Expand Up @@ -240,25 +226,10 @@ await models.IterParallel(async model =>
async ValueTask writeInformationFile(ApiDiagnosticModel model, ApiName apiName, ManagementServiceDirectory serviceDirectory, CancellationToken cancellationToken)
{
var informationFile = ApiDiagnosticInformationFile.From(model.Name, apiName, serviceDirectory);
var dto = getDto(model);
var dto = MapToDto(model);

await informationFile.WriteDto(dto, cancellationToken);
}

static ApiDiagnosticDto getDto(ApiDiagnosticModel model) =>
new()
{
Properties = new ApiDiagnosticDto.DiagnosticContract
{
LoggerId = $"/loggers/{model.LoggerName}",
AlwaysLog = model.AlwaysLog.ValueUnsafe(),
Sampling = model.Sampling.Map(sampling => new ApiDiagnosticDto.SamplingSettings
{
SamplingType = sampling.Type,
Percentage = sampling.Percentage
}).ValueUnsafe()
}
};
}
public static void ConfigureValidatePublishedApiDiagnostics(IHostApplicationBuilder builder)
{
Expand Down Expand Up @@ -298,13 +269,60 @@ static string normalizeDto(ApiDiagnosticDto dto) =>
{
LoggerId = string.Join('/', dto.Properties.LoggerId?.Split('/')?.TakeLast(2)?.ToArray() ?? []),
AlwaysLog = dto.Properties.AlwaysLog ?? string.Empty,
LargeLanguageModel = NormalizeLargeLanguageModel(dto.Properties.LargeLanguageModel),
Sampling = new
{
Type = dto.Properties.Sampling?.SamplingType ?? string.Empty,
Percentage = dto.Properties.Sampling?.Percentage ?? 0
}
}.ToString()!;
}

private static ApiDiagnosticDto MapToDto(ApiDiagnosticModel model) =>
new()
{
Properties = new ApiDiagnosticDto.DiagnosticContract
{
LoggerId = $"/loggers/{model.LoggerName}",
AlwaysLog = model.AlwaysLog.ValueUnsafe(),
Sampling = model.Sampling.Map(sampling => new ApiDiagnosticDto.SamplingSettings
{
SamplingType = sampling.Type,
Percentage = sampling.Percentage
}).ValueUnsafe(),
LargeLanguageModel = model.LargeLanguageModel.Match<ApiDiagnosticDto.LargeLanguageModelSettings?>(MapLargeLanguageModel, () => null)
}
};

private static ApiDiagnosticDto.LargeLanguageModelSettings MapLargeLanguageModel(ApiDiagnosticLargeLanguageModel largeLanguageModel) =>
new()
{
Logs = largeLanguageModel.Logs.Match(logs => logs, () => (string?)null),
Requests = largeLanguageModel.Requests.Match<ApiDiagnosticDto.LargeLanguageModelMessageSettings?>(MapLargeLanguageModelMessages, () => null),
Responses = largeLanguageModel.Responses.Match<ApiDiagnosticDto.LargeLanguageModelMessageSettings?>(MapLargeLanguageModelMessages, () => null)
};

private static ApiDiagnosticDto.LargeLanguageModelMessageSettings MapLargeLanguageModelMessages(ApiDiagnosticLargeLanguageModelMessages messages) =>
new()
{
Messages = messages.Messages,
MaxSizeInBytes = messages.MaxSizeInBytes
};

private static object NormalizeLargeLanguageModel(ApiDiagnosticDto.LargeLanguageModelSettings? largeLanguageModel) =>
new
{
Logs = largeLanguageModel?.Logs ?? string.Empty,
Requests = NormalizeLargeLanguageModelMessages(largeLanguageModel?.Requests),
Responses = NormalizeLargeLanguageModelMessages(largeLanguageModel?.Responses)
};

private static object NormalizeLargeLanguageModelMessages(ApiDiagnosticDto.LargeLanguageModelMessageSettings? largeLanguageModelMessages) =>
new
{
Messages = largeLanguageModelMessages?.Messages ?? string.Empty,
MaxSizeInBytes = largeLanguageModelMessages?.MaxSizeInBytes ?? 0
};
}
//internal static class ApiDiagnosticModule
//{
Expand Down
Loading