Skip to content

Commit b686dc1

Browse files
committed
refactoring + more unit tests for all exporters
1 parent 8067773 commit b686dc1

File tree

9 files changed

+936
-524
lines changed

9 files changed

+936
-524
lines changed

awsagentprovider/src/main/java/software/amazon/opentelemetry/javaagent/providers/exporter/aws/common/BaseEmfExporter.java

Lines changed: 11 additions & 16 deletions
Original file line numberDiff line numberDiff line change
@@ -38,13 +38,16 @@
3838
import java.util.Map;
3939
import java.util.TreeMap;
4040
import java.util.logging.Logger;
41+
import software.amazon.opentelemetry.javaagent.providers.exporter.aws.common.emitter.LogEventEmitter;
4142

42-
public abstract class BaseEmfExporter implements MetricExporter {
43+
public abstract class BaseEmfExporter<T> implements MetricExporter {
4344
private static final Logger logger = Logger.getLogger(BaseEmfExporter.class.getName());
4445
private final String namespace;
46+
protected final LogEventEmitter<T> emitter;
4547

46-
protected BaseEmfExporter(String namespace) {
48+
protected BaseEmfExporter(String namespace, LogEventEmitter<T> emitter) {
4749
this.namespace = namespace != null ? namespace : "default";
50+
this.emitter = emitter;
4851
}
4952

5053
@Override
@@ -80,7 +83,9 @@ record =
8083
}
8184

8285
if (record == null) {
83-
logger.fine("Unsupported metric data type: " + metricData.getClass().getSimpleName());
86+
logger.fine(
87+
String.format(
88+
"Unsupported metric data type: %s", metricData.getClass().getSimpleName()));
8489
continue;
8590
}
8691

@@ -102,17 +107,16 @@ record =
102107
this.namespace,
103108
firstRecord.getTimestamp());
104109

105-
// Create log event with message and timestamp like Python implementation
106110
Map<String, Object> logEvent = new HashMap<>();
107111
logEvent.put("message", new ObjectMapper().writeValueAsString(emfLog));
108112
logEvent.put("timestamp", firstRecord.getTimestamp());
109-
this.emit(logEvent);
113+
this.emitter.emit(logEvent);
110114
}
111115
}
112116

113117
return CompletableResultCode.ofSuccess();
114118
} catch (Exception e) {
115-
logger.severe("Failed to export metrics: " + e.getMessage());
119+
logger.severe(String.format("Failed to export metrics: %s", e.getMessage()));
116120
return CompletableResultCode.ofFailure();
117121
}
118122
}
@@ -136,17 +140,8 @@ public Aggregation getDefaultAggregation(InstrumentType instrumentType) {
136140
return Aggregation.defaultAggregation();
137141
}
138142

139-
/**
140-
* Export a log event.
141-
*
142-
* <p>This method must be implemented by subclasses to define where the EMF logs are sent.
143-
*
144-
* @param logEvent The log event to send
145-
*/
146-
protected abstract void emit(Map<String, Object> logEvent);
147-
148143
private String groupByAttributesAndTimestamp(MetricRecord record) {
149-
// Java doesn't have built-in, hashable tuples, so we
144+
// Java doesn't have built-in, hash-able tuples, so we
150145
// concatenate the attributes key and timestamp into a single string to create a unique
151146
// grouping key for the HashMap.
152147
String attrsKey = getAttributesKey(record.getAttributes());

0 commit comments

Comments
 (0)