Skip to content

Commit

Permalink
metrics: add push-labels option for java sdk and add `--custom-labl…
Browse files Browse the repository at this point in the history
…es` option for mount (#4312)
  • Loading branch information
reAsOn2010 authored Jan 9, 2024
1 parent fe79909 commit 94bdcc6
Show file tree
Hide file tree
Showing 11 changed files with 58 additions and 8 deletions.
4 changes: 4 additions & 0 deletions cmd/flags.go
Original file line number Diff line number Diff line change
Expand Up @@ -291,6 +291,10 @@ func shareInfoFlags() []cli.Flag {
Value: "127.0.0.1:9567",
Usage: "address to export metrics",
},
&cli.StringFlag{
Name: "custom-labels",
Usage: "custom labels for metrics",
},
&cli.StringFlag{
Name: "consul",
Value: "127.0.0.1:8500",
Expand Down
2 changes: 1 addition & 1 deletion cmd/gateway.go
Original file line number Diff line number Diff line change
Expand Up @@ -194,7 +194,7 @@ func initForSvc(c *cli.Context, mp string, metaUrl string) (*vfs.Config, *fs.Fil
if st := metaCli.Chroot(meta.Background, metaConf.Subdir); st != 0 {
logger.Fatalf("Chroot to %s: %s", metaConf.Subdir, st)
}
registerer, registry := wrapRegister(mp, format.Name)
registerer, registry := wrapRegister(c, mp, format.Name)

blob, err := NewReloadableStorage(format, metaCli, updateFormat(c))
if err != nil {
Expand Down
2 changes: 1 addition & 1 deletion cmd/mdtest.go
Original file line number Diff line number Diff line change
Expand Up @@ -198,7 +198,7 @@ func initForMdtest(c *cli.Context, mp string, metaUrl string) *fs.FileSystem {
if st := m.Chroot(meta.Background, metaConf.Subdir); st != 0 {
logger.Fatalf("Chroot to %s: %s", metaConf.Subdir, st)
}
registerer, registry := wrapRegister(mp, format.Name)
registerer, registry := wrapRegister(c, mp, format.Name)

blob, err := NewReloadableStorage(format, m, updateFormat(c))
if err != nil {
Expand Down
25 changes: 22 additions & 3 deletions cmd/mount.go
Original file line number Diff line number Diff line change
Expand Up @@ -159,10 +159,29 @@ func exposeMetrics(c *cli.Context, registerer prometheus.Registerer, registry *p
return metricsAddr
}

func wrapRegister(mp, name string) (prometheus.Registerer, *prometheus.Registry) {
func wrapRegister(c *cli.Context, mp, name string) (prometheus.Registerer, *prometheus.Registry) {
commonLabels := prometheus.Labels{"mp": mp, "vol_name": name}
if h, err := os.Hostname(); err == nil {
commonLabels["instance"] = h
} else {
logger.Warnf("cannot get hostname: %s", err)
}
if c.IsSet("custom-labels") {
for _, kv := range strings.Split(c.String("custom-labels"), ",") {
splited := strings.Split(kv, ":")
if len(splited) != 2 {
logger.Fatalf("invalid label format: %s", kv)
}
if utils.StringContains([]string{"mp", "vol_name", "instance"}, splited[0]) {
logger.Warnf("overriding reserved label: %s", splited[0])
}
commonLabels[splited[0]] = splited[1]
}
}
registry := prometheus.NewRegistry() // replace default so only JuiceFS metrics are exposed
registerer := prometheus.WrapRegistererWithPrefix("juicefs_",
prometheus.WrapRegistererWith(prometheus.Labels{"mp": mp, "vol_name": name}, registry))
prometheus.WrapRegistererWith(commonLabels, registry))

registerer.MustRegister(collectors.NewProcessCollector(collectors.ProcessCollectorOpts{}))
registerer.MustRegister(collectors.NewGoCollector())
return registerer, registry
Expand Down Expand Up @@ -619,7 +638,7 @@ func mount(c *cli.Context) error {
}

// Wrap the default registry, all prometheus.MustRegister() calls should be afterwards
registerer, registry := wrapRegister(mp, format.Name)
registerer, registry := wrapRegister(c, mp, format.Name)

blob, err := NewReloadableStorage(format, metaCli, updateFormat(c))
if err != nil {
Expand Down
14 changes: 11 additions & 3 deletions cmd/mount_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -62,17 +62,24 @@ func Test_exposeMetrics(t *testing.T) {
return "127.0.0.1:9567"
case "consul":
return "127.0.0.1:8500"
case "custom-labels":
return "key1:value1"
default:
return ""
}
})
isSetPatches := gomonkey.ApplyMethod(reflect.TypeOf(appCtx), "IsSet", func(_ *cli.Context, _ string) bool {
return false
isSetPatches := gomonkey.ApplyMethod(reflect.TypeOf(appCtx), "IsSet", func(_ *cli.Context, arg string) bool {
switch arg {
case "custom-labels":
return true
default:
return false
}
})
defer stringPatches.Reset()
defer isSetPatches.Reset()
ResetHttp()
registerer, registry := wrapRegister("test", "test")
registerer, registry := wrapRegister(appCtx, "test", "test")
metricsAddr := exposeMetrics(appCtx, registerer, registry)
client.InitMetrics(registerer)
vfs.InitMetrics(registerer)
Expand All @@ -82,6 +89,7 @@ func Test_exposeMetrics(t *testing.T) {
all, err := io.ReadAll(resp.Body)
So(err, ShouldBeNil)
So(string(all), ShouldNotBeBlank)
So(string(all), ShouldContainSubstring, `key1="value1"`)
})
})
}
Expand Down
1 change: 1 addition & 0 deletions docs/en/deployment/hadoop_java_sdk.md
Original file line number Diff line number Diff line change
Expand Up @@ -204,6 +204,7 @@ Please refer to the following table to set the relevant parameters of the JuiceF
| `juicefs.push-auth` | | [Prometheus basic auth](https://prometheus.io/docs/guides/basic-auth) information, format is `<username>:<password>`. |
| `juicefs.push-graphite` | | [Graphite](https://graphiteapp.org) address, format is `<host>:<port>`. |
| `juicefs.push-interval` | 10 | Metric push interval (in seconds) |
| `juicefs.push-labels` | | Metric labels, format is `key1:value1,key2:value2`. |
| `juicefs.fast-resolve` | `true` | Whether enable faster metadata lookup using Redis Lua script |
| `juicefs.no-usage-report` | `false` | Whether disable usage reporting. JuiceFS only collects anonymous usage data (e.g. version number), no user or any sensitive data will be collected. |
| `juicefs.no-bgjob` | `false` | Disable background jobs (clean-up, backup, etc.) |
Expand Down
1 change: 1 addition & 0 deletions docs/en/reference/command_reference.md
Original file line number Diff line number Diff line change
Expand Up @@ -683,6 +683,7 @@ For metadata cache description and usage, refer to [Kernel metadata cache](../gu
||Items|Description|
|-|-|
|`--metrics=127.0.0.1:9567`|address to export metrics (default: `127.0.0.1:9567`)|
|`--custom-labels`|custom labels for metrics, format: `key1:value1,key2:value2` (default: "")|
|`--consul=127.0.0.1:8500`|Consul address to register (default: `127.0.0.1:8500`)|
|`--no-usage-report`|do not send usage report (default: false)|

Expand Down
1 change: 1 addition & 0 deletions docs/zh_cn/deployment/hadoop_java_sdk.md
Original file line number Diff line number Diff line change
Expand Up @@ -204,6 +204,7 @@ make win
| `juicefs.push-auth` | | [Prometheus 基本认证](https://prometheus.io/docs/guides/basic-auth)信息,格式为 `<username>:<password>`|
| `juicefs.push-graphite` | | [Graphite](https://graphiteapp.org) 地址,格式为 `<host>:<port>`|
| `juicefs.push-interval` | 10 | 指标推送的时间间隔,单位为秒。 |
| `juicefs.push-labels` | | 指标额外标签,格式为 `key1:value1,key2:value2`|
| `juicefs.fast-resolve` | `true` | 是否开启快速元数据查找(通过 Redis Lua 脚本实现) |
| `juicefs.no-usage-report` | `false` | 是否上报数据。仅上版本号等使用量数据,不包含任何用户信息。 |
| `juicefs.block.size` | `134217728` | 单位为字节,同 HDFS 的 `dfs.blocksize`,默认 128 MB |
Expand Down
1 change: 1 addition & 0 deletions docs/zh_cn/reference/command_reference.md
Original file line number Diff line number Diff line change
Expand Up @@ -683,6 +683,7 @@ juicefs mount redis://localhost /mnt/jfs --backup-meta 0
|| 说明|
|-|-|
|`--metrics=127.0.0.1:9567`|监控数据导出地址,默认为 `127.0.0.1:9567`|
|`--custom-labels`|监控指标自定义标签,格式为 `key1:value1,key2:value2` (默认:"")|
|`--consul=127.0.0.1:8500`|Consul 注册中心地址,默认为 `127.0.0.1:8500`|
|`--no-usage-report`|不发送使用量信息 (默认:false)|

Expand Down
14 changes: 14 additions & 0 deletions sdk/java/libjfs/main.go
Original file line number Diff line number Diff line change
Expand Up @@ -309,6 +309,7 @@ type javaConf struct {
PushGateway string `json:"pushGateway"`
PushInterval int `json:"pushInterval"`
PushAuth string `json:"pushAuth"`
PushLabels string `json:"pushLabels"`
PushGraphite string `json:"pushGraphite"`
}

Expand Down Expand Up @@ -454,6 +455,19 @@ func jfs_init(cname, jsonConf, user, group, superuser, supergroup *C.char) uintp
} else {
logger.Warnf("cannot get hostname: %s", err)
}
if jConf.PushLabels != "" {
for _, kv := range strings.Split(jConf.PushLabels, ",") {
var splited = strings.Split(kv, ":")
if len(splited) != 2 {
logger.Errorf("invalid label format: %s", kv)
return nil
}
if utils.StringContains([]string{"mp", "vol_name", "instance"}, splited[0]) {
logger.Warnf("overriding reserved label: %s", splited[0])
}
commonLabels[splited[0]] = splited[1]
}
}
registry := prometheus.NewRegistry()
registerer = prometheus.WrapRegistererWithPrefix("juicefs_", registry)
registerer.MustRegister(collectors.NewProcessCollector(collectors.ProcessCollectorOpts{}))
Expand Down
1 change: 1 addition & 0 deletions sdk/java/src/main/java/io/juicefs/JuiceFileSystemImpl.java
Original file line number Diff line number Diff line change
Expand Up @@ -397,6 +397,7 @@ public void initialize(URI uri, Configuration conf) throws IOException {
obj.put("pushGateway", getConf(conf, "push-gateway", ""));
obj.put("pushInterval", Integer.valueOf(getConf(conf, "push-interval", "10")));
obj.put("pushAuth", getConf(conf, "push-auth", ""));
obj.put("pushLabels", getConf(conf, "push-labels", ""));
obj.put("pushGraphite", getConf(conf, "push-graphite", ""));
obj.put("fastResolve", Boolean.valueOf(getConf(conf, "fast-resolve", "true")));
obj.put("noUsageReport", Boolean.valueOf(getConf(conf, "no-usage-report", "false")));
Expand Down

0 comments on commit 94bdcc6

Please sign in to comment.