-
Notifications
You must be signed in to change notification settings - Fork 772
/
LoggerProviderSdk.cs
237 lines (193 loc) · 7.32 KB
/
LoggerProviderSdk.cs
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
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
// Copyright The OpenTelemetry Authors
// SPDX-License-Identifier: Apache-2.0
using System.Diagnostics;
#if NETSTANDARD2_1_OR_GREATER || NET6_0_OR_GREATER
using System.Diagnostics.CodeAnalysis;
#endif
using System.Text;
using Microsoft.Extensions.DependencyInjection;
using OpenTelemetry.Internal;
using OpenTelemetry.Resources;
namespace OpenTelemetry.Logs;
/// <summary>
/// SDK <see cref="LoggerProvider"/> implementation.
/// </summary>
internal sealed class LoggerProviderSdk : LoggerProvider
{
internal readonly IServiceProvider ServiceProvider;
internal readonly IDisposable? OwnedServiceProvider;
internal bool Disposed;
internal int ShutdownCount;
private readonly List<object> instrumentations = new();
private ILogRecordPool? threadStaticPool = LogRecordThreadStaticPool.Instance;
public LoggerProviderSdk(
IServiceProvider serviceProvider,
bool ownsServiceProvider)
{
Debug.Assert(serviceProvider != null, "serviceProvider was null");
var state = serviceProvider!.GetRequiredService<LoggerProviderBuilderSdk>();
state.RegisterProvider(this);
this.ServiceProvider = serviceProvider!;
if (ownsServiceProvider)
{
this.OwnedServiceProvider = serviceProvider as IDisposable;
Debug.Assert(this.OwnedServiceProvider != null, "ownedServiceProvider was null");
}
OpenTelemetrySdkEventSource.Log.LoggerProviderSdkEvent("Building LoggerProvider.");
var configureProviderBuilders = serviceProvider!.GetServices<IConfigureLoggerProviderBuilder>();
foreach (var configureProviderBuilder in configureProviderBuilders)
{
configureProviderBuilder.ConfigureBuilder(serviceProvider!, state);
}
var resourceBuilder = state.ResourceBuilder ?? ResourceBuilder.CreateDefault();
resourceBuilder.ServiceProvider = serviceProvider;
this.Resource = resourceBuilder.Build();
foreach (var processor in state.Processors)
{
this.AddProcessor(processor);
}
StringBuilder instrumentationFactoriesAdded = new StringBuilder();
foreach (var instrumentation in state.Instrumentation)
{
if (instrumentation.Instance is not null)
{
this.instrumentations.Add(instrumentation.Instance);
}
instrumentationFactoriesAdded.Append(instrumentation.Name);
instrumentationFactoriesAdded.Append(';');
}
if (instrumentationFactoriesAdded.Length != 0)
{
instrumentationFactoriesAdded.Remove(instrumentationFactoriesAdded.Length - 1, 1);
OpenTelemetrySdkEventSource.Log.LoggerProviderSdkEvent($"Instrumentations added = \"{instrumentationFactoriesAdded}\".");
}
OpenTelemetrySdkEventSource.Log.LoggerProviderSdkEvent("LoggerProviderSdk built successfully.");
}
public Resource Resource { get; }
public List<object> Instrumentations => this.instrumentations;
public BaseProcessor<LogRecord>? Processor { get; private set; }
public ILogRecordPool LogRecordPool => this.threadStaticPool ?? LogRecordSharedPool.Current;
public void AddProcessor(BaseProcessor<LogRecord> processor)
{
Guard.ThrowIfNull(processor);
processor.SetParentProvider(this);
if (this.threadStaticPool != null && this.ContainsBatchProcessor(processor))
{
OpenTelemetrySdkEventSource.Log.LoggerProviderSdkEvent("Using shared thread pool.");
this.threadStaticPool = null;
}
StringBuilder processorAdded = new StringBuilder();
if (this.Processor == null)
{
processorAdded.Append("Setting processor to '");
processorAdded.Append(processor);
processorAdded.Append('\'');
this.Processor = processor;
}
else if (this.Processor is CompositeProcessor<LogRecord> compositeProcessor)
{
processorAdded.Append("Adding processor '");
processorAdded.Append(processor);
processorAdded.Append("' to composite processor");
compositeProcessor.AddProcessor(processor);
}
else
{
processorAdded.Append("Creating new composite processor and adding new processor '");
processorAdded.Append(processor);
processorAdded.Append('\'');
var newCompositeProcessor = new CompositeProcessor<LogRecord>(new[]
{
this.Processor,
});
newCompositeProcessor.SetParentProvider(this);
newCompositeProcessor.AddProcessor(processor);
this.Processor = newCompositeProcessor;
}
OpenTelemetrySdkEventSource.Log.LoggerProviderSdkEvent($"Completed adding processor = \"{processorAdded}\".");
}
public bool ForceFlush(int timeoutMilliseconds = Timeout.Infinite)
{
try
{
return this.Processor?.ForceFlush(timeoutMilliseconds) ?? true;
}
catch (Exception ex)
{
OpenTelemetrySdkEventSource.Log.LoggerProviderException(nameof(this.ForceFlush), ex);
return false;
}
}
public bool Shutdown(int timeoutMilliseconds)
{
if (Interlocked.Increment(ref this.ShutdownCount) > 1)
{
return false; // shutdown already called
}
try
{
return this.Processor?.Shutdown(timeoutMilliseconds) ?? true;
}
catch (Exception ex)
{
OpenTelemetrySdkEventSource.Log.LoggerProviderException(nameof(this.Shutdown), ex);
return false;
}
}
public bool ContainsBatchProcessor(BaseProcessor<LogRecord> processor)
{
if (processor is BatchExportProcessor<LogRecord>)
{
return true;
}
else if (processor is CompositeProcessor<LogRecord> compositeProcessor)
{
var current = compositeProcessor.Head;
while (current != null)
{
if (this.ContainsBatchProcessor(current.Value))
{
return true;
}
current = current.Next;
}
}
return false;
}
/// <inheritdoc />
protected override bool TryCreateLogger(
string? name,
#if NETSTANDARD2_1_OR_GREATER || NET6_0_OR_GREATER
[NotNullWhen(true)]
#endif
out Logger? logger)
{
logger = new LoggerSdk(this, name);
return true;
}
/// <inheritdoc/>
protected override void Dispose(bool disposing)
{
if (!this.Disposed)
{
if (disposing)
{
if (this.instrumentations != null)
{
foreach (var item in this.instrumentations)
{
(item as IDisposable)?.Dispose();
}
this.instrumentations.Clear();
}
// Wait for up to 5 seconds grace period
this.Processor?.Shutdown(5000);
this.Processor?.Dispose();
this.OwnedServiceProvider?.Dispose();
}
this.Disposed = true;
OpenTelemetrySdkEventSource.Log.ProviderDisposed(nameof(LoggerProviderSdk));
}
base.Dispose(disposing);
}
}