Skip to content

Commit

Permalink
after merge adjustments
Browse files Browse the repository at this point in the history
  • Loading branch information
lachmatt committed Apr 11, 2022
1 parent beeb224 commit 3bd2baa
Show file tree
Hide file tree
Showing 4 changed files with 72 additions and 42 deletions.
4 changes: 3 additions & 1 deletion docs/config.md
Original file line number Diff line number Diff line change
Expand Up @@ -58,7 +58,9 @@ Default logs directory paths are:
- Windows: `%ProgramData%\OpenTelemetry .NET AutoInstrumentation\logs`
- Linux: `/var/log/opentelemetry/dotnet`

If there is a problem with creation of the default directory, 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) is used.
If there is a problem with the creation of the default directory,
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)
is used.

| Environment variable | Description | Default |
|-|-|-|
Expand Down
2 changes: 1 addition & 1 deletion src/OpenTelemetry.AutoInstrumentation/Logging/Logger.cs
Original file line number Diff line number Diff line change
Expand Up @@ -196,7 +196,7 @@ private void WriteImpl(LogLevel level, Exception exception, string messageTempla
try
{
var message =
$"[{DateTime.UtcNow}] [{level}] {messageTemplate} {Environment.NewLine}";
$"[{DateTime.UtcNow}] [{level}] {string.Format(messageTemplate, args)} {Environment.NewLine}";
_sink.Write(message);

if (exception != null)
Expand Down
8 changes: 4 additions & 4 deletions src/OpenTelemetry.AutoInstrumentation/Logging/OtelLogging.cs
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
// <copyright file="OtelLogging.cs" company="OpenTelemetry Authors">
// <copyright file="OtelLogging.cs" company="OpenTelemetry Authors">
// Copyright The OpenTelemetry Authors
//
// Licensed under the Apache License, Version 2.0 (the "License");
Expand Down Expand Up @@ -84,11 +84,11 @@ private static string GetLogDirectory()

if (logDirectory == null)
{
var nativeLogFile = Environment.GetEnvironmentVariable(ConfigurationKeys.LogPath);
var envVarValue = Environment.GetEnvironmentVariable(ConfigurationKeys.LogPath);

if (!string.IsNullOrEmpty(nativeLogFile))
if (!string.IsNullOrEmpty(envVarValue))
{
logDirectory = Path.GetDirectoryName(nativeLogFile);
logDirectory = Path.GetDirectoryName(envVarValue);
}
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -16,57 +16,75 @@

// Source originated from https://github.com/open-telemetry/opentelemetry-dotnet/blob/23609730ddd73c860553de847e67c9b2226cff94/test/OpenTelemetry.Tests/Internal/SelfDiagnosticsEventListenerTest.cs

using System;
using System.Collections.Generic;
using System.Diagnostics.Tracing;
using System.IO;
using FluentAssertions;
using OpenTelemetry.AutoInstrumentation.Diagnostics;
using OpenTelemetry.AutoInstrumentation.Logging;
using Xunit;

namespace OpenTelemetry.AutoInstrumentation.Tests.Diagnostics;

public class SdkSelfDiagnosticsEventListenerTests
{
[Fact]
public void EventSourceSetup_LowerSeverity()
{
var originalConsoleOut = Console.Out; // preserve the original stream
using var writer = new StringWriter();
var listener = new SdkSelfDiagnosticsEventListener(EventLevel.Error);

// Emitting a Verbose event. Or any EventSource event with lower severity than Error.
AspNetTelemetryEventSource.Log.ActivityRestored("123");
OpenTelemetrySdkEventSource.Log.ActivityStarted("Activity started", "1");

// Prepare the output for assertion
writer.Flush();
var outputString = writer.GetStringBuilder().ToString();
outputString.Should().NotContain("EventSource=OpenTelemetry-Instrumentation-AspNet-Telemetry, Message=Activity restored, Id='123'");
outputString.Should().NotContain("EventSource=OpenTelemetry-Sdk, Message=Activity started.");

// Cleanup
Console.SetOut(originalConsoleOut);
// get default sink using reflection
var defaultLogger = OtelLogging.GetLogger();
var sinkField = defaultLogger.GetType().GetField("_sink", System.Reflection.BindingFlags.NonPublic | System.Reflection.BindingFlags.Instance);
var defaultSink = (ISink)sinkField.GetValue(defaultLogger);

try
{
var testSink = new TestSink();
var listener = new SdkSelfDiagnosticsEventListener(EventLevel.Error);

// override default logger's sink with test sink
sinkField.SetValue(defaultLogger, testSink);

// Emitting a Verbose event. Or any EventSource event with lower severity than Error.
AspNetTelemetryEventSource.Log.ActivityRestored("123");
OpenTelemetrySdkEventSource.Log.ActivityStarted("Activity started", "1");

testSink.Messages.Should().BeEmpty("events with lower severity than error should not be written.");
}
finally
{
// restore default sink
sinkField.SetValue(defaultLogger, defaultSink);
}
}

[Fact]
public void EventSourceSetup_HigherSeverity()
{
// Redirect the ConsoleLogger
var originalConsoleOut = Console.Out;
using var writer = new StringWriter();
Console.SetOut(writer);
var listener = new SdkSelfDiagnosticsEventListener(EventLevel.Verbose);

// Emitting a Verbose event. Or any EventSource event with lower severity than Error.
AspNetTelemetryEventSource.Log.ActivityRestored("123");
OpenTelemetrySdkEventSource.Log.ActivityStarted("Activity started", "1");

// Prepare the output for assertion
writer.Flush();
var outputString = writer.GetStringBuilder().ToString();
outputString.Should().Contain("EventSource=OpenTelemetry-Instrumentation-AspNet-Telemetry, Message=Activity restored, Id='123'");
outputString.Should().Contain("EventSource=OpenTelemetry-Sdk, Message=Activity started.");

// Cleanup
Console.SetOut(originalConsoleOut);
// get default sink using reflection
var defaultLogger = OtelLogging.GetLogger();
var sinkField = defaultLogger.GetType().GetField("_sink", System.Reflection.BindingFlags.NonPublic | System.Reflection.BindingFlags.Instance);
var defaultSink = (ISink)sinkField.GetValue(defaultLogger);

try
{
var testSink = new TestSink();

var listener = new SdkSelfDiagnosticsEventListener(EventLevel.Verbose);

// override default logger's sink with test sink
sinkField.SetValue(defaultLogger, testSink);

// Emitting a Verbose event. Or any EventSource event with lower severity than Error.
AspNetTelemetryEventSource.Log.ActivityRestored("123");
OpenTelemetrySdkEventSource.Log.ActivityStarted("Activity started", "1");

testSink.Messages.Should().Contain(msg => msg.Contains("EventSource=OpenTelemetry-Instrumentation-AspNet-Telemetry, Message=Activity restored, Id='123'"));
testSink.Messages.Should().Contain(msg => msg.Contains("EventSource=OpenTelemetry-Sdk, Message=Activity started."));
}
finally
{
// restore default sink
sinkField.SetValue(defaultLogger, defaultSink);
}
}

[EventSource(Name = "OpenTelemetry-Instrumentation-AspNet-Telemetry", Guid = "1de158cc-f7ce-4293-bd19-2358c93c8186")]
Expand All @@ -92,4 +110,14 @@ public void ActivityStarted(string operationName, string id)
this.WriteEvent(24, operationName, id);
}
}

private class TestSink : ISink
{
public IList<string> Messages { get; } = new List<string>();

public void Write(string message)
{
Messages.Add(message);
}
}
}

0 comments on commit 3bd2baa

Please sign in to comment.