1 Star 0 Fork 0

zhangjungang/beats

加入 Gitee
与超过 1200万 开发者一起发现、参与优秀开源项目,私有仓库也完全免费 :)
免费加入
文件
克隆/下载
helper.go 1.31 KB
一键复制 编辑 原始数据 按行查看 历史
package docker
import (
"strings"
"github.com/elastic/beats/libbeat/common"
"github.com/fsouza/go-dockerclient"
)
type Container struct {
ID string
Name string
Labels common.MapStr
}
func (c *Container) ToMapStr() common.MapStr {
m := common.MapStr{
"id": c.ID,
"name": c.Name,
}
if len(c.Labels) > 0 {
m["labels"] = c.Labels
}
return m
}
func NewContainer(container *docker.APIContainers) *Container {
return &Container{
ID: container.ID,
Name: ExtractContainerName(container.Names),
Labels: DeDotLabels(container.Labels),
}
}
func ExtractContainerName(names []string) string {
output := names[0]
if len(names) > 1 {
for _, name := range names {
if strings.Count(output, "/") > strings.Count(name, "/") {
output = name
}
}
}
return strings.Trim(output, "/")
}
// DeDotLabels returns a new common.MapStr containing a copy of the labels
// where the dots in each label name have been changed to an underscore.
func DeDotLabels(labels map[string]string) common.MapStr {
outputLabels := common.MapStr{}
for k, v := range labels {
// This is necessary so that ES does not interpret '.' fields as new
// nested JSON objects, and also makes this compatible with ES 2.x.
label := strings.Replace(k, ".", "_", -1)
outputLabels.Put(label, v)
}
return outputLabels
}
马建仓 AI 助手
尝试更多
代码解读
代码找茬
代码优化
1
https://gitee.com/zhangjungang/beats.git
git@gitee.com:zhangjungang/beats.git
zhangjungang
beats
beats
v5.6.8

搜索帮助