Skip to content

Commit 8c1e638

Browse files
authored
[OpenTelemetry] enable analysis - part 1 (open-telemetry#6263)
1 parent 9c9d858 commit 8c1e638

31 files changed

+172
-141
lines changed

src/OpenTelemetry/BaseExportProcessor.cs

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -35,7 +35,9 @@ public abstract class BaseExportProcessor<T> : BaseProcessor<T>
3535
/// <summary>
3636
/// Gets the exporter used by the processor.
3737
/// </summary>
38+
#pragma warning disable CA1051 // Do not declare visible instance fields
3839
protected readonly BaseExporter<T> exporter;
40+
#pragma warning restore CA1051 // Do not declare visible instance fields
3941

4042
private readonly string friendlyTypeName;
4143
private bool disposed;

src/OpenTelemetry/BaseProcessor.cs

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -17,7 +17,9 @@ public abstract class BaseProcessor<T> : IDisposable
1717
/// <summary>
1818
/// Initializes a new instance of the <see cref="BaseProcessor{T}"/> class.
1919
/// </summary>
20+
#pragma warning disable CA1012 // Abstract types should not have public constructors
2021
public BaseProcessor()
22+
#pragma warning restore CA1012 // Abstract types should not have public constructors
2123
{
2224
this.typeName = this.GetType().Name;
2325
}

src/OpenTelemetry/CompositeProcessor.cs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -27,7 +27,7 @@ public CompositeProcessor(IEnumerable<BaseProcessor<T>> processors)
2727
using var iter = processors.GetEnumerator();
2828
if (!iter.MoveNext())
2929
{
30-
throw new ArgumentException($"'{iter}' is null or empty", nameof(iter));
30+
throw new ArgumentException($"'{iter}' is null or empty", nameof(processors));
3131
}
3232

3333
this.Head = new DoublyLinkedListNode(iter.Current);

src/OpenTelemetry/Internal/SelfDiagnosticsConfigRefresher.cs

Lines changed: 5 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -139,7 +139,7 @@ private void UpdateMemoryMappedFileFromConfiguration()
139139
if (this.configParser.TryGetConfiguration(out string? newLogDirectory, out int fileSizeInKB, out EventLevel newEventLevel))
140140
{
141141
int newFileSize = fileSizeInKB * 1024;
142-
if (!newLogDirectory.Equals(this.logDirectory) || this.logFileSize != newFileSize)
142+
if (!newLogDirectory.Equals(this.logDirectory, StringComparison.Ordinal) || this.logFileSize != newFileSize)
143143
{
144144
this.CloseLogFile();
145145
this.OpenLogFile(newLogDirectory, newFileSize);
@@ -194,7 +194,11 @@ private void OpenLogFile(string newLogDirectory, int newFileSize)
194194
{
195195
Directory.CreateDirectory(newLogDirectory);
196196
var fileName = Path.GetFileName(Process.GetCurrentProcess().MainModule?.FileName ?? "OpenTelemetrySdk") + "."
197+
#if NET
198+
+ Environment.ProcessId + ".log";
199+
#else
197200
+ Process.GetCurrentProcess().Id + ".log";
201+
#endif
198202
var filePath = Path.Combine(newLogDirectory, fileName);
199203

200204
// Because the API [MemoryMappedFile.CreateFromFile][1](the string version) behaves differently on

src/OpenTelemetry/Internal/SelfDiagnosticsEventListener.cs

Lines changed: 58 additions & 58 deletions
Original file line numberDiff line numberDiff line change
@@ -20,9 +20,9 @@ internal sealed class SelfDiagnosticsEventListener : EventListener
2020
private readonly EventLevel logLevel;
2121
private readonly SelfDiagnosticsConfigRefresher configRefresher;
2222
private readonly ThreadLocal<byte[]?> writeBuffer = new(() => null);
23-
private readonly List<EventSource>? eventSourcesBeforeConstructor = new();
23+
private readonly List<EventSource>? eventSourcesBeforeConstructor = [];
2424

25-
private bool disposedValue = false;
25+
private bool disposedValue;
2626

2727
public SelfDiagnosticsEventListener(EventLevel logLevel, SelfDiagnosticsConfigRefresher configRefresher)
2828
{
@@ -111,61 +111,6 @@ internal static int EncodeInBuffer(string? str, bool isParameter, byte[] buffer,
111111
return position;
112112
}
113113

114-
internal void WriteEvent(string? eventMessage, ReadOnlyCollection<object?>? payload)
115-
{
116-
try
117-
{
118-
var buffer = this.writeBuffer.Value;
119-
if (buffer == null)
120-
{
121-
buffer = new byte[BUFFERSIZE];
122-
this.writeBuffer.Value = buffer;
123-
}
124-
125-
var pos = this.DateTimeGetBytes(DateTime.UtcNow, buffer, 0);
126-
buffer[pos++] = (byte)':';
127-
pos = EncodeInBuffer(eventMessage, false, buffer, pos);
128-
if (payload != null)
129-
{
130-
// Not using foreach because it can cause allocations
131-
for (int i = 0; i < payload.Count; ++i)
132-
{
133-
object? obj = payload[i];
134-
if (obj != null)
135-
{
136-
pos = EncodeInBuffer(obj.ToString() ?? "null", true, buffer, pos);
137-
}
138-
else
139-
{
140-
pos = EncodeInBuffer("null", true, buffer, pos);
141-
}
142-
}
143-
}
144-
145-
buffer[pos++] = (byte)'\n';
146-
int byteCount = pos - 0;
147-
if (this.configRefresher.TryGetLogStream(byteCount, out Stream? stream, out int availableByteCount))
148-
{
149-
if (availableByteCount >= byteCount)
150-
{
151-
stream.Write(buffer, 0, byteCount);
152-
}
153-
else
154-
{
155-
stream.Write(buffer, 0, availableByteCount);
156-
stream.Seek(0, SeekOrigin.Begin);
157-
stream.Write(buffer, availableByteCount, byteCount - availableByteCount);
158-
}
159-
}
160-
}
161-
catch (Exception)
162-
{
163-
// Fail to allocate memory for buffer, or
164-
// A concurrent condition: memory mapped file is disposed in other thread after TryGetLogStream() finishes.
165-
// In this case, silently fail.
166-
}
167-
}
168-
169114
/// <summary>
170115
/// Write the <c>datetime</c> formatted string into <c>bytes</c> byte-array starting at <c>byteIndex</c> position.
171116
/// <para>
@@ -188,7 +133,7 @@ internal void WriteEvent(string? eventMessage, ReadOnlyCollection<object?>? payl
188133
/// <param name="bytes">Array of bytes to write.</param>
189134
/// <param name="byteIndex">Starting index into bytes array.</param>
190135
/// <returns>The number of bytes written.</returns>
191-
internal int DateTimeGetBytes(DateTime datetime, byte[] bytes, int byteIndex)
136+
internal static int DateTimeGetBytes(DateTime datetime, byte[] bytes, int byteIndex)
192137
{
193138
int num;
194139
int pos = byteIndex;
@@ -271,6 +216,61 @@ internal int DateTimeGetBytes(DateTime datetime, byte[] bytes, int byteIndex)
271216
return pos - byteIndex;
272217
}
273218

219+
internal void WriteEvent(string? eventMessage, ReadOnlyCollection<object?>? payload)
220+
{
221+
try
222+
{
223+
var buffer = this.writeBuffer.Value;
224+
if (buffer == null)
225+
{
226+
buffer = new byte[BUFFERSIZE];
227+
this.writeBuffer.Value = buffer;
228+
}
229+
230+
var pos = DateTimeGetBytes(DateTime.UtcNow, buffer, 0);
231+
buffer[pos++] = (byte)':';
232+
pos = EncodeInBuffer(eventMessage, false, buffer, pos);
233+
if (payload != null)
234+
{
235+
// Not using foreach because it can cause allocations
236+
for (int i = 0; i < payload.Count; ++i)
237+
{
238+
object? obj = payload[i];
239+
if (obj != null)
240+
{
241+
pos = EncodeInBuffer(obj.ToString() ?? "null", true, buffer, pos);
242+
}
243+
else
244+
{
245+
pos = EncodeInBuffer("null", true, buffer, pos);
246+
}
247+
}
248+
}
249+
250+
buffer[pos++] = (byte)'\n';
251+
int byteCount = pos - 0;
252+
if (this.configRefresher.TryGetLogStream(byteCount, out Stream? stream, out int availableByteCount))
253+
{
254+
if (availableByteCount >= byteCount)
255+
{
256+
stream.Write(buffer, 0, byteCount);
257+
}
258+
else
259+
{
260+
stream.Write(buffer, 0, availableByteCount);
261+
stream.Seek(0, SeekOrigin.Begin);
262+
stream.Write(buffer, availableByteCount, byteCount - availableByteCount);
263+
}
264+
}
265+
}
266+
catch (Exception)
267+
{
268+
// Fail to allocate memory for buffer, or
269+
// A concurrent condition: memory mapped file is disposed in other thread after TryGetLogStream() finishes.
270+
// In this case, silently fail.
271+
}
272+
}
273+
274274
protected override void OnEventSourceCreated(EventSource eventSource)
275275
{
276276
if (eventSource.Name.StartsWith(EventSourceNamePrefix, StringComparison.Ordinal))

src/OpenTelemetry/Logs/ILogger/OpenTelemetryLoggerProvider.cs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -18,7 +18,7 @@ public class OpenTelemetryLoggerProvider : BaseProvider, ILoggerProvider, ISuppo
1818
{
1919
internal readonly LoggerProvider Provider;
2020
private readonly bool ownsProvider;
21-
private readonly Hashtable loggers = new();
21+
private readonly Hashtable loggers = [];
2222
private bool disposed;
2323

2424
static OpenTelemetryLoggerProvider()

src/OpenTelemetry/Logs/LoggerProviderSdk.cs

Lines changed: 24 additions & 24 deletions
Original file line numberDiff line numberDiff line change
@@ -88,13 +88,36 @@ public LoggerProviderSdk(
8888

8989
public ILogRecordPool LogRecordPool => this.threadStaticPool ?? LogRecordSharedPool.Current;
9090

91+
public static bool ContainsBatchProcessor(BaseProcessor<LogRecord> processor)
92+
{
93+
if (processor is BatchExportProcessor<LogRecord>)
94+
{
95+
return true;
96+
}
97+
else if (processor is CompositeProcessor<LogRecord> compositeProcessor)
98+
{
99+
var current = compositeProcessor.Head;
100+
while (current != null)
101+
{
102+
if (ContainsBatchProcessor(current.Value))
103+
{
104+
return true;
105+
}
106+
107+
current = current.Next;
108+
}
109+
}
110+
111+
return false;
112+
}
113+
91114
public void AddProcessor(BaseProcessor<LogRecord> processor)
92115
{
93116
Guard.ThrowIfNull(processor);
94117

95118
processor.SetParentProvider(this);
96119

97-
if (this.threadStaticPool != null && this.ContainsBatchProcessor(processor))
120+
if (this.threadStaticPool != null && ContainsBatchProcessor(processor))
98121
{
99122
OpenTelemetrySdkEventSource.Log.LoggerProviderSdkEvent("Using shared thread pool.");
100123

@@ -168,29 +191,6 @@ public bool Shutdown(int timeoutMilliseconds)
168191
}
169192
}
170193

171-
public bool ContainsBatchProcessor(BaseProcessor<LogRecord> processor)
172-
{
173-
if (processor is BatchExportProcessor<LogRecord>)
174-
{
175-
return true;
176-
}
177-
else if (processor is CompositeProcessor<LogRecord> compositeProcessor)
178-
{
179-
var current = compositeProcessor.Head;
180-
while (current != null)
181-
{
182-
if (this.ContainsBatchProcessor(current.Value))
183-
{
184-
return true;
185-
}
186-
187-
current = current.Next;
188-
}
189-
}
190-
191-
return false;
192-
}
193-
194194
/// <inheritdoc />
195195
#if EXPOSE_EXPERIMENTAL_FEATURES
196196
protected

src/OpenTelemetry/Metrics/AggregationTemporality.cs

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -7,7 +7,11 @@ namespace OpenTelemetry.Metrics;
77
/// Enumeration used to define the aggregation temporality for a <see
88
/// cref="Metric"/>.
99
/// </summary>
10+
#pragma warning disable CA1008 // Enums should have zero value
11+
#pragma warning disable CA1028 // Enum storage should be Int32
1012
public enum AggregationTemporality : byte
13+
#pragma warning restore CA1028 // Enum storage should be Int32
14+
#pragma warning restore CA1008 // Enums should have zero value
1115
{
1216
/// <summary>
1317
/// Cumulative.

src/OpenTelemetry/Metrics/AggregatorStore.cs

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -23,7 +23,7 @@ internal sealed class AggregatorStore
2323
internal readonly int NumberOfMetricPoints;
2424
internal readonly ConcurrentDictionary<Tags, LookupData>? TagsToMetricPointIndexDictionaryDelta;
2525
internal readonly Func<ExemplarReservoir?>? ExemplarReservoirFactory;
26-
internal long DroppedMeasurements = 0;
26+
internal long DroppedMeasurements;
2727

2828
private const ExemplarFilterType DefaultExemplarFilter = ExemplarFilterType.AlwaysOff;
2929
private static readonly Comparison<KeyValuePair<string, object?>> DimensionComparisonDelegate = (x, y) => x.Key.CompareTo(y.Key);
@@ -50,8 +50,8 @@ internal sealed class AggregatorStore
5050
private readonly ExemplarFilterType exemplarFilter;
5151
private readonly Func<KeyValuePair<string, object?>[], int, int> lookupAggregatorStore;
5252

53-
private int metricPointIndex = 0;
54-
private int batchSize = 0;
53+
private int metricPointIndex;
54+
private int batchSize;
5555
private bool zeroTagMetricPointInitialized;
5656
private bool overflowTagMetricPointInitialized;
5757

src/OpenTelemetry/Metrics/Builder/MeterProviderBuilderExtensions.cs

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -108,7 +108,7 @@ public static MeterProviderBuilder AddView(this MeterProviderBuilder meterProvid
108108
throw new ArgumentException($"Custom view name {name} is invalid.", nameof(name));
109109
}
110110

111-
if (instrumentName.IndexOf('*') != -1)
111+
if (instrumentName.Contains('*'))
112112
{
113113
throw new ArgumentException(
114114
$"Instrument selection criteria is invalid. Instrument name '{instrumentName}' " +
@@ -136,7 +136,7 @@ public static MeterProviderBuilder AddView(this MeterProviderBuilder meterProvid
136136
Guard.ThrowIfNullOrWhitespace(instrumentName);
137137
Guard.ThrowIfNull(metricStreamConfiguration);
138138

139-
if (metricStreamConfiguration.Name != null && instrumentName.IndexOf('*') != -1)
139+
if (metricStreamConfiguration.Name != null && instrumentName.Contains('*'))
140140
{
141141
throw new ArgumentException(
142142
$"Instrument selection criteria is invalid. Instrument name '{instrumentName}' " +
@@ -149,7 +149,7 @@ public static MeterProviderBuilder AddView(this MeterProviderBuilder meterProvid
149149
{
150150
if (builder is MeterProviderBuilderSdk meterProviderBuilderSdk)
151151
{
152-
if (instrumentName.IndexOf('*') != -1)
152+
if (instrumentName.Contains('*'))
153153
{
154154
var pattern = '^' + Regex.Escape(instrumentName).Replace("\\*", ".*");
155155
var regex = new Regex(pattern, RegexOptions.Compiled | RegexOptions.IgnoreCase);

0 commit comments

Comments
 (0)