diff --git a/schemas/dab.draft.schema.json b/schemas/dab.draft.schema.json index 692b82b642..61fc4c19b9 100644 --- a/schemas/dab.draft.schema.json +++ b/schemas/dab.draft.schema.json @@ -410,6 +410,40 @@ }, "required": ["endpoint"] }, + "file": { + "type": "object", + "additionalProperties": false, + "properties": { + "enabled": { + "type": "boolean", + "description": "Enable/disable file sink telemetry logging.", + "default": false + }, + "path": { + "type": "string", + "description": "File path for telemetry logs.", + "default": "/logs/dab-log.txt" + }, + "rolling-interval": { + "type": "string", + "description": "Rolling interval for log files.", + "default": "day", + "enum": ["hour", "day", "week"] + }, + "retained-file-count-limit": { + "type": "integer", + "description": "Maximum number of retained log files.", + "default": 1, + "minimum": 1 + }, + "file-size-limit-bytes": { + "type": "integer", + "description": "Maximum file size in bytes before rolling.", + "default": 1048576, + "minimum": 1 + } + } + }, "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", diff --git a/src/Cli/Commands/AddTelemetryOptions.cs b/src/Cli/Commands/AddTelemetryOptions.cs index 8ec9313d14..f906db7429 100644 --- a/src/Cli/Commands/AddTelemetryOptions.cs +++ b/src/Cli/Commands/AddTelemetryOptions.cs @@ -27,6 +27,11 @@ public AddTelemetryOptions( string? openTelemetryHeaders = null, OtlpExportProtocol? openTelemetryExportProtocol = null, string? openTelemetryServiceName = null, + CliBool? fileSinkEnabled = null, + string? fileSinkPath = null, + RollingIntervalMode? fileSinkRollingInterval = null, + int? fileSinkRetainedFileCountLimit = null, + int? fileSinkFileSizeLimitBytes = null, string? config = null) : base(config) { AppInsightsConnString = appInsightsConnString; @@ -36,6 +41,11 @@ public AddTelemetryOptions( OpenTelemetryHeaders = openTelemetryHeaders; OpenTelemetryExportProtocol = openTelemetryExportProtocol; OpenTelemetryServiceName = openTelemetryServiceName; + FileSinkEnabled = fileSinkEnabled; + FileSinkPath = fileSinkPath; + FileSinkRollingInterval = fileSinkRollingInterval; + FileSinkRetainedFileCountLimit = fileSinkRetainedFileCountLimit; + FileSinkFileSizeLimitBytes = fileSinkFileSizeLimitBytes; } // Connection string for the Application Insights resource to which telemetry data should be sent. @@ -68,6 +78,26 @@ public AddTelemetryOptions( [Option("otel-service-name", Default = "dab", Required = false, HelpText = "(Default: dab) Headers for Open Telemetry for telemetry data")] public string? OpenTelemetryServiceName { get; } + // To specify whether File Sink telemetry should be enabled. This flag is optional and default value is false. + [Option("file-sink-enabled", Default = CliBool.False, Required = false, HelpText = "Enable/disable file sink telemetry logging. Default: false (boolean).")] + public CliBool? FileSinkEnabled { get; } + + // Path for the File Sink resource to which telemetry data should be sent. This flag is optional and default value is '/logs/dab-log.txt'. + [Option("file-sink-path", Default = "/logs/dab-log.txt", Required = false, HelpText = "File path for telemetry logs. Default: '/logs/dab-log.txt' (string).")] + public string? FileSinkPath { get; } + + // Rolling Interval for the File Sink resource to which telemetry data should be sent. This flag is optional and default value is new interval per Day. + [Option("file-sink-rolling-interval", Default = RollingIntervalMode.Day, Required = false, HelpText = "Rolling interval for log files. Default: Day. Allowed values: Hour, Day, Week.")] + public RollingIntervalMode? FileSinkRollingInterval { get; } + + // Retained File Count Limit for the File Sink resource to which telemetry data should be sent. This flag is optional and default value is 1. + [Option("file-sink-retained-file-count-limit", Default = 1, Required = false, HelpText = "Maximum number of retained log files. Default: 1 (integer, minimum: 1).")] + public int? FileSinkRetainedFileCountLimit { get; } + + // File Size Limit Bytes for the File Sink resource to which telemetry data should be sent. This flag is optional and default value is 1048576. + [Option("file-sink-file-size-limit-bytes", Default = 1048576, Required = false, HelpText = "Maximum file size in bytes before rolling. Default: 1048576 (integer, minimum: 1).")] + public int? FileSinkFileSizeLimitBytes { get; } + public int Handler(ILogger logger, FileSystemRuntimeConfigLoader loader, IFileSystem fileSystem) { logger.LogInformation("{productName} {version}", PRODUCT_NAME, ProductInfo.GetProductVersion()); diff --git a/src/Cli/ConfigGenerator.cs b/src/Cli/ConfigGenerator.cs index 7655b84cee..3b6aba16e3 100644 --- a/src/Cli/ConfigGenerator.cs +++ b/src/Cli/ConfigGenerator.cs @@ -1969,22 +1969,21 @@ public static bool TryAddTelemetry(AddTelemetryOptions options, FileSystemRuntim ServiceName: options.OpenTelemetryServiceName ); + FileSinkOptions fileSinkOptions = new( + Enabled: options.FileSinkEnabled is CliBool.True ? true : false, + Path: options.FileSinkPath, + RollingInterval: options.FileSinkRollingInterval, + RetainedFileCountLimit: options.FileSinkRetainedFileCountLimit, + FileSizeLimitBytes: options.FileSinkFileSizeLimitBytes + ); + runtimeConfig = runtimeConfig with { Runtime = runtimeConfig.Runtime with { Telemetry = runtimeConfig.Runtime.Telemetry is null - ? new TelemetryOptions(ApplicationInsights: applicationInsightsOptions, OpenTelemetry: openTelemetryOptions) - : runtimeConfig.Runtime.Telemetry with { ApplicationInsights = applicationInsightsOptions, OpenTelemetry = openTelemetryOptions } - } - }; - runtimeConfig = runtimeConfig with - { - Runtime = runtimeConfig.Runtime with - { - Telemetry = runtimeConfig.Runtime.Telemetry is null - ? new TelemetryOptions(ApplicationInsights: applicationInsightsOptions) - : runtimeConfig.Runtime.Telemetry with { ApplicationInsights = applicationInsightsOptions } + ? new TelemetryOptions(ApplicationInsights: applicationInsightsOptions, OpenTelemetry: openTelemetryOptions, File: fileSinkOptions) + : runtimeConfig.Runtime.Telemetry with { ApplicationInsights = applicationInsightsOptions, OpenTelemetry = openTelemetryOptions, File = fileSinkOptions } } }; diff --git a/src/Config/ObjectModel/FileSinkOptions.cs b/src/Config/ObjectModel/FileSinkOptions.cs new file mode 100644 index 0000000000..d01ba3f1ce --- /dev/null +++ b/src/Config/ObjectModel/FileSinkOptions.cs @@ -0,0 +1,25 @@ +// Copyright (c) Microsoft Corporation. +// Licensed under the MIT License. + +namespace Azure.DataApiBuilder.Config.ObjectModel; + +/// +/// Represents the options for configuring file sink telemetry. +/// +public record FileSinkOptions( + bool Enabled = false, + string? Path = "/logs/dab-log.txt", + RollingIntervalMode? RollingInterval = RollingIntervalMode.Day, + int? RetainedFileCountLimit = 1, + int? FileSizeLimitBytes = 1048576) +{ + public bool Enabled { get; init; } = Enabled; + + public string? Path { get; init; } = Path; + + public RollingIntervalMode? RollingInterval { get; init; } = RollingInterval; + + public int? RetainedFileCountLimit { get; init; } = RetainedFileCountLimit; + + public int? FileSizeLimitBytes { get; init; } = FileSizeLimitBytes; +} diff --git a/src/Config/ObjectModel/FileSinkRollingIntervalMode.cs b/src/Config/ObjectModel/FileSinkRollingIntervalMode.cs new file mode 100644 index 0000000000..a807a9b0aa --- /dev/null +++ b/src/Config/ObjectModel/FileSinkRollingIntervalMode.cs @@ -0,0 +1,18 @@ +// Copyright (c) Microsoft Corporation. +// Licensed under the MIT License. + +using System.Text.Json.Serialization; + +namespace Azure.DataApiBuilder.Config.ObjectModel; + +/// +/// Represents the rolling interval options for file sink. +/// The time it takes between the creation of new files. +/// +[JsonConverter(typeof(JsonStringEnumConverter))] +public enum RollingIntervalMode +{ + Hour, + Day, + Week +} diff --git a/src/Config/ObjectModel/TelemetryOptions.cs b/src/Config/ObjectModel/TelemetryOptions.cs index ed2099f2a4..9956deee82 100644 --- a/src/Config/ObjectModel/TelemetryOptions.cs +++ b/src/Config/ObjectModel/TelemetryOptions.cs @@ -9,7 +9,7 @@ namespace Azure.DataApiBuilder.Config.ObjectModel; /// /// Represents the options for telemetry. /// -public record TelemetryOptions(ApplicationInsightsOptions? ApplicationInsights = null, OpenTelemetryOptions? OpenTelemetry = null, Dictionary? LoggerLevel = null) +public record TelemetryOptions(ApplicationInsightsOptions? ApplicationInsights = null, OpenTelemetryOptions? OpenTelemetry = null, FileSinkOptions? File = null, Dictionary? LoggerLevel = null) { [JsonPropertyName("log-level")] public Dictionary? LoggerLevel { get; init; } = LoggerLevel;