forked from open-telemetry/opentelemetry-java
-
Notifications
You must be signed in to change notification settings - Fork 0
/
SdkLogRecordBuilder.java
113 lines (99 loc) · 3.41 KB
/
SdkLogRecordBuilder.java
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
/*
* Copyright The OpenTelemetry Authors
* SPDX-License-Identifier: Apache-2.0
*/
package io.opentelemetry.sdk.logs;
import io.opentelemetry.api.common.AttributeKey;
import io.opentelemetry.api.logs.EventBuilder;
import io.opentelemetry.api.logs.LogRecordBuilder;
import io.opentelemetry.api.logs.Severity;
import io.opentelemetry.api.trace.Span;
import io.opentelemetry.context.Context;
import io.opentelemetry.sdk.common.InstrumentationScopeInfo;
import io.opentelemetry.sdk.internal.AttributesMap;
import io.opentelemetry.sdk.logs.data.Body;
import java.time.Instant;
import java.util.concurrent.TimeUnit;
import javax.annotation.Nullable;
/** SDK implementation of {@link EventBuilder} and {@link LogRecordBuilder}. */
final class SdkLogRecordBuilder implements EventBuilder {
private final LoggerSharedState loggerSharedState;
private final LogLimits logLimits;
private final InstrumentationScopeInfo instrumentationScopeInfo;
private long epochNanos;
@Nullable private Context context;
private Severity severity = Severity.UNDEFINED_SEVERITY_NUMBER;
@Nullable private String severityText;
private Body body = Body.empty();
@Nullable private AttributesMap attributes;
SdkLogRecordBuilder(
LoggerSharedState loggerSharedState, InstrumentationScopeInfo instrumentationScopeInfo) {
this.loggerSharedState = loggerSharedState;
this.logLimits = loggerSharedState.getLogLimits();
this.instrumentationScopeInfo = instrumentationScopeInfo;
}
@Override
public SdkLogRecordBuilder setEpoch(long timestamp, TimeUnit unit) {
this.epochNanos = unit.toNanos(timestamp);
return this;
}
@Override
public SdkLogRecordBuilder setEpoch(Instant instant) {
this.epochNanos = TimeUnit.SECONDS.toNanos(instant.getEpochSecond()) + instant.getNano();
return this;
}
@Override
public SdkLogRecordBuilder setContext(Context context) {
this.context = context;
return this;
}
@Override
public SdkLogRecordBuilder setSeverity(Severity severity) {
this.severity = severity;
return this;
}
@Override
public SdkLogRecordBuilder setSeverityText(String severityText) {
this.severityText = severityText;
return this;
}
@Override
public SdkLogRecordBuilder setBody(String body) {
this.body = Body.string(body);
return this;
}
@Override
public <T> SdkLogRecordBuilder setAttribute(AttributeKey<T> key, T value) {
if (key == null || key.getKey().isEmpty() || value == null) {
return this;
}
if (this.attributes == null) {
this.attributes =
AttributesMap.create(
logLimits.getMaxNumberOfAttributes(), logLimits.getMaxAttributeValueLength());
}
this.attributes.put(key, value);
return this;
}
@Override
public void emit() {
if (loggerSharedState.hasBeenShutdown()) {
return;
}
Context context = this.context == null ? Context.current() : this.context;
loggerSharedState
.getLogRecordProcessor()
.onEmit(
context,
SdkReadWriteLogRecord.create(
loggerSharedState.getLogLimits(),
loggerSharedState.getResource(),
instrumentationScopeInfo,
this.epochNanos == 0 ? this.loggerSharedState.getClock().now() : this.epochNanos,
Span.fromContext(context).getSpanContext(),
severity,
severityText,
body,
attributes));
}
}