Skip to content

Commit 0a353ec

Browse files
authored
Merge pull request #830 from scouter-project/develop
Develop
2 parents ae16506 + 01c199e commit 0a353ec

File tree

21 files changed

+1714
-319
lines changed

21 files changed

+1714
-319
lines changed

scouter.agent.host/src/main/java/scouter/agent/Configure.java

Lines changed: 66 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -48,6 +48,7 @@
4848
import java.util.HashSet;
4949
import java.util.Map;
5050
import java.util.Properties;
51+
import java.util.concurrent.ThreadLocalRandom;
5152

5253
public class Configure extends Thread {
5354

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

68+
private static long randomNo = ThreadLocalRandom.current().nextLong(100000L, 999999L);
69+
private long seqNoForKube = -1;
70+
private boolean useKubeHostName = false;
71+
private String podName = "";
72+
73+
public long getSeqNoForKube() {
74+
return seqNoForKube;
75+
}
76+
77+
public void setSeqNoForKube(long seqNoForKube) {
78+
this.seqNoForKube = seqNoForKube;
79+
}
80+
81+
public boolean isUseKubeHostName() {
82+
return useKubeHostName;
83+
}
84+
85+
public String getPodName() {
86+
return podName;
87+
}
88+
6789
//Network
6890
@ConfigDesc("UDP local IP")
6991
public String net_local_udp_ip = null;
@@ -92,6 +114,16 @@ public final static synchronized Configure getInstance() {
92114
@ConfigDesc("Object Name")
93115
public String obj_name = "";
94116

117+
public String host_name = StringUtil.emptyToDefault(SysJMX.getHostName(), "host_" + randomNo);
118+
119+
@ConfigDesc("is it on kube env. auto detecting.")
120+
public boolean kube = isKube();
121+
//KUBERNETES_SERVICE_HOST
122+
@ConfigDesc("use sequencial name on kube. {obj_name}_{seq}")
123+
public boolean kube_pod_sequence_name_enabled = true;
124+
@ConfigDesc("use just hostname as obj_name if do not exceed this number of hostname size.")
125+
public int kube_pod_sequence_name_min_length = 18;
126+
95127
//Manager
96128
@ConfigDesc("")
97129
@ConfigValueType(ValueType.COMMA_SEPARATED_VALUE)
@@ -301,10 +333,38 @@ public synchronized void resetObjInfo() {
301333
this.monitoring_group_type = getValue("monitoring_group_type");
302334
this.obj_type = StringUtil.isEmpty(this.monitoring_group_type) ? getValue("obj_type", detected) : this.monitoring_group_type;
303335

304-
this.obj_name = getValue("obj_name", SysJMX.getHostName());
336+
this.kube = getBoolean("kube", isKube());
337+
this.kube_pod_sequence_name_enabled = getBoolean("kube_pod_sequence_name_enabled", true);
338+
this.kube_pod_sequence_name_min_length = getInt("kube_pod_sequence_name_min_length", 18);
339+
340+
String hostNameForTest = getValue("test_host_name", host_name);
341+
host_name = hostNameForTest;
342+
String applyingObjName = getValue("obj_name", host_name);
343+
344+
boolean tmpUseKubeHostName = false;
345+
if (kube && kube_pod_sequence_name_enabled && host_name.length() > kube_pod_sequence_name_min_length) {
346+
String[] split = host_name.split("-");
347+
if (split.length > 2) {
348+
tmpUseKubeHostName = true;
349+
StringBuilder builder = new StringBuilder();
350+
builder.append(split[0]);
351+
for (int i = 1; i < split.length - 2; i++) {
352+
builder.append('-');
353+
builder.append(split[i]);
354+
}
355+
podName = getValue("obj_name", builder.toString());
356+
if (seqNoForKube > -1) {
357+
applyingObjName = podName + "_" + seqNoForKube;
358+
} else {
359+
applyingObjName = podName + "_rnd" + randomNo;
360+
}
361+
}
362+
}
305363

364+
this.obj_name = applyingObjName;
306365
this.objName = "/" + this.obj_name;
307366
this.objHash = HashUtil.hash(objName);
367+
this.useKubeHostName = tmpUseKubeHostName;
308368

309369
}
310370

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

506+
private boolean isKube() {
507+
Properties properties = System.getProperties();
508+
return !StringUtil.isEmpty(properties.getProperty("KUBERNETES_SERVICE_HOST"));
509+
}
510+
446511
public static void main(String[] args) {
447512
Configure o = new Configure(true);
448513
StringKeyLinkedMap<Object> defMap = ConfigValueUtil.getConfigDefault(o);

scouter.agent.host/src/main/java/scouter/agent/counter/task/AgentHeartBeat.java

Lines changed: 43 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -24,12 +24,19 @@
2424
import scouter.agent.netio.data.HostAgentDataProxy;
2525
import scouter.agent.netio.data.net.TcpWorker;
2626
import scouter.lang.pack.ObjectPack;
27+
import scouter.lang.value.BooleanValue;
28+
import scouter.util.FileUtil;
2729
import scouter.util.StringKeyLinkedMap;
2830
import scouter.util.StringUtil;
2931

32+
import java.io.File;
3033
import java.util.Enumeration;
3134

35+
import static scouter.lang.constants.ScouterConstants.HOST_NAME;
36+
import static scouter.lang.constants.ScouterConstants.KUBE_SEQ;
37+
import static scouter.lang.constants.ScouterConstants.POD_NAME;
3238
import static scouter.lang.constants.ScouterConstants.TAG_OBJ_DETECTED_TYPE;
39+
import static scouter.lang.constants.ScouterConstants.USE_KUBE_SEQ;
3340

3441
public class AgentHeartBeat {
3542
public AgentHeartBeat() {
@@ -60,6 +67,37 @@ public void alive(CounterBasket pw) {
6067
}
6168
}
6269

70+
@Counter
71+
public void writeHostNameForKube(CounterBasket pw) {
72+
Configure conf = Configure.getInstance();
73+
if (conf.isUseKubeHostName() && conf.getSeqNoForKube() > -1) {
74+
long seqNoForKube = conf.getSeqNoForKube();
75+
File dir = new File(conf.counter_object_registry_path);
76+
File file = new File(dir, seqNoForKube + ".scouterkubeseq");
77+
if (dir.canWrite()) {
78+
FileUtil.save(file, conf.obj_name.getBytes());
79+
}
80+
81+
} else {
82+
File dir = new File(conf.counter_object_registry_path);
83+
if (dir == null)
84+
return;
85+
86+
File[] files = dir.listFiles();
87+
for (int i = 0; i < files.length; i++) {
88+
if (files[i].isDirectory())
89+
continue;
90+
String name = files[i].getName();
91+
if (!name.endsWith(".scouterkubeseq")) {
92+
continue;
93+
}
94+
if (files[i].canWrite()) {
95+
files[i].delete();
96+
}
97+
}
98+
}
99+
}
100+
63101
private ObjectPack getMainObject() {
64102
Configure conf = Configure.getInstance();
65103
ObjectPack p = new ObjectPack();
@@ -74,6 +112,11 @@ private ObjectPack getMainObject() {
74112
p.tags.put(TAG_OBJ_DETECTED_TYPE, conf.getObjDetectedType());
75113
}
76114

115+
p.tags.put(HOST_NAME, conf.host_name);
116+
p.tags.put(POD_NAME, conf.getPodName());
117+
p.tags.put(KUBE_SEQ, conf.getSeqNoForKube());
118+
p.tags.put(USE_KUBE_SEQ, new BooleanValue(conf.isUseKubeHostName()));
119+
77120
return p;
78121
}
79122

scouter.agent.host/src/main/java/scouter/agent/netio/request/handle/HostAgentEnv.java

Lines changed: 20 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -16,19 +16,22 @@
1616
*/
1717
package scouter.agent.netio.request.handle;
1818

19-
import java.util.Enumeration;
20-
import java.util.Map;
21-
import java.util.Properties;
22-
import java.util.Set;
23-
19+
import scouter.agent.Configure;
2420
import scouter.agent.netio.request.anotation.RequestHandler;
2521
import scouter.lang.pack.MapPack;
2622
import scouter.lang.pack.Pack;
2723
import scouter.lang.value.TextValue;
2824
import scouter.net.RequestCmd;
2925

26+
import java.util.Enumeration;
27+
import java.util.Map;
28+
import java.util.Properties;
29+
import java.util.Set;
30+
3031
public class HostAgentEnv {
3132

33+
private static Configure conf = Configure.getInstance();
34+
3235
@RequestHandler(RequestCmd.OBJECT_ENV)
3336
public Pack getAgentEnv(Pack param) {
3437
MapPack m = new MapPack();
@@ -47,4 +50,16 @@ public Pack getAgentEnv(Pack param) {
4750
}
4851
return m;
4952
}
53+
54+
@RequestHandler(RequestCmd.OBJECT_SET_KUBE_SEQ)
55+
public Pack setKubePodSeq(Pack param) {
56+
long seq = ((MapPack)param).getLong("seq");
57+
if (seq != conf.getSeqNoForKube()) {
58+
conf.setSeqNoForKube(seq);
59+
conf.resetObjInfo();
60+
}
61+
62+
return new MapPack();
63+
}
64+
5065
}

scouter.agent.java/src/main/java/scouter/agent/Configure.java

Lines changed: 85 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -118,6 +118,10 @@ public static final Configure getInstance() {
118118
@ConfigDesc("Activating collect sub counters using JMX")
119119
public boolean jmx_counter_enabled = false;
120120

121+
@ConfigDesc("is it on kube env. auto detecting.")
122+
public boolean kube = isKube();
123+
public boolean obj_host_name_follow_host_agent = true;
124+
121125
//profile
122126
@ConfigDesc("Http Query String profile")
123127
public boolean profile_http_querystring_enabled;
@@ -1446,7 +1450,19 @@ public synchronized void resetObjInfo() {
14461450
detected = CounterConstants.HPUX;
14471451
}
14481452
this.obj_host_type = getValue("obj_host_type", detected);
1449-
this.obj_host_name = getValue("obj_host_name", SysJMX.getHostName());
1453+
1454+
this.kube = getBoolean("kube", isKube());
1455+
this.obj_host_name_follow_host_agent = getBoolean("obj_host_name_follow_host_agent", true);
1456+
1457+
String tempObjHostName = getValue("obj_host_name", SysJMX.getHostName());
1458+
if (kube && obj_host_name_follow_host_agent) {
1459+
String nameFromHost = readHostNameFromHostAgent();
1460+
if (StringUtil.isNotEmpty(nameFromHost) && nameFromHost.length() > 1 && nameFromHost.length() < 100) {
1461+
tempObjHostName = nameFromHost;
1462+
}
1463+
}
1464+
1465+
this.obj_host_name = tempObjHostName;
14501466
this.objHostName = "/" + this.obj_host_name;
14511467
this.objHostHash = HashUtil.hash(objHostName);
14521468
this.obj_name_auto_pid_enabled = getBoolean("obj_name_auto_pid_enabled", false);
@@ -1588,6 +1604,74 @@ public int getHookSignature() {
15881604
return this.hook_signature;
15891605
}
15901606

1607+
private File regRoot = null;
1608+
1609+
public void initTmpDir() {
1610+
try {
1611+
if (regRoot != null) {
1612+
return;
1613+
}
1614+
String objReg = counter_object_registry_path;
1615+
File objRegFile = new File(objReg);
1616+
if (!objRegFile.canRead()) {
1617+
objRegFile.mkdirs();
1618+
}
1619+
if (objRegFile.exists()) {
1620+
regRoot = objRegFile;
1621+
} else {
1622+
regRoot = null;
1623+
}
1624+
} catch (Exception e) {
1625+
e.printStackTrace();
1626+
}
1627+
}
1628+
1629+
private boolean isKube() {
1630+
Properties properties = System.getProperties();
1631+
return !StringUtil.isEmpty(properties.getProperty("KUBERNETES_SERVICE_HOST"));
1632+
}
1633+
1634+
private String readHostNameFromHostAgent() {
1635+
try {
1636+
initTmpDir();
1637+
File dir = regRoot;
1638+
if (dir == null)
1639+
return null;
1640+
1641+
File[] kubeHostSeq = dir.listFiles();
1642+
for (int i = 0; i < kubeHostSeq.length; i++) {
1643+
if (kubeHostSeq[i].isDirectory())
1644+
continue;
1645+
String name = kubeHostSeq[i].getName();
1646+
if (!name.endsWith(".scouterkubeseq")) {
1647+
continue;
1648+
}
1649+
int kubeSeq = cintErrorMinusOne(name.substring(0, name.lastIndexOf(".")));
1650+
if (kubeSeq < 0)
1651+
continue;
1652+
1653+
return new String(FileUtil.readAll(kubeHostSeq[i]));
1654+
}
1655+
return null;
1656+
} catch (Exception e) {
1657+
e.printStackTrace();
1658+
}
1659+
return null;
1660+
}
1661+
1662+
public static int cintErrorMinusOne(String value) {
1663+
if (value == null) {
1664+
return -1;
1665+
} else {
1666+
try {
1667+
return Integer.parseInt(value);
1668+
} catch (Exception e) {
1669+
return -1;
1670+
}
1671+
}
1672+
}
1673+
1674+
15911675
public static void main(String[] args) {
15921676
Configure o = new Configure(true);
15931677
StringKeyLinkedMap<Object> defMap = ConfigValueUtil.getConfigDefault(o);

scouter.agent.java/src/main/java/scouter/agent/asm/weaver/WeaverClassASM.java

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -44,7 +44,7 @@ public ClassVisitor transform(ClassVisitor cv, String className, ClassDesc class
4444
return cv;
4545
}
4646

47-
if ("scouterx/weaver/ScouterTraceSupport$ScouterTraceSupport0".equalsIgnoreCase(className)) {
47+
if ("scouterx/weaver/Scouter$Weaving".equalsIgnoreCase(className)) {
4848
TraceSupportWeave.touch();
4949
Method[] weaveMethods = TraceSupportWeave.class.getDeclaredMethods();
5050
for (int i = 0; i < weaveMethods.length; i++) {

scouter.agent.java/src/main/java/scouter/agent/netio/data/net/TcpRequestMgr.java

Lines changed: 13 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,13 +1,15 @@
11
package scouter.agent.netio.data.net;
22

3-
import java.util.concurrent.Executor;
4-
53
import scouter.agent.Configure;
4+
import scouter.util.IntEnumer;
65
import scouter.util.ThreadUtil;
76

7+
import java.util.concurrent.Executor;
8+
89
public class TcpRequestMgr extends Thread {
910

1011
private static TcpRequestMgr instance;
12+
private static Configure conf = Configure.getInstance();
1113

1214
public static synchronized TcpRequestMgr getInstance() {
1315
if (instance == null) {
@@ -40,6 +42,15 @@ public void run() {
4042
TcpWorker w = TcpWorker.LIVE.removeFirst();
4143
w.close();
4244
}
45+
46+
IntEnumer keys = TcpWorker.LIVE.keys();
47+
while (keys.hasMoreElements()) {
48+
int key = keys.nextInt();
49+
TcpWorker w = TcpWorker.LIVE.get(key);
50+
if (w.objHash != conf.getObjHash()) {
51+
w.close();
52+
}
53+
}
4354
} catch (Throwable t) {
4455
}
4556
}

scouter.agent.java/src/main/java/scouter/agent/trace/TraceMain.java

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -881,6 +881,8 @@ public static void endService(Object stat, Object returnValue, Throwable thr) {
881881

882882
DataProxy.sendServiceName(ctx.serviceHash, ctx.serviceName);
883883
pack.service = ctx.serviceHash;
884+
pack.userAgent = ctx.userAgent;
885+
pack.referer = ctx.referer;
884886
pack.threadNameHash = DataProxy.sendHashedMessage(ctx.threadName);
885887
pack.xType = ctx.xType;
886888

0 commit comments

Comments
 (0)