Skip to content

Addition & Deserialization of Azure Log Analytics Properties #2727

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

Open
wants to merge 16 commits into
base: main
Choose a base branch
from
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
41 changes: 41 additions & 0 deletions schemas/dab.draft.schema.json
Original file line number Diff line number Diff line change
Expand Up @@ -410,6 +410,47 @@
},
"required": ["endpoint"]
},
"azure-log-analytics": {
"type": "object",
"additionalProperties": false,
"properties": {
"enabled": {
"type": "boolean",
"description": "Allow enabling/disabling Azure Log Analytics.",
"default": false
},
"auth": {
"type": "object",
"additionalProperties": false,
"properties": {
"workspace-id": {
"type": "string",
"description": "Azure Log Analytics Workspace ID"
},
"dcr-immutable-id": {
"type": "string",
"description": "DCR ID for entra-id mode"
},
"dce-endpoint": {
"type": "string",
"description": "DCE endpoint for entra-id mode"
}
},
"required": ["workspace-id"]
},
"log-type": {
"type": "string",
"description": "Custom log table name in Log Analytics",
"default": "DabLogs"
},
"flush-interval-seconds": {
"type": "integer",
"description": "Interval between log batch pushes (in seconds)",
"default": 5
}
},
"required": ["auth"]
},
"log-level": {
"type": "object",
"description": "Global configuration of log level, defines logging severity levels for specific classes, when 'null' it will set logging level based on 'host: mode' property",
Expand Down
142 changes: 142 additions & 0 deletions src/Config/Converters/AzureLogAnalyticsAuthOptionsConverter.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,142 @@
// Copyright (c) Microsoft Corporation.
// Licensed under the MIT License.

using System.Text.Json;
using System.Text.Json.Serialization;
using Azure.DataApiBuilder.Config.ObjectModel;

namespace Azure.DataApiBuilder.Config.Converters;

internal class AzureLogAnalyticsAuthOptionsConverter : JsonConverter<AzureLogAnalyticsAuthOptions>
{
// Determines whether to replace environment variable with its
// value or not while deserializing.
private bool _replaceEnvVar;

/// <param name="replaceEnvVar">Whether to replace environment variable with its
/// value or not while deserializing.</param>
public AzureLogAnalyticsAuthOptionsConverter(bool replaceEnvVar)
{
_replaceEnvVar = replaceEnvVar;
}

/// <summary>
/// Defines how DAB reads Azure Log Analytics Auth options and defines which values are
/// used to instantiate AzureLogAnalyticsAuthOptions.
/// Uses default deserialize.
/// </summary>
/// <exception cref="JsonException">Thrown when improperly formatted Azure Log Analytics Auth options are provided.</exception>
public override AzureLogAnalyticsAuthOptions? Read(ref Utf8JsonReader reader, Type typeToConvert, JsonSerializerOptions options)
{
if (reader.TokenType is JsonTokenType.StartObject)
{
AzureLogAnalyticsAuthOptions? authOptions = new();

while (reader.Read())
{
if (reader.TokenType == JsonTokenType.EndObject)
{
break;
}

string? propertyName = reader.GetString();

reader.Read();
switch (propertyName)
{
case "workspace-id":
if (reader.TokenType is JsonTokenType.String)
{
string? workspaceId = reader.DeserializeString(_replaceEnvVar);
if (workspaceId is null)
{
throw new JsonException($"Unsuported null value entered for the property workspace-id");
}

authOptions = authOptions with { WorkspaceId = workspaceId };
}
else
{
throw new JsonException($"Unexpected type of value entered for workspace-id: {reader.TokenType}");
}

break;

case "dcr-immutable-id":
if (reader.TokenType is JsonTokenType.String)
{
string? dcrImmutableId = reader.DeserializeString(_replaceEnvVar);
if (dcrImmutableId is null)
{
throw new JsonException($"Unsuported null value entered for the property dcr-immutable-id");
}

authOptions = authOptions with { DcrImmutableId = dcrImmutableId };
}
else
{
throw new JsonException($"Unexpected type of value entered for dcr-immutable-id: {reader.TokenType}");
}

break;

case "dce-endpoint":
if (reader.TokenType is JsonTokenType.String)
{
string? dceEndpoint = reader.DeserializeString(_replaceEnvVar);
if (dceEndpoint is null)
{
throw new JsonException($"Unsuported null value entered for the property dce-endpoint");
}

authOptions = authOptions with { DceEndpoint = dceEndpoint };
}
else
{
throw new JsonException($"Unexpected type of value entered for dce-endpoint: {reader.TokenType}");
}

break;

default:
throw new JsonException($"Unexpected property {propertyName}");
}
}

return authOptions;
}

throw new JsonException("Failed to read the Azure Log Analytics Auth Options");
}

/// <summary>
/// When writing the AzureLogAnalyticsAuthOptions back to a JSON file, only write the properties
/// if they are user provided. This avoids polluting the written JSON file with properties
/// the user most likely omitted when writing the original DAB runtime config file.
/// This Write operation is only used when a RuntimeConfig object is serialized to JSON.
/// </summary>
public override void Write(Utf8JsonWriter writer, AzureLogAnalyticsAuthOptions value, JsonSerializerOptions options)
{
writer.WriteStartObject();

if (value?.UserProvidedWorkspaceId is true)
{
writer.WritePropertyName("workspace-id");
JsonSerializer.Serialize(writer, value.WorkspaceId, options);
}

if (value?.UserProvidedDcrImmutableId is true)
{
writer.WritePropertyName("drc-immutable-id");
JsonSerializer.Serialize(writer, value.DcrImmutableId, options);
}

if (value?.UserProvidedDceEndpoint is true)
{
writer.WritePropertyName("dce-endpoint");
JsonSerializer.Serialize(writer, value.DceEndpoint, options);
}

writer.WriteEndObject();
}
}
187 changes: 187 additions & 0 deletions src/Config/Converters/AzureLogAnalyticsOptionsConverterFactory.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,187 @@
// Copyright (c) Microsoft Corporation.
// Licensed under the MIT License.

using System.Text.Json;
using System.Text.Json.Serialization;
using Azure.DataApiBuilder.Config.ObjectModel;

namespace Azure.DataApiBuilder.Config.Converters;

/// <summary>
/// Defines how DAB reads and writes Azure Log Analytics options.
/// </summary>
internal class AzureLogAnalyticsOptionsConverterFactory : JsonConverterFactory
{
// Determines whether to replace environment variable with its
// value or not while deserializing.
private bool _replaceEnvVar;

/// <inheritdoc/>
public override bool CanConvert(Type typeToConvert)
{
return typeToConvert.IsAssignableTo(typeof(AzureLogAnalyticsOptions));
}

/// <inheritdoc/>
public override JsonConverter? CreateConverter(Type typeToConvert, JsonSerializerOptions options)
{
return new AzureLogAnalyticsOptionsConverter(_replaceEnvVar);
}

/// <param name="replaceEnvVar">Whether to replace environment variable with its
/// value or not while deserializing.</param>
internal AzureLogAnalyticsOptionsConverterFactory(bool replaceEnvVar)
{
_replaceEnvVar = replaceEnvVar;
}

private class AzureLogAnalyticsOptionsConverter : JsonConverter<AzureLogAnalyticsOptions>
{
// Determines whether to replace environment variable with its
// value or not while deserializing.
private bool _replaceEnvVar;

/// <param name="replaceEnvVar">Whether to replace environment variable with its
/// value or not while deserializing.</param>
internal AzureLogAnalyticsOptionsConverter(bool replaceEnvVar)
{
_replaceEnvVar = replaceEnvVar;
}

/// <summary>
/// Defines how DAB reads Azure Log Analytics options and defines which values are
/// used to instantiate AzureLogAnalyticsOptions.
/// Uses default deserialize.
/// </summary>
/// <exception cref="JsonException">Thrown when improperly formatted Azure Log Analytics options are provided.</exception>
public override AzureLogAnalyticsOptions? Read(ref Utf8JsonReader reader, Type typeToConvert, JsonSerializerOptions options)
{
if (reader.TokenType is JsonTokenType.StartObject)
{
AzureLogAnalyticsOptions azureLogAnalyticsOptions = new();
AzureLogAnalyticsAuthOptionsConverter authOptionsConverter = new(_replaceEnvVar);

while (reader.Read())
{
if (reader.TokenType == JsonTokenType.EndObject)
{
break;
}

string? propertyName = reader.GetString();

reader.Read();
switch (propertyName)
{
case "enabled":
if (reader.TokenType is JsonTokenType.True || reader.TokenType is JsonTokenType.False)
Copy link
Contributor

@aaronburtle aaronburtle Jul 3, 2025

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

should we give the default value in the case of a null here, and for log-type and flush-interval?

{
azureLogAnalyticsOptions = azureLogAnalyticsOptions with { Enabled = reader.GetBoolean() };
}
else
{
throw new JsonException($"Unsupported value entered for the property 'enabled': {reader.TokenType}");
}

break;

case "auth":
azureLogAnalyticsOptions = azureLogAnalyticsOptions with { Auth = authOptionsConverter.Read(ref reader, typeToConvert, options) };
break;

case "log-type":
if (reader.TokenType is JsonTokenType.String)
{
string? logType = reader.DeserializeString(_replaceEnvVar);
if (logType is null)
{
logType = "DabLogs";
}

azureLogAnalyticsOptions = azureLogAnalyticsOptions with { LogType = logType };
}
else
{
throw new JsonException($"Unexpected type of value entered for log-type: {reader.TokenType}");
}

break;

case "flush-interval-seconds":
if (reader.TokenType is JsonTokenType.Number)
{
int flushIntSec;
try
{
flushIntSec = reader.GetInt32();
}
catch (FormatException)
{
throw new JsonException($"The JSON token value is of the incorrect numeric format.");
}

if (flushIntSec <= 0)
{
throw new JsonException($"Invalid flush-interval-seconds: {flushIntSec}. Specify a number > 0.");
}

azureLogAnalyticsOptions = azureLogAnalyticsOptions with { FlushIntervalSeconds = flushIntSec };
}
else
{
throw new JsonException($"Unsupported value entered for flush-interval-seconds: {reader.TokenType}");
}

break;

default:
throw new JsonException($"Unexpected property {propertyName}");
}
}

return azureLogAnalyticsOptions;
}

throw new JsonException("Failed to read the Azure Log Analytics Options");
}

/// <summary>
/// When writing the AzureLogAnalyticsOptions back to a JSON file, only write the properties
/// if they are user provided. This avoids polluting the written JSON file with properties
/// the user most likely omitted when writing the original DAB runtime config file.
/// This Write operation is only used when a RuntimeConfig object is serialized to JSON.
/// </summary>
public override void Write(Utf8JsonWriter writer, AzureLogAnalyticsOptions value, JsonSerializerOptions options)
{
writer.WriteStartObject();

if (value?.UserProvidedEnabled is true)
{
writer.WritePropertyName("enabled");
JsonSerializer.Serialize(writer, value.Enabled, options);
}

if (value?.Auth is not null)
{
AzureLogAnalyticsAuthOptionsConverter authOptionsConverter = options.GetConverter(typeof(AzureLogAnalyticsAuthOptions)) as AzureLogAnalyticsAuthOptionsConverter ??
throw new JsonException("Failed to get azure-log-analytics.auth options converter");

authOptionsConverter.Write(writer, value.Auth, options);
}

if (value?.UserProvidedLogType is true)
{
writer.WritePropertyName("log-type");
JsonSerializer.Serialize(writer, value.LogType, options);
}

if (value?.UserProvidedFlushIntervalSeconds is true)
{
writer.WritePropertyName("flush-interval-seconds");
JsonSerializer.Serialize(writer, value.FlushIntervalSeconds, options);
}

writer.WriteEndObject();
}
}
}
Loading