1 Star 0 Fork 0

sqos/beats

加入 Gitee
与超过 1200万 开发者一起发现、参与优秀开源项目,私有仓库也完全免费 :)
免费加入
克隆/下载
data.go 2.08 KB
一键复制 编辑 原始数据 按行查看 历史
package jmx
import (
"encoding/json"
"github.com/joeshaw/multierror"
"github.com/pkg/errors"
"github.com/elastic/beats/libbeat/common"
)
type Entry struct {
Request struct {
Mbean string `json:"mbean"`
}
Value map[string]interface{}
}
// Map responseBody to common.MapStr
//
// A response has the following structure
// [
// {
// "request": {
// "mbean": "java.lang:type=Memory",
// "attribute": [
// "HeapMemoryUsage",
// "NonHeapMemoryUsage"
// ],
// "type": "read"
// },
// "value": {
// "HeapMemoryUsage": {
// "init": 1073741824,
// "committed": 1037959168,
// "max": 1037959168,
// "used": 227420472
// },
// "NonHeapMemoryUsage": {
// "init": 2555904,
// "committed": 53477376,
// "max": -1,
// "used": 50519768
// }
// },
// "timestamp": 1472298687,
// "status": 200
// }
// ]
func eventMapping(content []byte, mapping map[string]string) (common.MapStr, error) {
var entries []Entry
if err := json.Unmarshal(content, &entries); err != nil {
return nil, errors.Wrapf(err, "failed to unmarshal jolokia JSON response '%v'", string(content))
}
event := common.MapStr{}
var errs multierror.Errors
for _, v := range entries {
for attribute, value := range v.Value {
// Extend existing event
err := parseResponseEntry(v.Request.Mbean, attribute, value, event, mapping)
if err != nil {
errs = append(errs, err)
}
}
}
return event, errs.Err()
}
func parseResponseEntry(
mbeanName string,
attributeName string,
attibuteValue interface{},
event common.MapStr,
mapping map[string]string,
) error {
// Create metric name by merging mbean and attribute fields.
var metricName = mbeanName + "_" + attributeName
key, exists := mapping[metricName]
if !exists {
return errors.Errorf("metric key '%v' not found in response", key)
}
_, err := event.Put(key, attibuteValue)
return err
}
马建仓 AI 助手
尝试更多
代码解读
代码找茬
代码优化
1
https://gitee.com/sqos/beats.git
git@gitee.com:sqos/beats.git
sqos
beats
beats
v6.1.0

搜索帮助

344bd9b3 5694891 D2dac590 5694891