Skip to content

Commit 6acac91

Browse files
committed
Tweaks.
1 parent 80a746f commit 6acac91

File tree

2 files changed

+25
-9
lines changed

2 files changed

+25
-9
lines changed

src/OpenTelemetry/Logs/LogRecord.cs

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -573,9 +573,9 @@ private LogRecordEnrichedAttributes EnsureEnrichedAttributes()
573573
{
574574
if (this.Attributes is not LogRecordEnrichedAttributes enrichedAttributes)
575575
{
576-
enrichedAttributes = this.EnrichedAttributeStorage ??= new();
576+
enrichedAttributes = this.EnrichedAttributeStorage ??= new(this);
577577

578-
enrichedAttributes.Reset(this.Attributes);
578+
enrichedAttributes.Reset();
579579

580580
this.Attributes = enrichedAttributes;
581581
}

src/OpenTelemetry/Logs/LogRecordEnrichedAttributes.cs

Lines changed: 23 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -11,22 +11,35 @@ namespace OpenTelemetry.Logs;
1111
internal sealed class LogRecordEnrichedAttributes : IReadOnlyList<KeyValuePair<string, object?>>
1212
{
1313
private readonly List<KeyValuePair<string, object?>> enrichedAttributes = new();
14-
private IReadOnlyList<KeyValuePair<string, object?>> initialAttributes;
14+
private readonly LogRecord logRecord;
15+
private IReadOnlyList<KeyValuePair<string, object?>>? initialAttributes;
1516

16-
public LogRecordEnrichedAttributes()
17+
public LogRecordEnrichedAttributes(LogRecord logRecord)
1718
{
18-
this.initialAttributes = Array.Empty<KeyValuePair<string, object?>>();
19+
Debug.Assert(logRecord != null, "logRecord was null");
20+
21+
this.logRecord = logRecord!;
1922
}
2023

21-
public int Count => this.initialAttributes.Count + this.enrichedAttributes.Count;
24+
public int Count
25+
{
26+
get
27+
{
28+
Debug.Assert(this.initialAttributes != null, "this.initialAttributes was null");
29+
30+
return this.initialAttributes!.Count + this.enrichedAttributes.Count;
31+
}
32+
}
2233

2334
public KeyValuePair<string, object?> this[int index]
2435
{
2536
get
2637
{
2738
Guard.ThrowIfNegative(index);
2839

29-
var initialAttributes = this.initialAttributes;
40+
Debug.Assert(this.initialAttributes != null, "this.initialAttributes was null");
41+
42+
var initialAttributes = this.initialAttributes!;
3043

3144
var count = initialAttributes.Count;
3245
if (index < count)
@@ -38,9 +51,12 @@ public LogRecordEnrichedAttributes()
3851
}
3952
}
4053

41-
public void Reset(IReadOnlyList<KeyValuePair<string, object?>>? initialAttributes)
54+
public void Reset()
4255
{
43-
this.initialAttributes = initialAttributes ?? Array.Empty<KeyValuePair<string, object?>>();
56+
this.initialAttributes = this.logRecord.Attributes ?? Array.Empty<KeyValuePair<string, object?>>();
57+
58+
// Note: Clear sets the count/size to 0 but it maintains the underlying
59+
// array(capacity).
4460
this.enrichedAttributes.Clear();
4561
}
4662

0 commit comments

Comments
 (0)