|  | 
| 23 | 23 | import io.opentelemetry.sdk.trace.SpanProcessor; | 
| 24 | 24 | import io.opentelemetry.semconv.resource.attributes.ResourceAttributes; | 
| 25 | 25 | import org.hypertrace.agent.otel.extensions.CgroupsReader; | 
|  | 26 | +import org.slf4j.Logger; | 
|  | 27 | +import org.slf4j.LoggerFactory; | 
| 26 | 28 | 
 | 
| 27 | 29 | public class AddTagsSpanProcessor implements SpanProcessor { | 
| 28 | 30 | 
 | 
|  | 31 | +  private static final Logger log = LoggerFactory.getLogger(AddTagsSpanProcessor.class.getName()); | 
|  | 32 | + | 
| 29 | 33 |   // initialize at startup because the processor is executed for every span. | 
| 30 |  | -  private String containerId; | 
|  | 34 | +  private final String containerId; | 
|  | 35 | +  private final String hostName; | 
| 31 | 36 | 
 | 
| 32 | 37 |   /** Note - the container id is not available using this technique if cgroup2 is installed. */ | 
| 33 | 38 |   public AddTagsSpanProcessor() { | 
| 34 | 39 |     CgroupsReader cgroupsReader = new CgroupsReader(); | 
| 35 | 40 |     containerId = cgroupsReader.readContainerId(); | 
|  | 41 | +    String hostnameEnv = ""; | 
|  | 42 | +    try { | 
|  | 43 | +      hostnameEnv = System.getenv("HOSTNAME"); | 
|  | 44 | +    } catch (SecurityException e) { | 
|  | 45 | +      log.error("could not get hostname", e); | 
|  | 46 | +    } | 
|  | 47 | +    hostName = hostnameEnv; | 
| 36 | 48 |   } | 
| 37 | 49 | 
 | 
| 38 | 50 |   @Override | 
| 39 | 51 |   public void onStart(Context parentContext, ReadWriteSpan span) { | 
| 40 | 52 |     if (containerId != null && !containerId.isEmpty()) { | 
| 41 | 53 |       span.setAttribute(ResourceAttributes.CONTAINER_ID, containerId); | 
| 42 | 54 |     } | 
|  | 55 | +    if (hostName != null && !hostName.isEmpty()) { | 
|  | 56 | +      span.setAttribute(ResourceAttributes.HOST_NAME, hostName); | 
|  | 57 | +    } | 
| 43 | 58 |   } | 
| 44 | 59 | 
 | 
| 45 | 60 |   @Override | 
|  | 
0 commit comments