1 /* 2 * Copyright 2018, OpenCensus Authors 3 * 4 * Licensed under the Apache License, Version 2.0 (the "License"); 5 * you may not use this file except in compliance with the License. 6 * You may obtain a copy of the License at 7 * 8 * http://www.apache.org/licenses/LICENSE-2.0 9 * 10 * Unless required by applicable law or agreed to in writing, software 11 * distributed under the License is distributed on an "AS IS" BASIS, 12 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. 13 * See the License for the specific language governing permissions and 14 * limitations under the License. 15 */ 16 17 package io.opencensus.exporter.trace.ocagent; 18 19 import com.google.common.annotations.VisibleForTesting; 20 import io.opencensus.common.OpenCensusLibraryInformation; 21 import io.opencensus.common.Timestamp; 22 import io.opencensus.contrib.resource.util.ResourceUtils; 23 import io.opencensus.proto.agent.common.v1.LibraryInfo; 24 import io.opencensus.proto.agent.common.v1.LibraryInfo.Language; 25 import io.opencensus.proto.agent.common.v1.Node; 26 import io.opencensus.proto.agent.common.v1.ProcessIdentifier; 27 import io.opencensus.proto.agent.common.v1.ServiceInfo; 28 import io.opencensus.proto.resource.v1.Resource; 29 import java.lang.management.ManagementFactory; 30 import java.net.InetAddress; 31 import java.net.UnknownHostException; 32 import java.security.SecureRandom; 33 import java.util.Map.Entry; 34 import javax.annotation.Nullable; 35 36 /** Utilities for detecting and creating {@link Node}. */ 37 final class OcAgentNodeUtils { 38 39 // The current version of the OpenCensus OC-Agent Exporter. 40 @VisibleForTesting 41 static final String OC_AGENT_EXPORTER_VERSION = "0.32.0-SNAPSHOT"; // CURRENT_OPENCENSUS_VERSION 42 43 @Nullable 44 private static final io.opencensus.resource.Resource AUTO_DETECTED_RESOURCE = 45 ResourceUtils.detectResource(); 46 47 // Creates a Node with information from the OpenCensus library and environment variables. getNodeInfo(String serviceName)48 static Node getNodeInfo(String serviceName) { 49 String jvmName = ManagementFactory.getRuntimeMXBean().getName(); 50 Timestamp censusTimestamp = Timestamp.fromMillis(System.currentTimeMillis()); 51 return Node.newBuilder() 52 .setIdentifier(getProcessIdentifier(jvmName, censusTimestamp)) 53 .setLibraryInfo(getLibraryInfo(OpenCensusLibraryInformation.VERSION)) 54 .setServiceInfo(getServiceInfo(serviceName)) 55 .build(); 56 } 57 58 // Creates process identifier with the given JVM name and start time. 59 @VisibleForTesting getProcessIdentifier(String jvmName, Timestamp censusTimestamp)60 static ProcessIdentifier getProcessIdentifier(String jvmName, Timestamp censusTimestamp) { 61 String hostname; 62 int pid; 63 // jvmName should be something like '<pid>@<hostname>', at least in Oracle and OpenJdk JVMs 64 int delimiterIndex = jvmName.indexOf('@'); 65 if (delimiterIndex < 1) { 66 // Not the expected format, generate a random number. 67 try { 68 hostname = InetAddress.getLocalHost().getHostName(); 69 } catch (UnknownHostException e) { 70 hostname = "localhost"; 71 } 72 // Generate a random number as the PID. 73 pid = new SecureRandom().nextInt(); 74 } else { 75 hostname = jvmName.substring(delimiterIndex + 1, jvmName.length()); 76 try { 77 pid = Integer.parseInt(jvmName.substring(0, delimiterIndex)); 78 } catch (NumberFormatException e) { 79 // Generate a random number as the PID if format is unexpected. 80 pid = new SecureRandom().nextInt(); 81 } 82 } 83 84 return ProcessIdentifier.newBuilder() 85 .setHostName(hostname) 86 .setPid(pid) 87 .setStartTimestamp(TraceProtoUtils.toTimestampProto(censusTimestamp)) 88 .build(); 89 } 90 91 // Creates library info with the given OpenCensus Java version. 92 @VisibleForTesting getLibraryInfo(String currentOcJavaVersion)93 static LibraryInfo getLibraryInfo(String currentOcJavaVersion) { 94 return LibraryInfo.newBuilder() 95 .setLanguage(Language.JAVA) 96 .setCoreLibraryVersion(currentOcJavaVersion) 97 .setExporterVersion(OC_AGENT_EXPORTER_VERSION) 98 .build(); 99 } 100 101 // Creates service info with the given service name. 102 @VisibleForTesting getServiceInfo(String serviceName)103 static ServiceInfo getServiceInfo(String serviceName) { 104 return ServiceInfo.newBuilder().setName(serviceName).build(); 105 } 106 107 @Nullable getAutoDetectedResourceProto()108 static Resource getAutoDetectedResourceProto() { 109 return toResourceProto(AUTO_DETECTED_RESOURCE); 110 } 111 112 // Converts a Java Resource object to a Resource proto. 113 @Nullable 114 @VisibleForTesting toResourceProto(@ullable io.opencensus.resource.Resource resource)115 static Resource toResourceProto(@Nullable io.opencensus.resource.Resource resource) { 116 if (resource == null || resource.getType() == null) { 117 return null; 118 } else { 119 Resource.Builder resourceProtoBuilder = Resource.newBuilder(); 120 resourceProtoBuilder.setType(resource.getType()); 121 for (Entry<String, String> keyValuePairs : resource.getLabels().entrySet()) { 122 resourceProtoBuilder.putLabels(keyValuePairs.getKey(), keyValuePairs.getValue()); 123 } 124 return resourceProtoBuilder.build(); 125 } 126 } 127 OcAgentNodeUtils()128 private OcAgentNodeUtils() {} 129 } 130