11package io .github .delirius325 .jmeter .backendlistener .elasticsearch ;
22
33import java .net .InetAddress ;
4+ import java .net .UnknownHostException ;
45import java .text .ParseException ;
56import java .text .SimpleDateFormat ;
67import java .time .LocalDateTime ;
2425
2526public class ElasticSearchMetric {
2627 private static final Logger logger = LoggerFactory .getLogger (ElasticSearchMetric .class );
28+ private static final String HOSTNAME = solveHostName ();
29+
2730 private SampleResult sampleResult ;
2831 private String esTestMode ;
2932 private String esTimestamp ;
@@ -46,6 +49,15 @@ public ElasticSearchMetric(
4649 this .fields = fields ;
4750 }
4851
52+ private static String solveHostName () {
53+ try {
54+ return InetAddress .getLocalHost ().getHostName ();
55+ } catch (UnknownHostException e ) {
56+ logger .warn ("Could not resolve host name, falling back to localhost" , e );
57+ return "localhost" ;
58+ }
59+ }
60+
4961 /**
5062 * This method returns the current metric as a Map(String, Object) for the provided sampleResult
5163 *
@@ -77,7 +89,7 @@ public Map<String, Object> getMetric(BackendListenerContext context) throws Exce
7789 addFilteredJSON ("SampleStartTime" , sdf .format (new Date (this .sampleResult .getStartTime ())));
7890 addFilteredJSON ("SampleEndTime" , sdf .format (new Date (this .sampleResult .getEndTime ())));
7991 addFilteredJSON ("Timestamp" , this .sampleResult .getTimeStamp ());
80- addFilteredJSON ("InjectorHostname" , InetAddress . getLocalHost (). getHostName () );
92+ addFilteredJSON ("InjectorHostname" , HOSTNAME );
8193
8294 // Add the details according to the mode that is set
8395 switch (this .esTestMode ) {
0 commit comments