Skip to content

Commit

Permalink
Merge pull request #830 from scouter-project/develop
Browse files Browse the repository at this point in the history
Develop
  • Loading branch information
gunlee01 committed Oct 24, 2021
2 parents ae16506 + 01c199e commit 0a353ec
Show file tree
Hide file tree
Showing 21 changed files with 1,714 additions and 319 deletions.
67 changes: 66 additions & 1 deletion scouter.agent.host/src/main/java/scouter/agent/Configure.java
Original file line number Diff line number Diff line change
Expand Up @@ -48,6 +48,7 @@
import java.util.HashSet;
import java.util.Map;
import java.util.Properties;
import java.util.concurrent.ThreadLocalRandom;

public class Configure extends Thread {

Expand All @@ -64,6 +65,27 @@ public final static synchronized Configure getInstance() {
return instance;
}

private static long randomNo = ThreadLocalRandom.current().nextLong(100000L, 999999L);
private long seqNoForKube = -1;
private boolean useKubeHostName = false;
private String podName = "";

public long getSeqNoForKube() {
return seqNoForKube;
}

public void setSeqNoForKube(long seqNoForKube) {
this.seqNoForKube = seqNoForKube;
}

public boolean isUseKubeHostName() {
return useKubeHostName;
}

public String getPodName() {
return podName;
}

//Network
@ConfigDesc("UDP local IP")
public String net_local_udp_ip = null;
Expand Down Expand Up @@ -92,6 +114,16 @@ public final static synchronized Configure getInstance() {
@ConfigDesc("Object Name")
public String obj_name = "";

public String host_name = StringUtil.emptyToDefault(SysJMX.getHostName(), "host_" + randomNo);

@ConfigDesc("is it on kube env. auto detecting.")
public boolean kube = isKube();
//KUBERNETES_SERVICE_HOST
@ConfigDesc("use sequencial name on kube. {obj_name}_{seq}")
public boolean kube_pod_sequence_name_enabled = true;
@ConfigDesc("use just hostname as obj_name if do not exceed this number of hostname size.")
public int kube_pod_sequence_name_min_length = 18;

//Manager
@ConfigDesc("")
@ConfigValueType(ValueType.COMMA_SEPARATED_VALUE)
Expand Down Expand Up @@ -301,10 +333,38 @@ public synchronized void resetObjInfo() {
this.monitoring_group_type = getValue("monitoring_group_type");
this.obj_type = StringUtil.isEmpty(this.monitoring_group_type) ? getValue("obj_type", detected) : this.monitoring_group_type;

this.obj_name = getValue("obj_name", SysJMX.getHostName());
this.kube = getBoolean("kube", isKube());
this.kube_pod_sequence_name_enabled = getBoolean("kube_pod_sequence_name_enabled", true);
this.kube_pod_sequence_name_min_length = getInt("kube_pod_sequence_name_min_length", 18);

String hostNameForTest = getValue("test_host_name", host_name);
host_name = hostNameForTest;
String applyingObjName = getValue("obj_name", host_name);

boolean tmpUseKubeHostName = false;
if (kube && kube_pod_sequence_name_enabled && host_name.length() > kube_pod_sequence_name_min_length) {
String[] split = host_name.split("-");
if (split.length > 2) {
tmpUseKubeHostName = true;
StringBuilder builder = new StringBuilder();
builder.append(split[0]);
for (int i = 1; i < split.length - 2; i++) {
builder.append('-');
builder.append(split[i]);
}
podName = getValue("obj_name", builder.toString());
if (seqNoForKube > -1) {
applyingObjName = podName + "_" + seqNoForKube;
} else {
applyingObjName = podName + "_rnd" + randomNo;
}
}
}

this.obj_name = applyingObjName;
this.objName = "/" + this.obj_name;
this.objHash = HashUtil.hash(objName);
this.useKubeHostName = tmpUseKubeHostName;

}

Expand Down Expand Up @@ -443,6 +503,11 @@ public StringKeyLinkedMap<ValueType> getConfigureValueType() {
return ConfigValueUtil.getConfigValueTypeMap(this);
}

private boolean isKube() {
Properties properties = System.getProperties();
return !StringUtil.isEmpty(properties.getProperty("KUBERNETES_SERVICE_HOST"));
}

public static void main(String[] args) {
Configure o = new Configure(true);
StringKeyLinkedMap<Object> defMap = ConfigValueUtil.getConfigDefault(o);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -24,12 +24,19 @@
import scouter.agent.netio.data.HostAgentDataProxy;
import scouter.agent.netio.data.net.TcpWorker;
import scouter.lang.pack.ObjectPack;
import scouter.lang.value.BooleanValue;
import scouter.util.FileUtil;
import scouter.util.StringKeyLinkedMap;
import scouter.util.StringUtil;

import java.io.File;
import java.util.Enumeration;

import static scouter.lang.constants.ScouterConstants.HOST_NAME;
import static scouter.lang.constants.ScouterConstants.KUBE_SEQ;
import static scouter.lang.constants.ScouterConstants.POD_NAME;
import static scouter.lang.constants.ScouterConstants.TAG_OBJ_DETECTED_TYPE;
import static scouter.lang.constants.ScouterConstants.USE_KUBE_SEQ;

public class AgentHeartBeat {
public AgentHeartBeat() {
Expand Down Expand Up @@ -60,6 +67,37 @@ public void alive(CounterBasket pw) {
}
}

@Counter
public void writeHostNameForKube(CounterBasket pw) {
Configure conf = Configure.getInstance();
if (conf.isUseKubeHostName() && conf.getSeqNoForKube() > -1) {
long seqNoForKube = conf.getSeqNoForKube();
File dir = new File(conf.counter_object_registry_path);
File file = new File(dir, seqNoForKube + ".scouterkubeseq");
if (dir.canWrite()) {
FileUtil.save(file, conf.obj_name.getBytes());
}

} else {
File dir = new File(conf.counter_object_registry_path);
if (dir == null)
return;

File[] files = dir.listFiles();
for (int i = 0; i < files.length; i++) {
if (files[i].isDirectory())
continue;
String name = files[i].getName();
if (!name.endsWith(".scouterkubeseq")) {
continue;
}
if (files[i].canWrite()) {
files[i].delete();
}
}
}
}

private ObjectPack getMainObject() {
Configure conf = Configure.getInstance();
ObjectPack p = new ObjectPack();
Expand All @@ -74,6 +112,11 @@ private ObjectPack getMainObject() {
p.tags.put(TAG_OBJ_DETECTED_TYPE, conf.getObjDetectedType());
}

p.tags.put(HOST_NAME, conf.host_name);
p.tags.put(POD_NAME, conf.getPodName());
p.tags.put(KUBE_SEQ, conf.getSeqNoForKube());
p.tags.put(USE_KUBE_SEQ, new BooleanValue(conf.isUseKubeHostName()));

return p;
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -16,19 +16,22 @@
*/
package scouter.agent.netio.request.handle;

import java.util.Enumeration;
import java.util.Map;
import java.util.Properties;
import java.util.Set;

import scouter.agent.Configure;
import scouter.agent.netio.request.anotation.RequestHandler;
import scouter.lang.pack.MapPack;
import scouter.lang.pack.Pack;
import scouter.lang.value.TextValue;
import scouter.net.RequestCmd;

import java.util.Enumeration;
import java.util.Map;
import java.util.Properties;
import java.util.Set;

public class HostAgentEnv {

private static Configure conf = Configure.getInstance();

@RequestHandler(RequestCmd.OBJECT_ENV)
public Pack getAgentEnv(Pack param) {
MapPack m = new MapPack();
Expand All @@ -47,4 +50,16 @@ public Pack getAgentEnv(Pack param) {
}
return m;
}

@RequestHandler(RequestCmd.OBJECT_SET_KUBE_SEQ)
public Pack setKubePodSeq(Pack param) {
long seq = ((MapPack)param).getLong("seq");
if (seq != conf.getSeqNoForKube()) {
conf.setSeqNoForKube(seq);
conf.resetObjInfo();
}

return new MapPack();
}

}
86 changes: 85 additions & 1 deletion scouter.agent.java/src/main/java/scouter/agent/Configure.java
Original file line number Diff line number Diff line change
Expand Up @@ -118,6 +118,10 @@ public static final Configure getInstance() {
@ConfigDesc("Activating collect sub counters using JMX")
public boolean jmx_counter_enabled = false;

@ConfigDesc("is it on kube env. auto detecting.")
public boolean kube = isKube();
public boolean obj_host_name_follow_host_agent = true;

//profile
@ConfigDesc("Http Query String profile")
public boolean profile_http_querystring_enabled;
Expand Down Expand Up @@ -1446,7 +1450,19 @@ public synchronized void resetObjInfo() {
detected = CounterConstants.HPUX;
}
this.obj_host_type = getValue("obj_host_type", detected);
this.obj_host_name = getValue("obj_host_name", SysJMX.getHostName());

this.kube = getBoolean("kube", isKube());
this.obj_host_name_follow_host_agent = getBoolean("obj_host_name_follow_host_agent", true);

String tempObjHostName = getValue("obj_host_name", SysJMX.getHostName());
if (kube && obj_host_name_follow_host_agent) {
String nameFromHost = readHostNameFromHostAgent();
if (StringUtil.isNotEmpty(nameFromHost) && nameFromHost.length() > 1 && nameFromHost.length() < 100) {
tempObjHostName = nameFromHost;
}
}

this.obj_host_name = tempObjHostName;
this.objHostName = "/" + this.obj_host_name;
this.objHostHash = HashUtil.hash(objHostName);
this.obj_name_auto_pid_enabled = getBoolean("obj_name_auto_pid_enabled", false);
Expand Down Expand Up @@ -1588,6 +1604,74 @@ public int getHookSignature() {
return this.hook_signature;
}

private File regRoot = null;

public void initTmpDir() {
try {
if (regRoot != null) {
return;
}
String objReg = counter_object_registry_path;
File objRegFile = new File(objReg);
if (!objRegFile.canRead()) {
objRegFile.mkdirs();
}
if (objRegFile.exists()) {
regRoot = objRegFile;
} else {
regRoot = null;
}
} catch (Exception e) {
e.printStackTrace();
}
}

private boolean isKube() {
Properties properties = System.getProperties();
return !StringUtil.isEmpty(properties.getProperty("KUBERNETES_SERVICE_HOST"));
}

private String readHostNameFromHostAgent() {
try {
initTmpDir();
File dir = regRoot;
if (dir == null)
return null;

File[] kubeHostSeq = dir.listFiles();
for (int i = 0; i < kubeHostSeq.length; i++) {
if (kubeHostSeq[i].isDirectory())
continue;
String name = kubeHostSeq[i].getName();
if (!name.endsWith(".scouterkubeseq")) {
continue;
}
int kubeSeq = cintErrorMinusOne(name.substring(0, name.lastIndexOf(".")));
if (kubeSeq < 0)
continue;

return new String(FileUtil.readAll(kubeHostSeq[i]));
}
return null;
} catch (Exception e) {
e.printStackTrace();
}
return null;
}

public static int cintErrorMinusOne(String value) {
if (value == null) {
return -1;
} else {
try {
return Integer.parseInt(value);
} catch (Exception e) {
return -1;
}
}
}


public static void main(String[] args) {
Configure o = new Configure(true);
StringKeyLinkedMap<Object> defMap = ConfigValueUtil.getConfigDefault(o);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -44,7 +44,7 @@ public ClassVisitor transform(ClassVisitor cv, String className, ClassDesc class
return cv;
}

if ("scouterx/weaver/ScouterTraceSupport$ScouterTraceSupport0".equalsIgnoreCase(className)) {
if ("scouterx/weaver/Scouter$Weaving".equalsIgnoreCase(className)) {
TraceSupportWeave.touch();
Method[] weaveMethods = TraceSupportWeave.class.getDeclaredMethods();
for (int i = 0; i < weaveMethods.length; i++) {
Expand Down
Original file line number Diff line number Diff line change
@@ -1,13 +1,15 @@
package scouter.agent.netio.data.net;

import java.util.concurrent.Executor;

import scouter.agent.Configure;
import scouter.util.IntEnumer;
import scouter.util.ThreadUtil;

import java.util.concurrent.Executor;

public class TcpRequestMgr extends Thread {

private static TcpRequestMgr instance;
private static Configure conf = Configure.getInstance();

public static synchronized TcpRequestMgr getInstance() {
if (instance == null) {
Expand Down Expand Up @@ -40,6 +42,15 @@ public void run() {
TcpWorker w = TcpWorker.LIVE.removeFirst();
w.close();
}

IntEnumer keys = TcpWorker.LIVE.keys();
while (keys.hasMoreElements()) {
int key = keys.nextInt();
TcpWorker w = TcpWorker.LIVE.get(key);
if (w.objHash != conf.getObjHash()) {
w.close();
}
}
} catch (Throwable t) {
}
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -881,6 +881,8 @@ public static void endService(Object stat, Object returnValue, Throwable thr) {

DataProxy.sendServiceName(ctx.serviceHash, ctx.serviceName);
pack.service = ctx.serviceHash;
pack.userAgent = ctx.userAgent;
pack.referer = ctx.referer;
pack.threadNameHash = DataProxy.sendHashedMessage(ctx.threadName);
pack.xType = ctx.xType;

Expand Down
Loading

0 comments on commit 0a353ec

Please sign in to comment.