43
43
import java .net .InetAddress ;
44
44
import java .net .UnknownHostException ;
45
45
import java .time .Duration ;
46
+ import java .util .Arrays ;
46
47
import java .util .Map ;
47
48
import java .util .concurrent .ConcurrentHashMap ;
48
49
import java .util .concurrent .Executors ;
@@ -70,7 +71,9 @@ public abstract class AbstractDc<Cfg extends BasicDcConfig> implements IDc<Cfg>
70
71
71
72
private int prometheusPort = DcUtil .DEFAULT_PROMETHEUS_PORT ;
72
73
private String prometheusHost = DcUtil .DEFAULT_PROMETHEUS_HOST ;
73
- private String [] prometricsMetricRestrictions = null ;
74
+ private String [] prometheusMetricRestrictions = null ;
75
+
76
+ private String [] metricRestrictions = null ;
74
77
75
78
private String serviceName = DcUtil .DEFAULT_OTEL_SERVICE_NAME ;
76
79
private String serviceInstanceId = null ;
@@ -196,33 +199,52 @@ public void setPrometheusHost(String prometheusHost) {
196
199
}
197
200
198
201
@ Override
199
- public String [] getPrometricsMetricRestrictions () {
200
- return prometricsMetricRestrictions ;
202
+ public String [] getPrometheusMetricRestrictions () {
203
+ return prometheusMetricRestrictions ;
201
204
}
202
205
203
- @ Override
204
- public void setPrometricsMetricRestrictions (String metricRestrictionString ) {
206
+ private String [] parseMetricRestrictions (String metricRestrictionString ) {
207
+ String [] restrictions = null ;
208
+
205
209
if (metricRestrictionString != null ) {
206
- String [] restrictions = metricRestrictionString .split ("," );
207
- prometricsMetricRestrictions = new String [restrictions .length ];
208
- for (int i = 0 ; i < restrictions .length ; i ++) {
209
- prometricsMetricRestrictions [i ] = restrictions [i ].trim ();
210
+ String [] restrictions0 = metricRestrictionString .split ("," );
211
+ restrictions = new String [restrictions0 .length ];
212
+ for (int i = 0 ; i < restrictions0 .length ; i ++) {
213
+ restrictions [i ] = restrictions0 [i ].trim ();
210
214
}
211
215
212
- } else {
213
- prometricsMetricRestrictions = null ;
214
216
}
217
+ return restrictions ;
218
+ }
219
+
220
+ @ Override
221
+ public void setPrometheusMetricRestrictions (String metricRestrictionString ) {
222
+ prometheusMetricRestrictions = parseMetricRestrictions (metricRestrictionString );
223
+ }
224
+
225
+ @ Override
226
+ public void setPrometheusMetricRestrictions (String [] prometricsMetricRestrictions ) {
227
+ this .prometheusMetricRestrictions = prometricsMetricRestrictions ;
228
+ }
229
+
230
+ @ Override
231
+ public String [] getMetricRestrictions () {
232
+ return metricRestrictions ;
215
233
}
216
234
217
235
@ Override
218
- public void setPrometricsMetricRestrictions (String [] prometricsMetricRestrictions ) {
219
- this .prometricsMetricRestrictions = prometricsMetricRestrictions ;
236
+ public void setMetricRestrictions (String metricRestrictionString ) {
237
+ metricRestrictions = parseMetricRestrictions (metricRestrictionString );
238
+ }
239
+
240
+ @ Override
241
+ public void setMetricRestrictions (String [] MetricRestrictions ) {
242
+ this .metricRestrictions = MetricRestrictions ;
220
243
}
221
244
222
245
@ Override
223
246
public String getHostname () {
224
- if (hostname != null )
225
- return hostname ;
247
+ if (hostname != null ) return hostname ;
226
248
227
249
try {
228
250
hostname = InetAddress .getLocalHost ().getHostName ();
@@ -243,8 +265,7 @@ public void setHostname(String hostname) {
243
265
244
266
@ Override
245
267
public long getPid () {
246
- if (pid != null )
247
- return pid ;
268
+ if (pid != null ) return pid ;
248
269
249
270
pid = DcUtil .getPid ();
250
271
return pid ;
@@ -257,8 +278,7 @@ public void setPid(long pid) {
257
278
258
279
@ Override
259
280
public String getContainerId () {
260
- if (containerId != null )
261
- return containerId ;
281
+ if (containerId != null ) return containerId ;
262
282
263
283
containerId = new ContainerResource ().getContainerId ().orElse (DcUtil .N_A );
264
284
return containerId ;
@@ -289,10 +309,24 @@ public Map<String, Meter> getMeters() {
289
309
public void registerMetrics () {
290
310
// Iterate through the raw metrics and register each one
291
311
for (RawMetric rawMetric : rawMetricsMap .values ()) {
292
- DcUtil .registerMetric (meters , rawMetric );
312
+ DcUtil .registerMetric (meters , rawMetric , this );
293
313
}
294
314
}
295
315
316
+ @ Override
317
+ public boolean preRecordMetric (RawMetric rawMetric ) {
318
+ if (metricRestrictions == null ) return true ;
319
+
320
+ return !Arrays .asList (metricRestrictions ).contains (rawMetric .getName ());
321
+ }
322
+
323
+ @ Override
324
+ public boolean preRecordMetric (String metricName , Number value , Map <String , Object > attributes ) {
325
+ if (metricRestrictions == null ) return true ;
326
+
327
+ return !Arrays .asList (metricRestrictions ).contains (metricName );
328
+ }
329
+
296
330
/**
297
331
* Retrieves a raw metric by its name.
298
332
*
@@ -447,8 +481,7 @@ public LogRecordExporter createOtlpHttpLogRecordExporter(Map<String, String> hea
447
481
448
482
//private static MetricReader prometheusMetricReader = null;
449
483
450
- private static final Predicate <String > dftResAttrsFilterForPrometheus =
451
- (String key ) -> DcUtil .OJR_PLUGIN .equals (key ) || HostIncubatingAttributes .HOST_NAME .getKey ().equals (key );
484
+ private static final Predicate <String > dftResAttrsFilterForPrometheus = (String key ) -> DcUtil .OJR_PLUGIN .equals (key ) || HostIncubatingAttributes .HOST_NAME .getKey ().equals (key );
452
485
453
486
/**
454
487
* Returns the filter for resource attributes used when exporting metrics to Prometheus.
@@ -484,7 +517,7 @@ public synchronized OjrPrometheusHttpServer createPrometheusHttpServerIfNotExist
484
517
if (prometheusHttpServer != null ) {
485
518
return prometheusHttpServer ; // Return early if the server is already created
486
519
}
487
- prometheusHttpServer = new OjrPrometheusHttpServer (prometheusHost , prometheusPort , null , MemoryMode .REUSABLE_DATA , prometricsMetricRestrictions );
520
+ prometheusHttpServer = new OjrPrometheusHttpServer (prometheusHost , prometheusPort , null , MemoryMode .REUSABLE_DATA , prometheusMetricRestrictions );
488
521
return prometheusHttpServer ;
489
522
}
490
523
@@ -601,9 +634,11 @@ public void readBuiltinParameters(Map<String, Object> properties, Cfg config) {
601
634
setBackendUrl ((String ) properties .getOrDefault (DcUtil .OTEL_BACKEND_URL , DcUtil .DEFAULT_OTEL_BACKEND_URL ));
602
635
setTransport ((String ) properties .getOrDefault (DcUtil .OTEL_TRANSPORT , DcUtil .DEFAULT_OTEL_TRANSPORT ));
603
636
637
+ setMetricRestrictions ((String ) properties .get (DcUtil .OTEL_RESTRICTED_METRICS ));
638
+
604
639
setPrometheusPort ((Integer ) properties .getOrDefault (DcUtil .PROMETHEUS_PORT , DcUtil .DEFAULT_PROMETHEUS_PORT ));
605
640
setPrometheusHost ((String ) properties .get (DcUtil .PROMETHEUS_HOST ));
606
- setPrometricsMetricRestrictions ((String ) properties .get (DcUtil .PROMETHEUS_RESTRICTED_METRICS ));
641
+ setPrometheusMetricRestrictions ((String ) properties .get (DcUtil .PROMETHEUS_RESTRICTED_METRICS ));
607
642
608
643
setServiceName ((String ) properties .getOrDefault (DcUtil .OTEL_SERVICE_NAME , DcUtil .DEFAULT_OTEL_SERVICE_NAME ));
609
644
setServiceInstanceId ((String ) properties .get (DcUtil .OTEL_SERVICE_INSTANCE_ID ));
@@ -647,8 +682,7 @@ public void initEnv(Map<String, Object> properties, Cfg config) throws Exception
647
682
* @return a Resource object containing the attributes.
648
683
*/
649
684
public Resource retrieveResourceAttributes () {
650
- Resource resource = Resource .create (Attributes .of (ServiceAttributes .SERVICE_NAME , serviceName , TelemetryAttributes .TELEMETRY_SDK_NAME , "ojr" ,
651
- TelemetryAttributes .TELEMETRY_SDK_LANGUAGE , "java" , TelemetryAttributes .TELEMETRY_SDK_VERSION , DcUtil .OCR_VERSION ));
685
+ Resource resource = Resource .create (Attributes .of (ServiceAttributes .SERVICE_NAME , serviceName , TelemetryAttributes .TELEMETRY_SDK_NAME , "ojr" , TelemetryAttributes .TELEMETRY_SDK_LANGUAGE , "java" , TelemetryAttributes .TELEMETRY_SDK_VERSION , DcUtil .OCR_VERSION ));
652
686
653
687
ResourceEnricher enricher = new ResourceEnricher (resource );
654
688
enricher .enrich (ServiceIncubatingAttributes .SERVICE_INSTANCE_ID , serviceInstanceId );
0 commit comments