4545import org .springframework .web .bind .annotation .ResponseBody ;
4646import software .amazon .awssdk .services .s3 .S3Client ;
4747import software .amazon .awssdk .services .s3 .model .GetBucketLocationRequest ;
48+ import io .opentelemetry .api .GlobalOpenTelemetry ;
49+ import io .opentelemetry .api .metrics .Meter ;
50+ import io .opentelemetry .api .metrics .LongCounter ;
51+ import io .opentelemetry .api .common .Attributes ;
52+ import io .opentelemetry .api .common .AttributeKey ;
53+ import io .opentelemetry .exporter .otlp .http .metrics .OtlpHttpMetricExporter ;
54+ import io .opentelemetry .sdk .metrics .export .MetricExporter ;
55+ import io .opentelemetry .sdk .metrics .SdkMeterProvider ;
56+ import io .opentelemetry .sdk .resources .Resource ;
57+ import io .opentelemetry .sdk .metrics .export .MetricReader ;
58+ import io .opentelemetry .sdk .metrics .export .PeriodicMetricReader ;
59+ import io .opentelemetry .api .OpenTelemetry ;
60+ import io .opentelemetry .sdk .OpenTelemetrySdk ;
61+ import io .opentelemetry .api .metrics .DoubleHistogram ;
62+ import io .opentelemetry .api .metrics .LongUpDownCounter ;
63+ import java .time .Duration ;
4864
4965@ Controller
5066public class FrontendServiceController {
@@ -76,10 +92,74 @@ private void runLocalRootClientCallRecurringService() { // run the service
7692 executorService .scheduleAtFixedRate (runnableTask , 100 , 1000 , TimeUnit .MILLISECONDS );
7793 }
7894
95+ // Agent-based metrics using GlobalOpenTelemetry
96+ private final Meter agentMeter ;
97+ private final LongCounter agentBasedCounter ;
98+ private final DoubleHistogram agentBasedHistogram ;
99+ private final LongUpDownCounter agentBasedGauge ;
100+
101+ // Pipeline-based metrics (conditionally initialized)
102+ private final Meter customPipelineMeter ;
103+ private final LongCounter customPipelineCounter ;
104+ private final DoubleHistogram customPipelineHistogram ;
105+ private final LongUpDownCounter customPipelineGauge ;
106+
79107 @ Autowired
80108 public FrontendServiceController (CloseableHttpClient httpClient , S3Client s3 ) {
81109 this .httpClient = httpClient ;
82110 this .s3 = s3 ;
111+
112+ // Initialize agent-based metrics using GLOBAL OpenTelemetry (ADOT agent's configuration)
113+ this .agentMeter = GlobalOpenTelemetry .get ().getMeter ("agent-meter" );
114+
115+ this .agentBasedCounter = agentMeter .counterBuilder ("agent_based_counter" ).build ();
116+ this .agentBasedHistogram = agentMeter .histogramBuilder ("agent_based_histogram" ).build ();
117+ this .agentBasedGauge = agentMeter .upDownCounterBuilder ("agent_based_gauge" ).build ();
118+
119+ // Get environment variables
120+ String serviceName = System .getenv ("SERVICE_NAME" );
121+ String deploymentEnvironmentName = System .getenv ("DEPLOYMENT_ENVIRONMENT_NAME" );
122+
123+ // Only create pipeline if environment variables exist (matching Python logic)
124+ if (serviceName != null && deploymentEnvironmentName != null &&
125+ !serviceName .isEmpty () && !deploymentEnvironmentName .isEmpty ()) {
126+
127+ Resource pipelineResource = Resource .getDefault ().toBuilder ()
128+ .put ("service.name" , serviceName )
129+ .put ("deployment.environment.name" , deploymentEnvironmentName )
130+ .build ();
131+
132+ MetricExporter pipelineMetricExporter = OtlpHttpMetricExporter .builder ()
133+ .setEndpoint ("http://localhost:4318/v1/metrics" )
134+ .setTimeout (Duration .ofSeconds (10 ))
135+ .build ();
136+
137+ MetricReader pipelineMetricReader = PeriodicMetricReader .builder (pipelineMetricExporter )
138+ .setInterval (Duration .ofSeconds (1 ))
139+ .build ();
140+
141+ SdkMeterProvider pipelineMeterProvider = SdkMeterProvider .builder ()
142+ .setResource (pipelineResource )
143+ .registerMetricReader (pipelineMetricReader )
144+ .build ();
145+
146+ // Initialize pipeline metrics using SEPARATE SdkMeterProvider
147+ this .customPipelineMeter = pipelineMeterProvider .get ("pipeline-meter" );
148+
149+ this .customPipelineCounter = customPipelineMeter .counterBuilder ("custom_pipeline_counter" ).build ();
150+ this .customPipelineHistogram = customPipelineMeter .histogramBuilder ("custom_pipeline_histogram" ).build ();
151+ this .customPipelineGauge = customPipelineMeter .upDownCounterBuilder ("custom_pipeline_gauge" ).build ();
152+ } else {
153+ // No pipeline metrics if environment variables missing
154+ this .customPipelineMeter = null ;
155+ this .customPipelineCounter = null ;
156+ this .customPipelineHistogram = null ;
157+ this .customPipelineGauge = null ;
158+ }
159+ }
160+
161+ private int random (int min , int max ) {
162+ return (int ) (Math .random () * (max - min + 1 )) + min ;
83163 }
84164
85165 @ GetMapping ("/" )
@@ -92,6 +172,29 @@ public String healthcheck() {
92172 @ GetMapping ("/aws-sdk-call" )
93173 @ ResponseBody
94174 public String awssdkCall (@ RequestParam (name = "testingId" , required = false ) String testingId ) {
175+
176+ // Record agent-based metrics
177+ int histogramValue = random (100 ,1000 );
178+ int gaugeValue = random (-10 ,10 );
179+
180+ agentBasedCounter .add (1 , Attributes .of (AttributeKey .stringKey ("Operation" ), "counter" ));
181+
182+ agentBasedHistogram .record ((double )histogramValue , Attributes .of (AttributeKey .stringKey ("Operation" ), "histogram" ));
183+
184+ agentBasedGauge .add (gaugeValue , Attributes .of (AttributeKey .stringKey ("Operation" ), "gauge" ));
185+
186+ // Only record pipeline metrics if pipeline exists (matching Python logic)
187+ if (customPipelineCounter != null ) {
188+ int pipelineHistogramValue = random (100 ,1000 );
189+ int pipelineGaugeValue = random (-10 ,10 );
190+
191+ customPipelineCounter .add (1 , Attributes .of (AttributeKey .stringKey ("Operation" ), "pipeline_counter" ));
192+
193+ customPipelineHistogram .record (pipelineHistogramValue , Attributes .of (AttributeKey .stringKey ("Operation" ), "pipeline_histogram" ));
194+
195+ customPipelineGauge .add (pipelineGaugeValue , Attributes .of (AttributeKey .stringKey ("Operation" ), "pipeline_gauge" ));
196+ }
197+
95198 String bucketName = "e2e-test-bucket-name" ;
96199 if (testingId != null ) {
97200 bucketName += "-" + testingId ;
@@ -186,4 +289,4 @@ private String getXrayTraceId() {
186289 String xrayTraceId = "1-" + traceId .substring (0 , 8 ) + "-" + traceId .substring (8 );
187290 return String .format ("{\" traceId\" : \" %s\" }" , xrayTraceId );
188291 }
189- }
292+ }
0 commit comments