19
19
import io .opentelemetry .contrib .generator .core .dto .GeneratorResource ;
20
20
import io .opentelemetry .contrib .generator .telemetry .GeneratorsStateProvider ;
21
21
import io .opentelemetry .contrib .generator .telemetry .dto .GeneratorState ;
22
+ import io .opentelemetry .contrib .generator .telemetry .misc .GeneratorUtils ;
22
23
import io .opentelemetry .contrib .generator .telemetry .transport .PayloadHandler ;
23
24
import io .opentelemetry .contrib .generator .telemetry .ResourceModelProvider ;
24
25
import io .opentelemetry .contrib .generator .telemetry .jel .JELProvider ;
25
26
import io .opentelemetry .contrib .generator .telemetry .misc .Constants ;
26
27
import io .opentelemetry .contrib .generator .telemetry .metrics .dto .MetricDefinition ;
27
28
import io .opentelemetry .proto .collector .metrics .v1 .ExportMetricsServiceRequest ;
28
29
import io .opentelemetry .proto .common .v1 .InstrumentationScope ;
29
- import io .opentelemetry .proto .metrics .v1 .Metric ;
30
- import io .opentelemetry .proto .metrics .v1 .ResourceMetrics ;
31
- import io .opentelemetry .proto .metrics .v1 .ScopeMetrics ;
30
+ import io .opentelemetry .proto .common .v1 .KeyValue ;
31
+ import io .opentelemetry .proto .metrics .v1 .*;
32
32
import io .opentelemetry .proto .resource .v1 .Resource ;
33
33
import jakarta .el .ELProcessor ;
34
34
import lombok .extern .slf4j .Slf4j ;
35
35
36
- import java .util .ArrayList ;
37
- import java .util .List ;
36
+ import java .util .* ;
37
+ import java .util .function . Function ;
38
38
import java .util .stream .Collectors ;
39
39
40
40
/**
@@ -45,7 +45,7 @@ public class MetricGeneratorThread implements Runnable {
45
45
46
46
private final String groupKey ;
47
47
private final String requestID ;
48
- private final List < MetricDefinition > metrics ;
48
+ private final Map < String , MetricDefinition > metrics ;
49
49
private final PayloadHandler payloadHandler ;
50
50
private final GeneratorState <MetricGeneratorThread > metricGeneratorState ;
51
51
private final GaugeGenerator gaugeGenerator ;
@@ -57,7 +57,7 @@ public MetricGeneratorThread(String groupKey, List<MetricDefinition> metrics, Pa
57
57
String requestID ) {
58
58
this .groupKey = groupKey ;
59
59
this .requestID = requestID ;
60
- this .metrics = metrics ;
60
+ this .metrics = metrics . stream (). collect ( Collectors . toMap ( MetricDefinition :: getName , Function . identity ())) ;
61
61
this .payloadHandler = payloadHandler ;
62
62
this .metricGeneratorState = GeneratorsStateProvider .getMetricGeneratorState (requestID );
63
63
ELProcessor jelProcessor = JELProvider .getJelProcessor ();
@@ -70,18 +70,26 @@ public MetricGeneratorThread(String groupKey, List<MetricDefinition> metrics, Pa
70
70
@ Override
71
71
public void run () {
72
72
log .debug (requestID + ": Metric generator thread invoked for resource type: " + groupKey + " with metrics: " +
73
- metrics .stream ().map (MetricDefinition ::getName ).collect (Collectors .toList ()));
74
- if (metricGeneratorState .isGenerateData () && currentCount < metrics .get (0 ).getPayloadCount ()) {
73
+ metrics .values ().stream ().map (MetricDefinition ::getName ).collect (Collectors .toList ()));
74
+ int payloadCount = metrics .values ().stream ().findFirst ().get ().getPayloadCount ();
75
+ if (metricGeneratorState .isGenerateData () && currentCount < payloadCount ) {
75
76
List <ResourceMetrics > resourceMetricsList = new ArrayList <>();
76
77
ResourceMetrics resourceMetric ;
77
- List <Metric > otelMetrics = metrics .stream ()
78
+ List <Metric . Builder > partialOTelMetrics = metrics . values () .stream ()
78
79
.map (this ::getMetric ).collect (Collectors .toList ());
79
- List <Resource > resources = ResourceModelProvider .getResourceModel (requestID ).get (groupKey .split ("::" )[0 ]).stream ()
80
+ List <Resource > resources = ResourceModelProvider .getResourceModel (requestID )
81
+ .get (groupKey .split ("::" )[0 ]).stream ()
80
82
.filter (GeneratorResource ::isActive )
81
83
.map (GeneratorResource ::getOTelResource )
82
84
.collect (Collectors .toList ());
83
85
log .debug (requestID + ": Preparing " + resources .size () + " resource metric packets for " + groupKey );
84
86
for (Resource eachResource : resources ) {
87
+ List <Metric > otelMetrics = new ArrayList <>();
88
+ for (Metric .Builder eachPartialMetric : partialOTelMetrics ) {
89
+ List <KeyValue > resourceAttrs = GeneratorUtils .getResourceAttributes (metrics .get (eachPartialMetric .getName ())
90
+ .getCopyResourceAttributes (), eachResource );
91
+ otelMetrics .add (getMetricWithResourceAttributes (eachPartialMetric , resourceAttrs ));
92
+ }
85
93
resourceMetric = ResourceMetrics .newBuilder ()
86
94
.setResource (eachResource )
87
95
.addScopeMetrics (ScopeMetrics .newBuilder ()
@@ -106,7 +114,7 @@ public void run() {
106
114
}
107
115
}
108
116
109
- private Metric getMetric (MetricDefinition metricDefinition ) {
117
+ private Metric . Builder getMetric (MetricDefinition metricDefinition ) {
110
118
switch (metricDefinition .getOtelType ()) {
111
119
case Constants .GAUGE :
112
120
return gaugeGenerator .getOTelMetric (metricDefinition );
@@ -116,4 +124,29 @@ private Metric getMetric(MetricDefinition metricDefinition) {
116
124
return summaryGenerator .getOTelMetric (metricDefinition );
117
125
}
118
126
}
127
+
128
+ private Metric getMetricWithResourceAttributes (Metric .Builder partialMetric , List <KeyValue > resourceAttributes ) {
129
+ Metric .DataCase metricType = partialMetric .getDataCase ();
130
+ if (metricType .equals (Metric .DataCase .GAUGE )) {
131
+ List <NumberDataPoint > dataPoints = partialMetric .getGauge ().getDataPointsList ();
132
+ List <NumberDataPoint > dataPointsWAttrs = dataPoints .stream ().map (NumberDataPoint ::toBuilder )
133
+ .map (bdp -> bdp .addAllAttributes (resourceAttributes ).build ()).collect (Collectors .toList ());
134
+ Gauge newGauge = partialMetric .getGauge ().toBuilder ().clearDataPoints ().addAllDataPoints (dataPointsWAttrs ).build ();
135
+ return Metric .newBuilder ().setName (partialMetric .getName ()).setUnit (partialMetric .getUnit ()).setGauge (newGauge ).build ();
136
+ } else if (metricType .equals (Metric .DataCase .SUM )) {
137
+ List <NumberDataPoint > dataPoints = partialMetric .getSum ().getDataPointsList ();
138
+ List <NumberDataPoint > dataPointsWAttrs = dataPoints .stream ().map (NumberDataPoint ::toBuilder )
139
+ .map (bdp -> bdp .addAllAttributes (resourceAttributes ).build ()).collect (Collectors .toList ());
140
+ partialMetric .getSum ().toBuilder ().clearDataPoints ().addAllDataPoints (dataPointsWAttrs ).build ();
141
+ Sum newSum = partialMetric .getSum ().toBuilder ().clearDataPoints ().addAllDataPoints (dataPointsWAttrs ).build ();
142
+ return Metric .newBuilder ().setName (partialMetric .getName ()).setUnit (partialMetric .getUnit ()).setSum (newSum ).build ();
143
+ } else {
144
+ List <SummaryDataPoint > dataPoints = partialMetric .getSummary ().getDataPointsList ();
145
+ List <SummaryDataPoint > dataPointsWAttrs = dataPoints .stream ().map (SummaryDataPoint ::toBuilder )
146
+ .map (bdp -> bdp .addAllAttributes (resourceAttributes ).build ()).collect (Collectors .toList ());
147
+ Summary newSummary = partialMetric .getSummary ().toBuilder ().clearDataPoints ().addAllDataPoints (dataPointsWAttrs ).build ();
148
+ return Metric .newBuilder ().setName (partialMetric .getName ()).setUnit (partialMetric .getUnit ()).setSummary (newSummary ).build ();
149
+ }
150
+ }
151
+
119
152
}
0 commit comments