Skip to content

Commit a839f97

Browse files
committed
extract method
1 parent 4488b2d commit a839f97

File tree

1 file changed

+41
-38
lines changed
  • agent/agent-tooling/src/main/java/com/microsoft/applicationinsights/agent/internal/exporter

1 file changed

+41
-38
lines changed

agent/agent-tooling/src/main/java/com/microsoft/applicationinsights/agent/internal/exporter/AgentLogExporter.java

Lines changed: 41 additions & 38 deletions
Original file line numberDiff line numberDiff line change
@@ -24,7 +24,7 @@
2424
import io.opentelemetry.sdk.logs.export.LogRecordExporter;
2525
import io.opentelemetry.sdk.trace.samplers.SamplingDecision;
2626
import io.opentelemetry.sdk.trace.samplers.SamplingResult;
27-
import io.opentelemetry.semconv.SemanticAttributes;
27+
import io.opentelemetry.semconv.ExceptionAttributes;
2828
import java.util.Collection;
2929
import java.util.List;
3030
import java.util.function.Consumer;
@@ -99,55 +99,58 @@ private CompletableResultCode internalExport(Collection<LogRecordData> logs) {
9999
return CompletableResultCode.ofFailure();
100100
}
101101
for (LogRecordData log : logs) {
102-
try {
103-
int severityNumber = log.getSeverity().getSeverityNumber();
104-
if (severityNumber < severityThreshold) {
105-
continue;
106-
}
102+
internalExport(log);
103+
}
104+
// always returning success, because all error handling is performed internally
105+
return CompletableResultCode.ofSuccess();
106+
}
107107

108-
String stack = log.getAttributes().get(SemanticAttributes.EXCEPTION_STACKTRACE);
108+
private void internalExport(LogRecordData log) {
109+
try {
110+
int severityNumber = log.getSeverity().getSeverityNumber();
111+
if (severityNumber < severityThreshold) {
112+
return;
113+
}
109114

110-
SamplingOverrides samplingOverrides =
111-
stack != null ? exceptionSamplingOverrides : logSamplingOverrides;
115+
String stack = log.getAttributes().get(ExceptionAttributes.EXCEPTION_STACKTRACE);
112116

113-
SpanContext spanContext = log.getSpanContext();
114-
Double parentSpanSampleRate = log.getAttributes().get(AiSemanticAttributes.SAMPLE_RATE);
117+
SamplingOverrides samplingOverrides =
118+
stack != null ? exceptionSamplingOverrides : logSamplingOverrides;
115119

116-
AiSamplerForOverride sampler = samplingOverrides.getOverride(log.getAttributes());
120+
SpanContext spanContext = log.getSpanContext();
121+
Double parentSpanSampleRate = log.getAttributes().get(AiSemanticAttributes.SAMPLE_RATE);
117122

118-
if (sampler == null && spanContext.isValid() && !spanContext.getTraceFlags().isSampled()) {
119-
// if there is no sampling override, and the log is part of an unsampled trace, then don't
120-
// capture it
121-
continue;
122-
}
123+
AiSamplerForOverride sampler = samplingOverrides.getOverride(log.getAttributes());
123124

124-
Double sampleRate = null;
125-
if (sampler != null) {
126-
SamplingResult samplingResult =
127-
sampler.shouldSampleLog(spanContext, parentSpanSampleRate);
128-
if (samplingResult.getDecision() != SamplingDecision.RECORD_AND_SAMPLE) {
129-
continue;
130-
}
131-
sampleRate = samplingResult.getAttributes().get(AiSemanticAttributes.SAMPLE_RATE);
132-
}
125+
if (sampler == null && spanContext.isValid() && !spanContext.getTraceFlags().isSampled()) {
126+
// if there is no sampling override, and the log is part of an unsampled trace,
127+
// then don't capture it
128+
return;
129+
}
133130

134-
if (sampleRate == null) {
135-
sampleRate = parentSpanSampleRate;
131+
Double sampleRate = null;
132+
if (sampler != null) {
133+
SamplingResult samplingResult = sampler.shouldSampleLog(spanContext, parentSpanSampleRate);
134+
if (samplingResult.getDecision() != SamplingDecision.RECORD_AND_SAMPLE) {
135+
return;
136136
}
137+
sampleRate = samplingResult.getAttributes().get(AiSemanticAttributes.SAMPLE_RATE);
138+
}
139+
140+
if (sampleRate == null) {
141+
sampleRate = parentSpanSampleRate;
142+
}
137143

138-
logger.debug("exporting log: {}", log);
144+
logger.debug("exporting log: {}", log);
139145

140-
// TODO (trask) no longer need to check AiSemanticAttributes.SAMPLE_RATE in map() method
141-
TelemetryItem telemetryItem = mapper.map(log, stack, sampleRate);
142-
telemetryItemConsumer.accept(telemetryItem);
146+
// TODO (trask) no longer need to check AiSemanticAttributes.SAMPLE_RATE in map() method
147+
TelemetryItem telemetryItem = mapper.map(log, stack, sampleRate);
148+
telemetryItemConsumer.accept(telemetryItem);
143149

144-
exportingLogLogger.recordSuccess();
145-
} catch (Throwable t) {
146-
exportingLogLogger.recordFailure(t.getMessage(), t, EXPORTER_MAPPING_ERROR);
147-
}
150+
exportingLogLogger.recordSuccess();
151+
} catch (Throwable t) {
152+
exportingLogLogger.recordFailure(t.getMessage(), t, EXPORTER_MAPPING_ERROR);
148153
}
149-
// always returning success, because all error handling is performed internally
150-
return CompletableResultCode.ofSuccess();
151154
}
152155

153156
@Override

0 commit comments

Comments
 (0)