Skip to content

Commit

Permalink
Merge branch 'main' into instrumentation-sqlclient-drop-db.statement_…
Browse files Browse the repository at this point in the history
…type
  • Loading branch information
vishweshbankwar authored Feb 5, 2024
2 parents 354e9b8 + ef977c1 commit 1fb9863
Show file tree
Hide file tree
Showing 3 changed files with 49 additions and 3 deletions.
5 changes: 5 additions & 0 deletions src/OpenTelemetry.Exporter.OpenTelemetryProtocol/CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -21,6 +21,11 @@
experimental Logs Bridge API.
([#5268](https://github.com/open-telemetry/opentelemetry-dotnet/pull/5268))

* Updated `OtlpLogExporter` to set instrumentation scope name on the data model
from `LogRecord.Logger.Name` if `LogRecord.CategoryName` is `null`. This is
typically the case when using the experimental Logs Bridge API.
([#5300](https://github.com/open-telemetry/opentelemetry-dotnet/pull/5300))

## 1.7.0

Released 2023-Dec-08
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -18,6 +18,8 @@ internal sealed class OtlpLogRecordTransformer
{
internal static readonly ConcurrentBag<OtlpLogs.ScopeLogs> LogListPool = new();

private const string DefaultScopeName = "";

private readonly SdkLimitOptions sdkLimitOptions;
private readonly ExperimentalOptions experimentalOptions;

Expand Down Expand Up @@ -47,10 +49,11 @@ internal OtlpCollector.ExportLogsServiceRequest BuildExportRequest(
var otlpLogRecord = this.ToOtlpLog(logRecord);
if (otlpLogRecord != null)
{
if (!logsByCategory.TryGetValue(logRecord.CategoryName, out var scopeLogs))
var scopeName = logRecord.CategoryName ?? logRecord.Logger?.Name ?? DefaultScopeName;
if (!logsByCategory.TryGetValue(scopeName, out var scopeLogs))
{
scopeLogs = this.GetLogListFromPool(logRecord.CategoryName);
logsByCategory.Add(logRecord.CategoryName, scopeLogs);
scopeLogs = this.GetLogListFromPool(scopeName);
logsByCategory.Add(scopeName, scopeLogs);
resourceLogs.ScopeLogs.Add(scopeLogs);
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -1469,6 +1469,44 @@ public void VerifyEnvironmentVariablesTakenFromIConfigurationWhenUsingLoggingBui
});
}

[Theory]
[InlineData("my_instrumentation_scope_name", "my_instrumentation_scope_name")]
[InlineData(null, "")]
public void LogRecordLoggerNameIsExportedWhenUsingBridgeApi(string loggerName, string expectedScopeName)
{
LogRecordAttributeList attributes = default;
attributes.Add("name", "tomato");
attributes.Add("price", 2.99);
attributes.Add("{OriginalFormat}", "Hello from {name} {price}.");

var logRecords = new List<LogRecord>();

using (var loggerProvider = Sdk.CreateLoggerProviderBuilder()
.AddInMemoryExporter(logRecords)
.Build())
{
var logger = loggerProvider.GetLogger(loggerName);

logger.EmitLog(new LogRecordData());
}

Assert.Single(logRecords);

var otlpLogRecordTransformer = new OtlpLogRecordTransformer(DefaultSdkLimitOptions, new());

var batch = new Batch<LogRecord>(new[] { logRecords[0] }, 1);

var request = otlpLogRecordTransformer.BuildExportRequest(
new Proto.Resource.V1.Resource(),
batch);

Assert.NotNull(request);
Assert.Single(request.ResourceLogs);
Assert.Single(request.ResourceLogs[0].ScopeLogs);

Assert.Equal(expectedScopeName, request.ResourceLogs[0].ScopeLogs[0].Scope?.Name);
}

private static void RunVerifyEnvironmentVariablesTakenFromIConfigurationTest(
string optionsName,
Func<Action<IServiceCollection>, (IDisposable Container, ILoggerFactory LoggerFactory)> createLoggerFactoryFunc)
Expand Down

0 comments on commit 1fb9863

Please sign in to comment.