View Javadoc
1   /*
2    * Licensed to the Apache Software Foundation (ASF) under one or more
3    * contributor license agreements. See the NOTICE file distributed with
4    * this work for additional information regarding copyright ownership.
5    * The ASF licenses this file to You under the Apache license, Version 2.0
6    * (the "License"); you may not use this file except in compliance with
7    * the License. You may obtain a copy of the License at
8    *
9    *      http://www.apache.org/licenses/LICENSE-2.0
10   *
11   * Unless required by applicable law or agreed to in writing, software
12   * distributed under the License is distributed on an "AS IS" BASIS,
13   * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
14   * See the license for the specific language governing permissions and
15   * limitations under the license.
16   */
17  package org.apache.logging.log4j.docker;
18  
19  import java.io.IOException;
20  import java.net.URL;
21  import java.util.List;
22  import java.util.Map;
23  
24  import org.apache.logging.log4j.Logger;
25  import org.apache.logging.log4j.core.LogEvent;
26  import org.apache.logging.log4j.core.config.plugins.Plugin;
27  import org.apache.logging.log4j.core.lookup.AbstractLookup;
28  import org.apache.logging.log4j.core.lookup.StrLookup;
29  import org.apache.logging.log4j.core.util.NetUtils;
30  import org.apache.logging.log4j.docker.model.Container;
31  import org.apache.logging.log4j.docker.model.Network;
32  import org.apache.logging.log4j.status.StatusLogger;
33  import org.apache.logging.log4j.util.PropertiesUtil;
34  import com.fasterxml.jackson.core.type.TypeReference;
35  import com.fasterxml.jackson.databind.ObjectMapper;
36  
37  /**
38   *
39   */
40  @Plugin(name = "docker", category = StrLookup.CATEGORY)
41  public class DockerLookup extends AbstractLookup {
42  
43      private static final Logger LOGGER = StatusLogger.getLogger();
44      private static final String DOCKER_URI = "DOCKER_URI";
45      private static final String HTTP = "http";
46      private final Container container;
47  
48      public DockerLookup() {
49          String baseUri = System.getenv(DOCKER_URI);
50          if (baseUri == null) {
51              PropertiesUtil props = PropertiesUtil.getProperties();
52              baseUri = props.getStringProperty(DOCKER_URI);
53          }
54          if (baseUri == null) {
55              LOGGER.warn("No Docker URI provided. Docker information is unavailble");
56          }
57          Container current = null;
58          try {
59              URL url= new URL(baseUri + "/containers/json");
60              String hostName = NetUtils.getLocalHostname();
61              String macAddr = NetUtils.getMacAddressString();
62  
63              if (url.getProtocol().equals(HTTP)) {
64                  ObjectMapper objectMapper = new ObjectMapper();
65                  List<Container> containerList = objectMapper.readValue(url, new TypeReference<List<Container>>(){});
66  
67                  for (Container container : containerList) {
68                      if (macAddr != null && container.getNetworkSettings() != null) {
69                          Map<String, Network> networks = container.getNetworkSettings().getNetworks();
70                          if (networks != null) {
71                              for (Network network: networks.values()) {
72                                  if (macAddr.equals(network.getMacAddress())) {
73                                      current = container;
74                                      break;
75                                  }
76                              }
77                          }
78                      }
79                      if (current != null) {
80                          break;
81                      }
82                  }
83              }
84              if (current == null) {
85                  LOGGER.warn("Unable to determine current container");
86              }
87          } catch (IOException ioe) {
88              LOGGER.warn("Unable to read container information: " + ioe.getMessage());
89          }
90          container = current;
91      }
92  
93      @Override
94      public String lookup(LogEvent event, String key) {
95          if (container == null) {
96              return null;
97          }
98          switch (key) {
99              case "shortContainerId": {
100                 return container.getId().substring(0, 12);
101             }
102             case "containerId": {
103                 return container.getId();
104             }
105             case "containerName": {
106                 if (container.getNames().size() > 1) {
107                     return container.getNames().toString();
108                 }
109                 return container.getNames().get(0);
110             }
111             case "shortImageId": {
112                 return container.getImageId().substring(0, 12);
113             }
114             case "imageId": {
115                 return container.getImageId();
116             }
117             case "imageName": {
118                 return container.getImage();
119             }
120             default:
121                 return null;
122         }
123     }
124 }