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

[otlp] Add log exception attributes under feature flag #4892

Merged
Show file tree
Hide file tree
Changes from 3 commits
Commits
Show all changes
21 commits
Select commit Hold shift + click to select a range
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
@@ -0,0 +1,45 @@
// <copyright file="ExperimentalFeatures.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>

#nullable enable

using Microsoft.Extensions.Configuration;
using OpenTelemetry.Internal;

namespace OpenTelemetry.Exporter.OpenTelemetryProtocol.Implementation;

internal sealed class ExperimentalFeatures
vishweshbankwar marked this conversation as resolved.
Show resolved Hide resolved
{
public const string EMITLOGEXCEPTIONATTRIBUTES = "OTEL_DOTNET_EMIT_EXCEPTION_LOG_ATTRIBUTES";
vishweshbankwar marked this conversation as resolved.
Show resolved Hide resolved

public ExperimentalFeatures()
: this(new ConfigurationBuilder().AddEnvironmentVariables().Build())
{
}

public ExperimentalFeatures(IConfiguration configuration)
{
if (configuration.TryGetBoolValue(EMITLOGEXCEPTIONATTRIBUTES, out var emitLogExceptionAttributes))
{
this.EmitLogExceptionAttributes = emitLogExceptionAttributes;
}
}

/// <summary>
/// Gets or sets a value indicating whether log exception attributes should be exported.
/// </summary>
public bool EmitLogExceptionAttributes { get; set; } = false;
}
Original file line number Diff line number Diff line change
Expand Up @@ -16,7 +16,9 @@

using System.Runtime.CompilerServices;
using Google.Protobuf;
using OpenTelemetry.Internal;
using OpenTelemetry.Logs;
using OpenTelemetry.Trace;
using OtlpCollector = OpenTelemetry.Proto.Collector.Logs.V1;
using OtlpCommon = OpenTelemetry.Proto.Common.V1;
using OtlpLogs = OpenTelemetry.Proto.Logs.V1;
Expand All @@ -30,7 +32,8 @@ internal static void AddBatch(
this OtlpCollector.ExportLogsServiceRequest request,
SdkLimitOptions sdkLimitOptions,
OtlpResource.Resource processResource,
in Batch<LogRecord> logRecordBatch)
in Batch<LogRecord> logRecordBatch,
ExperimentalFeatures experimentalFeatures)
vishweshbankwar marked this conversation as resolved.
Show resolved Hide resolved
{
var resourceLogs = new OtlpLogs.ResourceLogs
{
Expand All @@ -43,7 +46,7 @@ internal static void AddBatch(

foreach (var logRecord in logRecordBatch)
{
var otlpLogRecord = logRecord.ToOtlpLog(sdkLimitOptions);
var otlpLogRecord = logRecord.ToOtlpLog(sdkLimitOptions, experimentalFeatures);
if (otlpLogRecord != null)
{
scopeLogs.LogRecords.Add(otlpLogRecord);
Expand All @@ -52,7 +55,7 @@ internal static void AddBatch(
}

[MethodImpl(MethodImplOptions.AggressiveInlining)]
internal static OtlpLogs.LogRecord ToOtlpLog(this LogRecord logRecord, SdkLimitOptions sdkLimitOptions)
internal static OtlpLogs.LogRecord ToOtlpLog(this LogRecord logRecord, SdkLimitOptions sdkLimitOptions, ExperimentalFeatures experimentalFeatures)
{
OtlpLogs.LogRecord otlpLogRecord = null;

Expand Down Expand Up @@ -104,14 +107,14 @@ internal static OtlpLogs.LogRecord ToOtlpLog(this LogRecord logRecord, SdkLimitO
{
otlpLogRecord.AddStringAttribute(nameof(logRecord.EventId.Name), logRecord.EventId.Name, attributeValueLengthLimit, attributeCountLimit);
}
*/

if (logRecord.Exception != null)
if (experimentalFeatures.EmitLogExceptionAttributes && logRecord.Exception != null)
{
otlpLogRecord.AddStringAttribute(SemanticConventions.AttributeExceptionType, logRecord.Exception.GetType().Name, attributeValueLengthLimit, attributeCountLimit);
otlpLogRecord.AddStringAttribute(SemanticConventions.AttributeExceptionMessage, logRecord.Exception.Message, attributeValueLengthLimit, attributeCountLimit);
otlpLogRecord.AddStringAttribute(SemanticConventions.AttributeExceptionStacktrace, logRecord.Exception.ToInvariantString(), attributeValueLengthLimit, attributeCountLimit);
}
*/

bool bodyPopulatedFromFormattedMessage = false;
if (logRecord.FormattedMessage != null)
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -31,6 +31,7 @@ namespace OpenTelemetry.Exporter;
internal sealed class OtlpLogExporter : BaseExporter<LogRecord>
{
private readonly SdkLimitOptions sdkLimitOptions;
private readonly ExperimentalFeatures experimentalFeatures;
private readonly IExportClient<OtlpCollector.ExportLogsServiceRequest> exportClient;

private OtlpResource.Resource processResource;
Expand Down Expand Up @@ -59,6 +60,7 @@ internal OtlpLogExporter(
Debug.Assert(sdkLimitOptions != null, "sdkLimitOptions was null");

this.sdkLimitOptions = sdkLimitOptions;
this.experimentalFeatures = new();

// Each of the Otlp exporters: Traces, Metrics, and Logs set the same value for `OtlpKeyValueTransformer.LogUnsupportedAttributeType`
// and `ConfigurationExtensions.LogInvalidEnvironmentVariable` so it should be fine even if these exporters are used together.
Expand Down Expand Up @@ -94,7 +96,7 @@ public override ExportResult Export(in Batch<LogRecord> logRecordBatch)

try
{
request.AddBatch(this.sdkLimitOptions, this.ProcessResource, logRecordBatch);
request.AddBatch(this.sdkLimitOptions, this.ProcessResource, logRecordBatch, this.experimentalFeatures);

if (!this.exportClient.SendExportRequest(request))
{
Expand Down
Loading