|
| 1 | +// Copyright (c) Microsoft Corporation. |
| 2 | +// Licensed under the MIT License. |
| 3 | + |
| 4 | +using System.Diagnostics.CodeAnalysis; |
| 5 | +using System.Text.Json.Serialization; |
| 6 | + |
| 7 | +namespace Azure.DataApiBuilder.Config.ObjectModel; |
| 8 | + |
| 9 | +/// <summary> |
| 10 | +/// Represents the options for configuring file sink telemetry. |
| 11 | +/// </summary> |
| 12 | +public record FileSinkOptions |
| 13 | +{ |
| 14 | + /// <summary> |
| 15 | + /// Default enabled for File Sink. |
| 16 | + /// </summary> |
| 17 | + public const bool DEFAULT_ENABLED = false; |
| 18 | + |
| 19 | + /// <summary> |
| 20 | + /// Default path for File Sink. |
| 21 | + /// </summary> |
| 22 | + public const string DEFAULT_PATH = "/logs/dab-log.txt"; |
| 23 | + |
| 24 | + /// <summary> |
| 25 | + /// Default rolling interval for File Sink. |
| 26 | + /// </summary> |
| 27 | + public const string DEFAULT_ROLLING_INTERVAL = nameof(RollingIntervalMode.Day); |
| 28 | + |
| 29 | + /// <summary> |
| 30 | + /// Default retained file count limit for File Sink. |
| 31 | + /// </summary> |
| 32 | + public const int DEFAULT_RETAINED_FILE_COUNT_LIMIT = 1; |
| 33 | + |
| 34 | + /// <summary> |
| 35 | + /// Default file size limit bytes for File Sink. |
| 36 | + /// </summary> |
| 37 | + public const int DEFAULT_FILE_SIZE_LIMIT_BYTES = 1048576; |
| 38 | + |
| 39 | + /// <summary> |
| 40 | + /// Whether File Sink is enabled. |
| 41 | + /// </summary> |
| 42 | + public bool Enabled { get; init; } |
| 43 | + |
| 44 | + /// <summary> |
| 45 | + /// Path to the file where logs will be uploaded. |
| 46 | + /// </summary> |
| 47 | + public string? Path { get; init; } |
| 48 | + |
| 49 | + /// <summary> |
| 50 | + /// Time it takes for files with logs to be discarded. |
| 51 | + /// </summary> |
| 52 | + public string? RollingInterval { get; init; } |
| 53 | + |
| 54 | + /// <summary> |
| 55 | + /// Amount of files that can exist simultaneously in which logs are saved. |
| 56 | + /// </summary> |
| 57 | + public int? RetainedFileCountLimit { get; init; } |
| 58 | + |
| 59 | + /// <summary> |
| 60 | + /// File size limit in bytes before a new file needs to be created. |
| 61 | + /// </summary> |
| 62 | + public int? FileSizeLimitBytes { get; init; } |
| 63 | + |
| 64 | + [JsonConstructor] |
| 65 | + public FileSinkOptions(bool? enabled = null, string? path = null, RollingIntervalMode? rollingInterval = null, int? retainedFileCountLimit = null, int? fileSizeLimitBytes = null) |
| 66 | + { |
| 67 | + if (enabled is not null) |
| 68 | + { |
| 69 | + Enabled = (bool)enabled; |
| 70 | + UserProvidedEnabled = true; |
| 71 | + } |
| 72 | + else |
| 73 | + { |
| 74 | + Enabled = DEFAULT_ENABLED; |
| 75 | + } |
| 76 | + |
| 77 | + if (path is not null) |
| 78 | + { |
| 79 | + Path = path; |
| 80 | + UserProvidedPath = true; |
| 81 | + } |
| 82 | + else |
| 83 | + { |
| 84 | + Path = DEFAULT_PATH; |
| 85 | + } |
| 86 | + |
| 87 | + if (rollingInterval is not null) |
| 88 | + { |
| 89 | + RollingInterval = rollingInterval.ToString(); |
| 90 | + UserProvidedRollingInterval = true; |
| 91 | + } |
| 92 | + else |
| 93 | + { |
| 94 | + RollingInterval = DEFAULT_ROLLING_INTERVAL; |
| 95 | + } |
| 96 | + |
| 97 | + if (retainedFileCountLimit is not null) |
| 98 | + { |
| 99 | + RetainedFileCountLimit = retainedFileCountLimit; |
| 100 | + UserProvidedRetainedFileCountLimit = true; |
| 101 | + } |
| 102 | + else |
| 103 | + { |
| 104 | + RetainedFileCountLimit = DEFAULT_RETAINED_FILE_COUNT_LIMIT; |
| 105 | + } |
| 106 | + |
| 107 | + if (fileSizeLimitBytes is not null) |
| 108 | + { |
| 109 | + FileSizeLimitBytes = fileSizeLimitBytes; |
| 110 | + UserProvidedFileSizeLimitBytes = true; |
| 111 | + } |
| 112 | + else |
| 113 | + { |
| 114 | + FileSizeLimitBytes = DEFAULT_FILE_SIZE_LIMIT_BYTES; |
| 115 | + } |
| 116 | + } |
| 117 | + |
| 118 | + /// <summary> |
| 119 | + /// Flag which informs CLI and JSON serializer whether to write enabled |
| 120 | + /// property/value to the runtime config file. |
| 121 | + /// When user doesn't provide the enabled property/value, which signals DAB to use the default, |
| 122 | + /// the DAB CLI should not write the default value to a serialized config. |
| 123 | + /// </summary> |
| 124 | + [JsonIgnore(Condition = JsonIgnoreCondition.Always)] |
| 125 | + [MemberNotNullWhen(true, nameof(Enabled))] |
| 126 | + public bool UserProvidedEnabled { get; init; } = false; |
| 127 | + |
| 128 | + /// <summary> |
| 129 | + /// Flag which informs CLI and JSON serializer whether to write path |
| 130 | + /// property/value to the runtime config file. |
| 131 | + /// When user doesn't provide the path property/value, which signals DAB to use the default, |
| 132 | + /// the DAB CLI should not write the default value to a serialized config. |
| 133 | + /// </summary> |
| 134 | + [JsonIgnore(Condition = JsonIgnoreCondition.Always)] |
| 135 | + [MemberNotNullWhen(true, nameof(Path))] |
| 136 | + public bool UserProvidedPath { get; init; } = false; |
| 137 | + |
| 138 | + /// <summary> |
| 139 | + /// Flag which informs CLI and JSON serializer whether to write rolling-interval |
| 140 | + /// property/value to the runtime config file. |
| 141 | + /// When user doesn't provide the rolling-interval property/value, which signals DAB to use the default, |
| 142 | + /// the DAB CLI should not write the default value to a serialized config. |
| 143 | + /// </summary> |
| 144 | + [JsonIgnore(Condition = JsonIgnoreCondition.Always)] |
| 145 | + [MemberNotNullWhen(true, nameof(RollingInterval))] |
| 146 | + public bool UserProvidedRollingInterval { get; init; } = false; |
| 147 | + |
| 148 | + /// <summary> |
| 149 | + /// Flag which informs CLI and JSON serializer whether to write retained-file-count-limit |
| 150 | + /// property/value to the runtime config file. |
| 151 | + /// When user doesn't provide the retained-file-count-limit property/value, which signals DAB to use the default, |
| 152 | + /// the DAB CLI should not write the default value to a serialized config. |
| 153 | + /// </summary> |
| 154 | + [JsonIgnore(Condition = JsonIgnoreCondition.Always)] |
| 155 | + [MemberNotNullWhen(true, nameof(RetainedFileCountLimit))] |
| 156 | + public bool UserProvidedRetainedFileCountLimit { get; init; } = false; |
| 157 | + |
| 158 | + /// <summary> |
| 159 | + /// Flag which informs CLI and JSON serializer whether to write file-size-limit-bytes |
| 160 | + /// property/value to the runtime config file. |
| 161 | + /// When user doesn't provide the file-size-limit-bytes property/value, which signals DAB to use the default, |
| 162 | + /// the DAB CLI should not write the default value to a serialized config. |
| 163 | + /// </summary> |
| 164 | + [JsonIgnore(Condition = JsonIgnoreCondition.Always)] |
| 165 | + [MemberNotNullWhen(true, nameof(FileSizeLimitBytes))] |
| 166 | + public bool UserProvidedFileSizeLimitBytes { get; init; } = false; |
| 167 | +} |
0 commit comments