-
Notifications
You must be signed in to change notification settings - Fork 891
Expand file tree
/
Copy pathLogRecordData.cs
More file actions
157 lines (142 loc) · 5.31 KB
/
LogRecordData.cs
File metadata and controls
157 lines (142 loc) · 5.31 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
// Copyright The OpenTelemetry Authors
// SPDX-License-Identifier: Apache-2.0
using System.Diagnostics;
#if EXPOSE_EXPERIMENTAL_FEATURES
using System.Diagnostics.CodeAnalysis;
using OpenTelemetry.Internal;
#endif
namespace OpenTelemetry.Logs;
#if EXPOSE_EXPERIMENTAL_FEATURES
/// <summary>
/// Stores details about a log message.
/// </summary>
/// <remarks><inheritdoc cref="Logger" path="/remarks"/></remarks>
[Experimental(DiagnosticDefinitions.LogsBridgeExperimentalApi, UrlFormat = DiagnosticDefinitions.ExperimentalApiUrlFormat)]
public
#else
/// <summary>
/// Stores details about a log message.
/// </summary>
internal
#endif
#pragma warning disable CA1815 // Override equals and operator equals on value types
struct LogRecordData
#pragma warning restore CA1815 // Override equals and operator equals on value types
{
internal DateTime TimestampBacking = DateTime.MinValue;
/// <summary>
/// Initializes a new instance of the <see cref="LogRecordData"/> struct.
/// </summary>
/// <remarks>
/// Notes:
/// <list type="bullet">
/// <item>The <see cref="Timestamp"/> property is initialized to <see
/// cref="DateTime.MinValue"/> automatically.</item>
/// <item>The <see cref="TraceId"/>, <see cref="SpanId"/>, and <see
/// cref="TraceFlags"/> properties will be set using the <see
/// cref="Activity.Current"/> instance.</item>
/// </list>
/// </remarks>
public LogRecordData()
: this(Activity.Current?.Context ?? default)
{
}
/// <summary>
/// Initializes a new instance of the <see cref="LogRecordData"/> struct.
/// </summary>
/// <remarks>
/// Note: The <see cref="Timestamp"/> property is initialized to <see
/// cref="DateTime.MinValue"/> automatically, indicating the timestamp is
/// not set. Set <see cref="Timestamp"/> explicitly to provide the time the
/// event occurred.
/// </remarks>
/// <param name="activity">Optional <see cref="Activity"/> used to populate
/// trace context properties (<see cref="TraceId"/>, <see cref="SpanId"/>,
/// and <see cref="TraceFlags"/>).</param>
public LogRecordData(Activity? activity)
: this(activity?.Context ?? default)
{
}
/// <summary>
/// Initializes a new instance of the <see cref="LogRecordData"/> struct.
/// </summary>
/// <remarks>
/// Note: The <see cref="Timestamp"/> property is initialized to <see
/// cref="DateTime.MinValue"/> automatically, indicating the timestamp is
/// not set. Set <see cref="Timestamp"/> explicitly to provide the time the
/// event occurred.
/// </remarks>
/// <param name="activityContext"><see cref="ActivityContext"/> used to
/// populate trace context properties (<see cref="TraceId"/>, <see
/// cref="SpanId"/>, and <see cref="TraceFlags"/>).</param>
public LogRecordData(in ActivityContext activityContext)
{
this.TraceId = activityContext.TraceId;
this.SpanId = activityContext.SpanId;
this.TraceFlags = activityContext.TraceFlags;
}
/// <summary>
/// Gets or sets the log timestamp.
/// </summary>
/// <remarks>
/// Notes:
/// <list type="bullet">
/// <item>The default value is <see cref="DateTime.MinValue"/>, which is
/// treated as "not set" per the OpenTelemetry specification. When
/// exported via OTLP this maps to <c>time_unix_nano = 0</c> (unknown or
/// missing timestamp).</item>
/// <item>If <see cref="Timestamp"/> is set to a value with <see
/// cref="DateTimeKind.Local"/> it will be automatically converted to UTC
/// using <see cref="DateTime.ToUniversalTime"/>.</item>
/// </list>
/// </remarks>
public DateTime Timestamp
{
readonly get => this.TimestampBacking;
set => this.TimestampBacking = value.Kind == DateTimeKind.Local ? value.ToUniversalTime() : value;
}
/// <summary>
/// Gets or sets the log <see cref="ActivityTraceId"/>.
/// </summary>
public ActivityTraceId TraceId { get; set; }
/// <summary>
/// Gets or sets the log <see cref="ActivitySpanId"/>.
/// </summary>
public ActivitySpanId SpanId { get; set; }
/// <summary>
/// Gets or sets the log <see cref="ActivityTraceFlags"/>.
/// </summary>
public ActivityTraceFlags TraceFlags { get; set; }
/// <summary>
/// Gets or sets the original string representation of the severity as it is
/// known at the source.
/// </summary>
public string? SeverityText { get; set; } = null;
/// <summary>
/// Gets or sets the log severity.
/// </summary>
public LogRecordSeverity? Severity { get; set; } = null;
/// <summary>
/// Gets or sets the log body.
/// </summary>
public string? Body { get; set; } = null;
/// <summary>
/// Gets or sets the name of the event associated with the log.
/// </summary>
public string? EventName { get; set; } = null;
internal static void SetActivityContext(ref LogRecordData data, Activity? activity)
{
if (activity != null)
{
data.TraceId = activity.TraceId;
data.SpanId = activity.SpanId;
data.TraceFlags = activity.ActivityTraceFlags;
}
else
{
data.TraceId = default;
data.SpanId = default;
data.TraceFlags = ActivityTraceFlags.None;
}
}
}