clawpro-diagnostics-metrics-cls 1.0.2 → 1.0.3

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "clawpro-diagnostics-metrics-cls",
3
- "version": "1.0.2",
3
+ "version": "1.0.3",
4
4
  "description": "Cls OpenClaw diagnostics exporter: Prometheus metrics (pull/remote-write)",
5
5
  "type": "module",
6
6
  "files": [
@@ -1122,6 +1122,7 @@ async function pushMetrics(
1122
1122
  if (!mergedLabels.cvm_instance_intra_ip) mergedLabels.cvm_instance_intra_ip = metadata.cvmInstanceIntraIp;
1123
1123
  if (!mergedLabels.cvm_instance_internet_ip) mergedLabels.cvm_instance_internet_ip = metadata.cvmInstanceInternetIp;
1124
1124
  if (!mergedLabels.cvm_instance_region) mergedLabels.cvm_instance_region = metadata.cvmInstanceRegion;
1125
+ if (!mergedLabels.host_name) mergedLabels.host_name = metadata.hostName;
1125
1126
 
1126
1127
  const metricsJson = await registry.getMetricsAsJSON();
1127
1128
  const now = Date.now();
@@ -9,6 +9,7 @@
9
9
  */
10
10
 
11
11
  import http from "node:http";
12
+ import os from "node:os";
12
13
 
13
14
  /** metadata 接口基础地址 */
14
15
  const METADATA_BASE = "http://metadata.tencentyun.com/latest/meta-data";
@@ -37,6 +38,7 @@ export interface InstanceMetadata {
37
38
  cvmInstanceIntraIp: string;
38
39
  cvmInstanceInternetIp: string;
39
40
  cvmInstanceRegion: string;
41
+ hostName: string;
40
42
  }
41
43
 
42
44
  /** 默认元数据(不可变) */
@@ -46,6 +48,7 @@ const DEFAULT_METADATA: InstanceMetadata = Object.freeze({
46
48
  cvmInstanceIntraIp: "",
47
49
  cvmInstanceInternetIp: "",
48
50
  cvmInstanceRegion: "",
51
+ hostName: "",
49
52
  });
50
53
 
51
54
  /** 缓存的实例元数据(不可变对象,每次刷新时替换引用) */
@@ -160,6 +163,14 @@ async function refreshMetadata(
160
163
 
161
164
  const [idResult, nameResult, ipv4Result, publicIpv4Result, regionResult] = results;
162
165
 
166
+ // 获取本机 hostname(同步调用,不依赖网络)
167
+ let hostName = "";
168
+ try {
169
+ hostName = os.hostname();
170
+ } catch {
171
+ logger?.warn(`diagnostics-metrics/instance-metadata: 获取 hostname 失败`);
172
+ }
173
+
163
174
  // 基于当前缓存构建新值,失败的字段保留上次缓存值
164
175
  const prev = cachedMetadata;
165
176
  const updated: InstanceMetadata = {
@@ -168,6 +179,7 @@ async function refreshMetadata(
168
179
  cvmInstanceIntraIp: prev.cvmInstanceIntraIp,
169
180
  cvmInstanceInternetIp: prev.cvmInstanceInternetIp,
170
181
  cvmInstanceRegion: prev.cvmInstanceRegion,
182
+ hostName: hostName || prev.hostName,
171
183
  };
172
184
 
173
185
  if (idResult.status === "fulfilled" && idResult.value) {
@@ -212,7 +224,7 @@ async function refreshMetadata(
212
224
  const successCount = results.filter((r) => r.status === "fulfilled" && r.value).length;
213
225
 
214
226
  logger?.info(
215
- `diagnostics-metrics/instance-metadata: 当前实例信息 cvm_instance_id=${cachedMetadata.cvmInstanceId}, cvm_instance_name=${cachedMetadata.cvmInstanceName}, cvm_instance_intra_ip=${cachedMetadata.cvmInstanceIntraIp}, cvm_instance_internet_ip=${cachedMetadata.cvmInstanceInternetIp}, cvm_instance_region=${cachedMetadata.cvmInstanceRegion}`,
227
+ `diagnostics-metrics/instance-metadata: 当前实例信息 cvm_instance_id=${cachedMetadata.cvmInstanceId}, cvm_instance_name=${cachedMetadata.cvmInstanceName}, cvm_instance_intra_ip=${cachedMetadata.cvmInstanceIntraIp}, cvm_instance_internet_ip=${cachedMetadata.cvmInstanceInternetIp}, cvm_instance_region=${cachedMetadata.cvmInstanceRegion}, host_name=${cachedMetadata.hostName}`,
216
228
  );
217
229
 
218
230
  return successCount > 0;