Skip to content

Commit ab278bb

Browse files
cijothomasrajkumar-rangarajCodeBlanch
authored
[GenevaExporter] Add ability to export Log Exception using StackTrace (#2422)
Co-authored-by: Rajkumar Rangaraj <[email protected]> Co-authored-by: Mikel Blanchard <[email protected]>
1 parent f7987b5 commit ab278bb

File tree

5 files changed

+53
-10
lines changed

5 files changed

+53
-10
lines changed
Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1 @@
1+
OpenTelemetry.Exporter.Geneva.ExceptionStackExportMode.ExportAsStackTraceString = 2 -> OpenTelemetry.Exporter.Geneva.ExceptionStackExportMode

src/OpenTelemetry.Exporter.Geneva/CHANGELOG.md

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -2,6 +2,12 @@
22

33
## Unreleased
44

5+
* Added support for exporting exception stack traces using
6+
`Exception.StackTrace`. This can be enabled via the
7+
`ExceptionStackExportMode.ExportAsStackTraceString` enum. Applicable only to
8+
the LogExporter.
9+
([#2422](https://github.com/open-telemetry/opentelemetry-dotnet-contrib/pull/2422))
10+
511
## 1.10.0
612

713
Released 2024-Nov-18

src/OpenTelemetry.Exporter.Geneva/ExceptionStackExportMode.cs

Lines changed: 20 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -4,19 +4,36 @@
44
namespace OpenTelemetry.Exporter.Geneva;
55

66
/// <summary>
7-
/// Contains the exception stack trace export mode defintions.
7+
/// Defines modes for exporting exception stack traces. Currently applicable only to the Logs signal.
88
/// </summary>
99
public enum ExceptionStackExportMode
1010
{
1111
/// <summary>
12-
/// Exception stack traces are dropped.
12+
/// Exception stack traces are dropped and not exported.
1313
/// </summary>
1414
Drop,
1515

1616
/// <summary>
17-
/// Exception stack traces are exported as strings.
17+
/// Exports exception stack traces as a string using the <c>ToString()</c> implementation of the exception.
18+
/// The output is formatted in a culture-agnostic manner and is primarily designed for human readability.
19+
/// See <see href="https://learn.microsoft.com/dotnet/api/system.exception.tostring"/>.
1820
/// </summary>
21+
/// <remarks>
22+
/// Typically, <c>ToString()</c> includes information about inner exceptions and additional details,
23+
/// such as the exception message. However, this behavior is not guaranteed, as custom exceptions
24+
/// can override <c>ToString()</c> to return arbitrary content.
25+
/// </remarks>
1926
ExportAsString,
2027

28+
/// <summary>
29+
/// Exports exception stack traces as a string using the <c>StackTrace</c> property of the exception.
30+
/// See <see href="https://learn.microsoft.com/dotnet/api/system.exception.stacktrace"/>.
31+
/// </summary>
32+
/// <remarks>
33+
/// This represents the raw stack trace and does not include inner exception details.
34+
/// Note that the <c>StackTrace</c> property can also be overridden in custom exception implementations.
35+
/// </remarks>
36+
ExportAsStackTraceString,
37+
2138
// ExportAsArrayOfStacks - future if stacks can be exported in more structured way
2239
}

src/OpenTelemetry.Exporter.Geneva/Internal/MsgPack/MsgPackLogExporter.cs

Lines changed: 17 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -419,20 +419,30 @@ internal ArraySegment<byte> SerializeLogRecord(LogRecord logRecord)
419419
cursor = MessagePackSerializer.SerializeUnicodeString(buffer, cursor, logRecord.Exception.Message);
420420
cntFields += 1;
421421

422+
// The current approach relies on the existing trim
423+
// capabilities which trims string in excess of STRING_SIZE_LIMIT_CHAR_COUNT
424+
// TODO: Revisit this:
425+
// 1. Trim it off based on how much more bytes are available
426+
// before running out of limit instead of STRING_SIZE_LIMIT_CHAR_COUNT.
427+
// 2. Trim smarter, by trimming the middle of stack, an
428+
// keep top and bottom.
422429
if (this.exportExceptionStack == ExceptionStackExportMode.ExportAsString)
423430
{
424-
// The current approach relies on the existing trim
425-
// capabilities which trims string in excess of STRING_SIZE_LIMIT_CHAR_COUNT
426-
// TODO: Revisit this:
427-
// 1. Trim it off based on how much more bytes are available
428-
// before running out of limit instead of STRING_SIZE_LIMIT_CHAR_COUNT.
429-
// 2. Trim smarter, by trimming the middle of stack, an
430-
// keep top and bottom.
431431
var exceptionStack = logRecord.Exception.ToInvariantString();
432432
cursor = MessagePackSerializer.SerializeAsciiString(buffer, cursor, "env_ex_stack");
433433
cursor = MessagePackSerializer.SerializeUnicodeString(buffer, cursor, exceptionStack);
434434
cntFields += 1;
435435
}
436+
else if (this.exportExceptionStack == ExceptionStackExportMode.ExportAsStackTraceString)
437+
{
438+
var exceptionStack = logRecord.Exception.StackTrace;
439+
if (exceptionStack != null)
440+
{
441+
cursor = MessagePackSerializer.SerializeAsciiString(buffer, cursor, "env_ex_stack");
442+
cursor = MessagePackSerializer.SerializeUnicodeString(buffer, cursor, exceptionStack);
443+
cntFields += 1;
444+
}
445+
}
436446
}
437447

438448
MessagePackSerializer.WriteUInt16(buffer, idxMapSizePatch, cntFields);

src/OpenTelemetry.Exporter.Geneva/Internal/Tld/TldLogExporter.cs

Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -260,6 +260,15 @@ internal EventBuilder SerializeLogRecord(LogRecord logRecord)
260260
eb.AddCountedAnsiString("ext_ex_stack", exceptionStack, Encoding.UTF8, 0, Math.Min(exceptionStack.Length, StringLengthLimit));
261261
partAFieldsCount++;
262262
}
263+
else if (this.exceptionStackExportMode == ExceptionStackExportMode.ExportAsStackTraceString)
264+
{
265+
var stackTrace = logRecord.Exception.StackTrace;
266+
if (stackTrace != null)
267+
{
268+
eb.AddCountedAnsiString("ext_ex_stack", stackTrace, Encoding.UTF8, 0, Math.Min(stackTrace.Length, StringLengthLimit));
269+
partAFieldsCount++;
270+
}
271+
}
263272
}
264273

265274
eb.SetStructFieldCount(partAFieldsCountPatch, partAFieldsCount);

0 commit comments

Comments
 (0)