2121import io .opentelemetry .sdk .metrics .Aggregation ;
2222import io .opentelemetry .sdk .metrics .InstrumentType ;
2323import io .opentelemetry .sdk .metrics .data .AggregationTemporality ;
24+ import io .opentelemetry .sdk .metrics .data .Data ;
2425import io .opentelemetry .sdk .metrics .data .ExponentialHistogramData ;
2526import io .opentelemetry .sdk .metrics .data .ExponentialHistogramPointData ;
2627import io .opentelemetry .sdk .metrics .data .GaugeData ;
@@ -57,29 +58,29 @@ public CompletableResultCode export(Collection<MetricData> metricsData) {
5758 Map <String , List <MetricRecord >> groupedMetrics = new HashMap <>();
5859
5960 for (MetricData metric : metricsData ) {
60- if (metric .getData () == null || metric .getData ().getPoints ().isEmpty ()) {
61+ Data <? extends PointData > metricData = metric .getData ();
62+ if (metricData == null || metricData .getPoints ().isEmpty ()) {
6163 continue ;
6264 }
6365
64- for (PointData point : metric . getData () .getPoints ()) {
66+ for (PointData point : metricData .getPoints ()) {
6567 MetricRecord record = null ;
6668
67- if (metric . getData () instanceof GaugeData || metric . getData () instanceof SumData ) {
69+ if (metricData instanceof GaugeData || metricData instanceof SumData ) {
6870 record = MetricRecord .convertGaugeAndSum (metric , point );
6971 }
70- if (metric . getData () instanceof HistogramData && point instanceof HistogramPointData ) {
72+ if (metricData instanceof HistogramData && point instanceof HistogramPointData ) {
7173 record = MetricRecord .convertHistogram (metric , (HistogramPointData ) point );
7274 }
73- if (metric . getData () instanceof ExponentialHistogramData
75+ if (metricData instanceof ExponentialHistogramData
7476 && point instanceof ExponentialHistogramPointData ) {
7577 record =
7678 MetricRecord .convertExponentialHistogram (
7779 metric , (ExponentialHistogramPointData ) point );
7880 }
7981
8082 if (record == null ) {
81- logger .fine (
82- "Unsupported metric data type: " + metric .getData ().getClass ().getSimpleName ());
83+ logger .fine ("Unsupported metric data type: " + metricData .getClass ().getSimpleName ());
8384 continue ;
8485 }
8586
@@ -116,25 +117,6 @@ record =
116117 }
117118 }
118119
119- private String groupByAttributesAndTimestamp (MetricRecord record ) {
120- // Java doesn't have built-in, hashable tuples, so we
121- // concatenate the attributes key and timestamp into a single string to create a unique
122- // grouping key for the HashMap.
123- String attrsKey = getAttributesKey (record .getAttributes ());
124- return attrsKey + "_" + record .getTimestamp ();
125- }
126-
127- private String getAttributesKey (Attributes attributes ) {
128- // Sort the attributes to ensure consistent keys
129- // Using TreeMap: The map is sorted
130- // according to the natural ordering of its keys, or by a Comparator provided at map creation
131- // time, depending on which constructor is used.
132- // https://docs.oracle.com/javase/8/docs/api/java/util/TreeMap.html
133- Map <String , Object > sortedAttrs = new TreeMap <>();
134- attributes .forEach ((key , value ) -> sortedAttrs .put (key .getKey (), value ));
135- return sortedAttrs .toString ();
136- }
137-
138120 @ Override
139121 public abstract CompletableResultCode flush ();
140122
@@ -143,7 +125,6 @@ private String getAttributesKey(Attributes attributes) {
143125
144126 @ Override
145127 public AggregationTemporality getAggregationTemporality (InstrumentType instrumentType ) {
146- // Set up temporality preference default to DELTA for all instrument types
147128 return AggregationTemporality .DELTA ;
148129 }
149130
@@ -156,11 +137,30 @@ public Aggregation getDefaultAggregation(InstrumentType instrumentType) {
156137 }
157138
158139 /**
159- * Send a log event to the destination (CloudWatch Logs, console, etc.) .
140+ * Export a log event.
160141 *
161142 * <p>This method must be implemented by subclasses to define where the EMF logs are sent.
162143 *
163144 * @param logEvent The log event to send
164145 */
165146 protected abstract void emit (Map <String , Object > logEvent );
147+
148+ private String groupByAttributesAndTimestamp (MetricRecord record ) {
149+ // Java doesn't have built-in, hashable tuples, so we
150+ // concatenate the attributes key and timestamp into a single string to create a unique
151+ // grouping key for the HashMap.
152+ String attrsKey = getAttributesKey (record .getAttributes ());
153+ return attrsKey + "_" + record .getTimestamp ();
154+ }
155+
156+ private String getAttributesKey (Attributes attributes ) {
157+ // Sort the attributes to ensure consistent keys
158+ // Using TreeMap: The map is sorted
159+ // according to the natural ordering of its keys, or by a Comparator provided at map creation
160+ // time, depending on which constructor is used.
161+ // https://docs.oracle.com/javase/8/docs/api/java/util/TreeMap.html
162+ Map <String , Object > sortedAttrs = new TreeMap <>();
163+ attributes .forEach ((key , value ) -> sortedAttrs .put (key .getKey (), value ));
164+ return sortedAttrs .toString ();
165+ }
166166}
0 commit comments