gameover453 opened a new issue #4520: Enhanced Agent Hostname detection function
URL: https://github.com/apache/skywalking/issues/4520
 
 
   Please answer these questions before submitting your issue.
   
   - Why do you submit this issue?
   - [ ] Question or discussion
   - [ ] Bug
   - [ ] Requirement
   - [X] Feature or performance improvement
   
   ### Requirement or improvement
   
   We know that the hostname detection method of skywalking-agent is provided 
by JVM API InetAddress.getLocalhost().GetHostName().
   
   
https://github.com/apache/skywalking/blob/69e3d80e24b203bef4d86bfce2ef201a961373b0/apm-sniffer/apm-agent-core/src/main/java/org/apache/skywalking/apm/agent/core/os/OSUtil.java#L46-L54
   
   However, in some Linux system multiple network card scenarios (for 
example,container/ Alibaba Cloud ECS), the /etc/hosts file configuration is set 
in the container. Might look like:
   
   Here is a case where hostname resolution is not assigned
   
   in /etc/hosts,Only the following records:  
   
   ```shell
   ::1     localhost       localhost.localdomain   localhost6      
localhost6.localdomain6
   127.0.0.1       localhost       localhost.localdomain   localhost4      
localhost4.localdomain4
   ```
   
   In this case, if there is no default bound device, GetHostName() will 
reverse resolve the host name 127.0.0.1 to "localhost", which is usually not 
what we expected.
   
   So we may be able to make some improvements and perform a second detection 
for the case of returning "localhost", for example, from the linux kernel 
information:
   
   
   ```java
   // simple demo .When hostname is localhost, check again.
   public static String getHostName() {
       if (HOST_NAME == null) {
           try {
               InetAddress host = InetAddress.getLocalHost();
               HOST_NAME = host.getHostName();
           } catch (UnknownHostException e) {
               HOST_NAME = "unknown";
           }
       }
   
       if (HOST_NAME == "localhost"){
           try{
               HOST_NAME = new 
String(Files.readAllBytes(Paths.get("/proc/sys/kernel/hostname")), 
StandardCharsets.UTF_8);
           } catch (Exception e) {
               HOST_NAME = "unknown";
           }
       }
       return HOST_NAME;
   }
   
   ```
   
   
   
   Maybe things will change. Looking forward to feedback : ) .
   
   

----------------------------------------------------------------
This is an automated message from the Apache Git Service.
To respond to the message, please log on to GitHub and use the
URL above to go to the specific comment.
 
For queries about this service, please contact Infrastructure at:
us...@infra.apache.org


With regards,
Apache Git Services

Reply via email to