|
| 1 | +[[configuration]] |
| 2 | +== Configuration |
| 3 | + |
| 4 | +Configuration of the OpenTelemetry SDK should be performed through the |
| 5 | +mechanisms https://opentelemetry.io/docs/languages/net/automatic/configuration/[documented on the OpenTelemetry website]. |
| 6 | + |
| 7 | +The Elastic distribution can be further configured using advanced settings when |
| 8 | +you need complete control of its behaviour. Configuration can be achieved by setting environment variables, |
| 9 | +using the `IConfiguration` integration, or manually configuring the Elastic distribution. |
| 10 | + |
| 11 | +=== Environment variables |
| 12 | + |
| 13 | +The Elastic distribution can be configured using environment variables. This is a cross-platform |
| 14 | +way to configure the Elastic distribution and is especially useful in containerized environments. |
| 15 | + |
| 16 | +Environment variables are read at startup and can be used to configure the Elastic distribution. |
| 17 | +For details of the various options available and their corresponding environment variable names, |
| 18 | +see <<configuration-options>>. |
| 19 | + |
| 20 | +Environment variables always take precedence over configuration provided by the `IConfiguration` |
| 21 | +system. |
| 22 | + |
| 23 | +=== IConfiguration integration |
| 24 | + |
| 25 | +In applications that use the "host" pattern, such as ASP.NET Core and worker service, the Elastic |
| 26 | +distribution can be configured using the `IConfiguration` integration. This is done by passing an |
| 27 | +`IConfiguration` instance to the `AddElasticOpenTelemetry` extension method on the `IServiceCollection`. |
| 28 | + |
| 29 | +When using an `IHostApplicationBuilder` such as modern ASP.NET Core applications, the current `IConfiguration` |
| 30 | +can be accessed via the `Configuration` property on the builder. |
| 31 | + |
| 32 | +[source,csharp] |
| 33 | +---- |
| 34 | +var builder = WebApplication.CreateBuilder(args); |
| 35 | +var currentConfig = builder.Configuration; <1> |
| 36 | +---- |
| 37 | +<1> Access the current `IConfiguration` instance from the builder. |
| 38 | + |
| 39 | +By default, at this stage, the configuration will be populated from the default configuration sources, |
| 40 | +including the `appsettings.json` file(s) and command-line arguments. You may use these sources to define |
| 41 | +the configuration for the Elastic distribution. |
| 42 | + |
| 43 | +For example, you can define the configuration for the Elastic distribution in the `appsettings.json` file: |
| 44 | + |
| 45 | +[source,json] |
| 46 | +---- |
| 47 | +{ |
| 48 | + "Elastic": { |
| 49 | + "OpenTelemetry": { |
| 50 | + "FileLogDirectory": "C:\\Logs" <1> |
| 51 | + } |
| 52 | + } |
| 53 | +} |
| 54 | +---- |
| 55 | +<1> This example sets the file log directory to `C:\Logs` which enables diagnostic file logging. |
| 56 | + |
| 57 | +Configuration from the "Elastic:OpenTelemetry" section of the `IConfiguration` instance will be |
| 58 | +bound to the `ElasticOpenTelemetryOptions` instance used to configure the Elastic distribution. |
| 59 | + |
| 60 | +To learn more about the Microsoft configuration system, see |
| 61 | +https://learn.microsoft.com/en-us/aspnet/core/fundamentals/configuration[Configuration in ASP.NET Core]. |
| 62 | + |
| 63 | +=== Manual configuration |
| 64 | + |
| 65 | +In all other scenarios, configuration can be achieved manually in code. This is done by creating |
| 66 | +an instance of `ElasticOpenTelemetryBuilderOptions` and passing it to the `ElasticOpenTelemetryBuilder` constructor |
| 67 | +or an overload of the `AddElasticOpenTelemetry` extension method on the `IServiceCollection`. |
| 68 | + |
| 69 | +For example, in traditional console applications, you can configure the Elastic distribution like this: |
| 70 | + |
| 71 | +[source,csharp] |
| 72 | +---- |
| 73 | +using Elastic.OpenTelemetry; |
| 74 | +using Elastic.OpenTelemetry.Configuration; |
| 75 | +using Elastic.OpenTelemetry.Extensions; |
| 76 | +using Microsoft.Extensions.DependencyInjection; |
| 77 | +using OpenTelemetry; |
| 78 | +
|
| 79 | +var services = new ServiceCollection(); |
| 80 | +
|
| 81 | +var builderOptions = new ElasticOpenTelemetryBuilderOptions <1> |
| 82 | +{ |
| 83 | + DistroOptions = new ElasticOpenTelemetryOptions <2> |
| 84 | + { |
| 85 | + FileLogDirectory = "C:\\Logs", <3> |
| 86 | + } |
| 87 | +}; |
| 88 | +
|
| 89 | +await using var session = new ElasticOpenTelemetryBuilder(builderOptions) <4> |
| 90 | + .WithTracing(b => b.AddSource("MySource")) |
| 91 | + .Build(); |
| 92 | +---- |
| 93 | +<1> Create an instance of `ElasticOpenTelemetryBuilderOptions` |
| 94 | +<2> Create an instance of `ElasticOpenTelemetryOptions` and configure the file log directory by |
| 95 | +setting the corresponding property. |
| 96 | +<3> This example sets the file log directory to `C:\Logs` which enables diagnostic file logging. |
| 97 | +<4> Pass the `ElasticOpenTelemetryBuilderOptions` instance to the `ElasticOpenTelemetryBuilder` constructor |
| 98 | +to configure the Elastic distribution. |
| 99 | + |
| 100 | +[[configuration-options]] |
| 101 | +=== Configuration options |
| 102 | + |
| 103 | +[float] |
| 104 | +[[config-filelogdirectory]] |
| 105 | +==== `FileLogDirectory` |
| 106 | + |
| 107 | +A string specifying the directory where the Elastic distribution will write diagnostic log files. |
| 108 | +When not provided, no file logging will occur. Each new .NET process will create a new log file in the |
| 109 | +specified directory. |
| 110 | + |
| 111 | +[options="header"] |
| 112 | +|============ |
| 113 | +| Environment variable name | IConfiguration key |
| 114 | +| `ELASTIC_OTEL_FILE_LOG_DIRECTORY` | `Elastic:OpenTelemetry:FileLogDirectory` |
| 115 | +|============ |
| 116 | + |
| 117 | +[options="header"] |
| 118 | +|============ |
| 119 | +| Default | Type |
| 120 | +| `string.Empty` | String |
| 121 | +|============ |
| 122 | + |
| 123 | + |
| 124 | +[float] |
| 125 | +[[config-fileloglevel]] |
| 126 | +==== `FileLogLevel` |
| 127 | + |
| 128 | +Sets the logging level for the distribtuion. |
| 129 | + |
| 130 | +Valid options: `Critical`, `Error`, `Warning`, `Information`, `Debug`, `Trace` and `None` (`None` disables the logging). |
| 131 | + |
| 132 | +[options="header"] |
| 133 | +|============ |
| 134 | +| Environment variable name | IConfiguration key |
| 135 | +| `ELASTIC_OTEL_FILE_LOG_LEVEL` | `Elastic:OpenTelemetry:FileLogLevel` |
| 136 | +|============ |
| 137 | + |
| 138 | +[options="header"] |
| 139 | +|============ |
| 140 | +| Default | Type |
| 141 | +| `Information` | String |
| 142 | +|============ |
| 143 | + |
| 144 | + |
| 145 | +[float] |
| 146 | +[[config-skipotlpexporter]] |
| 147 | +==== `SkipOtlpExporter` |
| 148 | + |
| 149 | +Allows the distribution to used with its defaults, but without enabling the export of telemetry data to |
| 150 | +an OTLP endpoint. This can be useful when you want to test applications without sending telemetry data. |
| 151 | + |
| 152 | +[options="header"] |
| 153 | +|============ |
| 154 | +| Environment variable name | IConfiguration key |
| 155 | +| `ELASTIC_OTEL_SKIP_OTLP_EXPORTER` | `Elastic:OpenTelemetry:SkipOtlpExporter` |
| 156 | +|============ |
| 157 | + |
| 158 | +[options="header"] |
| 159 | +|============ |
| 160 | +| Default | Type |
| 161 | +| `false` | Bool |
| 162 | +|============ |
| 163 | + |
| 164 | + |
| 165 | +[float] |
| 166 | +[[config-enabledelasticdefaults]] |
| 167 | +==== `EnabledElasticDefaults` |
| 168 | + |
| 169 | +A comma-separated list of Elastic defaults to enable. This can be useful when you want to enable |
| 170 | +only some of the Elastic distribution opinionated defaults. |
| 171 | + |
| 172 | +Valid options: `None`, `Tracing`, `Metrics`, `Logging`. |
| 173 | + |
| 174 | +Except for the `None` option, all other options can be combined. |
| 175 | + |
| 176 | +When this setting is not configured or the value is `string.Empty`, all Elastic distribution defaults will be enabled. |
| 177 | + |
| 178 | +When `None` is specified, no Elastic distribution defaults will be enabled, and you will need to manually |
| 179 | +configure the OpenTelemetry SDK to enable collection of telemetry signals. In this mode, the Elastic distribution |
| 180 | +does not provide any opinionated defaults, nor register any processors, allowing you to start with the "vanilla" |
| 181 | +OpenTelemetry SDK configuration. You may then choose to configure the various providers and register processors |
| 182 | +as required. |
| 183 | + |
| 184 | +In all other cases, the Elastic distribution will enable the specified defaults. For example, to enable only |
| 185 | +Elastic defaults only for tracing and metrics, set this value to `Tracing,Metrics`. |
| 186 | + |
| 187 | +[options="header"] |
| 188 | +|============ |
| 189 | +| Environment variable name | IConfiguration key |
| 190 | +| `ELASTIC_OTEL_ENABLE_ELASTIC_DEFAULTS` | `Elastic:OpenTelemetry:EnabledElasticDefaults` |
| 191 | +|============ |
| 192 | + |
| 193 | +[options="header"] |
| 194 | +|============ |
| 195 | +| Default | Type |
| 196 | +| `string.Empty` | String |
| 197 | +|============ |
0 commit comments