Skip to content
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

Configurable log level - native code and loader #2288

Merged
Merged
4 changes: 4 additions & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -9,12 +9,16 @@ This component adheres to [Semantic Versioning](https://semver.org/spec/v2.0.0.h

### Added

- Support `OTEL_LOG_LEVEL` to configure SDK logging level.
pellared marked this conversation as resolved.
Show resolved Hide resolved

### Changed

### Deprecated

### Removed

- Remove support for enabling debugging mode with `OTEL_DOTNET_AUTO_DEBUG`.

### Fixed

### Security
Expand Down
19 changes: 10 additions & 9 deletions docs/config.md
Original file line number Diff line number Diff line change
Expand Up @@ -23,7 +23,7 @@ with environment variables taking precedence over `App.config` or `Web.config` f
- `OTEL_DOTNET_AUTO_INTEGRATIONS_FILE`
- `OTEL_DOTNET_AUTO_[TRACES|METRICS|LOGS]_[ENABLED|DISABLED]_INSTRUMENTATIONS`
- `OTEL_DOTNET_AUTO_LOG_DIRECTORY`
- `OTEL_DOTNET_AUTO_DEBUG`
- `OTEL_LOG_LEVEL`
- `OTEL_DOTNET_AUTO_NETFX_REDIRECT_ENABLED`

Example with `OTEL_SERVICE_NAME` setting:
Expand All @@ -48,6 +48,7 @@ However, if given setting supports it, then:
|--------------------------------------|------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------|---------------|
| `OTEL_DOTNET_AUTO_HOME` | Installation location. | |
| `OTEL_DOTNET_AUTO_EXCLUDE_PROCESSES` | Names of the executable files that the profiler cannot instrument. Supports multiple comma-separated values, for example: `ReservedProcess.exe,powershell.exe`. If unset, the profiler attaches to all processes by default. | |
| `OTEL_LOG_LEVEL` | SDK log level. (supported values: `none`,`error`,`warn`,`info`,`debug`) | `info` |

## Resources

Expand Down Expand Up @@ -363,11 +364,11 @@ If the default log directories can't be created,
the instrumentation uses the path of the current user's [temporary folder](https://docs.microsoft.com/en-us/dotnet/api/System.IO.Path.GetTempPath?view=net-6.0)
instead.

| Environment variable | Description | Default value |
|-----------------------------------------------------|---------------------------------------------------------|------------------------------------------|
| `OTEL_DOTNET_AUTO_LOG_DIRECTORY` | Directory of the .NET Tracer logs. | _See the previous note on default paths_ |
| `OTEL_DOTNET_AUTO_DEBUG` | Enables debugging mode for the tracer. | `false` |
| `OTEL_DOTNET_AUTO_TRACES_CONSOLE_EXPORTER_ENABLED` | Whether the traces console exporter is enabled or not. | `false` |
| `OTEL_DOTNET_AUTO_METRICS_CONSOLE_EXPORTER_ENABLED` | Whether the metrics console exporter is enabled or not. | `false` |
| `OTEL_DOTNET_AUTO_LOGS_CONSOLE_EXPORTER_ENABLED` | Whether the logs console exporter is enabled or not. | `false` |
| `OTEL_DOTNET_AUTO_LOGS_INCLUDE_FORMATTED_MESSAGE` | Whether the log state should be formatted. | `false` |
| Environment variable | Description | Default value |
|-----------------------------------------------------|-------------------------------------------------------------------------|------------------------------------------|
| `OTEL_DOTNET_AUTO_LOG_DIRECTORY` | Directory of the .NET Tracer logs. | _See the previous note on default paths_ |
| `OTEL_LOG_LEVEL` | SDK log level. (supported values: `none`,`error`,`warn`,`info`,`debug`) | `info` |
| `OTEL_DOTNET_AUTO_TRACES_CONSOLE_EXPORTER_ENABLED` | Whether the traces console exporter is enabled or not. | `false` |
| `OTEL_DOTNET_AUTO_METRICS_CONSOLE_EXPORTER_ENABLED` | Whether the metrics console exporter is enabled or not. | `false` |
| `OTEL_DOTNET_AUTO_LOGS_CONSOLE_EXPORTER_ENABLED` | Whether the logs console exporter is enabled or not. | `false` |
| `OTEL_DOTNET_AUTO_LOGS_INCLUDE_FORMATTED_MESSAGE` | Whether the log state should be formatted. | `false` |
4 changes: 2 additions & 2 deletions docs/troubleshooting.md
Original file line number Diff line number Diff line change
Expand Up @@ -96,14 +96,14 @@ Detailed debug logs can help you troubleshoot instrumentation issues, and can be
attached to issues in this project to facilitate investigation.

To get the detailed logs from the OpenTelemetry .NET Automatic Instrumentation, set
the `OTEL_DOTNET_AUTO_DEBUG` environment variable to `true` before the
the `OTEL_LOG_LEVEL` environment variable to `debug` before the
instrumented process starts.

By default, the library writes the log files under predefined locations. If needed,
change the default location by updating the `OTEL_DOTNET_AUTO_LOG_DIRECTORY`
environment variable.

After obtaining the logs, remove the `OTEL_DOTNET_AUTO_DEBUG`
After obtaining the logs, remove the `OTEL_LOG_LEVEL`
environment variable to avoid unnecessary overhead.

## Nothing happens
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,25 @@
// <copyright file="AutoInstrumentationEventSource.Name.cs" company="OpenTelemetry Authors">
// Copyright The OpenTelemetry Authors
//
// Licensed under the Apache License, Version 2.0 (the "License");
// you may not use this file except in compliance with the License.
// You may obtain a copy of the License at
//
// http://www.apache.org/licenses/LICENSE-2.0
//
// Unless required by applicable law or agreed to in writing, software
// distributed under the License is distributed on an "AS IS" BASIS,
// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
// See the License for the specific language governing permissions and
// limitations under the License.
// </copyright>

using System.Diagnostics.Tracing;

// ReSharper disable once CheckNamespace
namespace OpenTelemetry.AutoInstrumentation;

[EventSource(Name = "OpenTelemetry-AutoInstrumentation-Loader")]
internal partial class AutoInstrumentationEventSource
{
}
70 changes: 0 additions & 70 deletions src/OpenTelemetry.AutoInstrumentation.Loader/FileSink.cs

This file was deleted.

Original file line number Diff line number Diff line change
Expand Up @@ -61,12 +61,12 @@ private static string ResolveManagedProfilerDirectory()
// load the originally referenced version
if (assemblyName.Name != null && assemblyName.Name.StartsWith("OpenTelemetry.AutoInstrumentation", StringComparison.OrdinalIgnoreCase) && File.Exists(path))
{
LoaderLogger.Debug("Loading {0} with Assembly.LoadFrom", path);
Logger.Debug("Loading {0} with Assembly.LoadFrom", path);
return Assembly.LoadFrom(path);
}
else if (File.Exists(path))
{
LoaderLogger.Debug("Loading {0} with DependencyLoadContext.LoadFromAssemblyPath", path);
Logger.Debug("Loading {0} with DependencyLoadContext.LoadFromAssemblyPath", path);
return DependencyLoadContext.LoadFromAssemblyPath(path); // Load unresolved framework and third-party dependencies into a custom Assembly Load Context
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -46,19 +46,19 @@ private static string ResolveManagedProfilerDirectory()
return null;
}

LoaderLogger.Debug("Requester [{0}] requested [{1}]", args?.RequestingAssembly?.FullName ?? "<null>", args?.Name ?? "<null>");
Logger.Debug("Requester [{0}] requested [{1}]", args?.RequestingAssembly?.FullName ?? "<null>", args?.Name ?? "<null>");
var path = Path.Combine(ManagedProfilerDirectory, $"{assemblyName}.dll");
if (File.Exists(path))
{
try
{
var loadedAssembly = Assembly.LoadFrom(path);
LoaderLogger.Debug("Assembly.LoadFrom(\"{0}\") succeeded={1}", path, loadedAssembly != null);
Logger.Debug<string, bool>("Assembly.LoadFrom(\"{0}\") succeeded={1}", path, loadedAssembly != null);
return loadedAssembly;
}
catch (Exception ex)
{
LoaderLogger.Debug("Assembly.LoadFrom(\"{0}\") Exception: {1}", path, ex);
Logger.Debug(ex, "Assembly.LoadFrom(\"{0}\") Exception: {1}", path, ex.Message);
}
}

Expand Down
11 changes: 7 additions & 4 deletions src/OpenTelemetry.AutoInstrumentation.Loader/Loader.cs
Original file line number Diff line number Diff line change
Expand Up @@ -15,6 +15,7 @@
// </copyright>

using System.Reflection;
using OpenTelemetry.AutoInstrumentation.Logging;

namespace OpenTelemetry.AutoInstrumentation.Loader;

Expand All @@ -25,6 +26,8 @@ internal partial class Loader
{
private static readonly string ManagedProfilerDirectory;

private static readonly IOtelLogger Logger = OtelLogging.GetLogger("Loader");

/// <summary>
/// Initializes static members of the <see cref="Loader"/> class.
/// This method also attempts to load the OpenTelemetry.AutoInstrumentation .NET assembly.
Expand All @@ -39,15 +42,15 @@ static Loader()
}
catch (Exception ex)
{
LoaderLogger.Log(ex, "Unable to register a callback to the CurrentDomain.AssemblyResolve event.");
Logger.Error(ex, "Unable to register a callback to the CurrentDomain.AssemblyResolve event.");
}

TryLoadManagedAssembly();
}

private static void TryLoadManagedAssembly()
{
LoaderLogger.Log("Managed Loader TryLoadManagedAssembly()");
Logger.Information("Managed Loader TryLoadManagedAssembly()");

try
{
Expand All @@ -73,7 +76,7 @@ private static void TryLoadManagedAssembly()
}
catch (Exception ex)
{
LoaderLogger.Log(ex, $"Error when loading managed assemblies. {ex.Message}");
Logger.Error(ex, "Error when loading managed assemblies. {0}", ex.Message);
throw;
}
}
Expand All @@ -86,7 +89,7 @@ private static void TryLoadManagedAssembly()
}
catch (Exception ex)
{
LoaderLogger.Log(ex, "Error while loading environment variable " + key);
Logger.Error(ex, "Error while loading environment variable {0}", key);
}

return null;
Expand Down
Loading