mirror of
https://github.com/netdata/netdata.git
synced 2025-04-16 18:37:50 +00:00
go.d docker respect DOCKER_HOST env var (#17979)
This commit is contained in:
parent
88fe280f22
commit
74d42b50fa
3 changed files with 34 additions and 0 deletions
src/go/collectors/go.d.plugin
|
@ -13,6 +13,7 @@ import (
|
|||
|
||||
"github.com/netdata/netdata/go/go.d.plugin/agent/discovery/sd/model"
|
||||
"github.com/netdata/netdata/go/go.d.plugin/logger"
|
||||
"github.com/netdata/netdata/go/go.d.plugin/pkg/dockerhost"
|
||||
"github.com/netdata/netdata/go/go.d.plugin/pkg/web"
|
||||
|
||||
"github.com/docker/docker/api/types"
|
||||
|
@ -43,6 +44,11 @@ func NewDiscoverer(cfg Config) (*Discoverer, error) {
|
|||
started: make(chan struct{}),
|
||||
}
|
||||
|
||||
if addr := dockerhost.FromEnv(); addr != "" && d.addr == docker.DefaultDockerHost {
|
||||
d.Infof("using docker host from environment: %s ", addr)
|
||||
d.addr = addr
|
||||
}
|
||||
|
||||
d.Tags().Merge(tags)
|
||||
|
||||
if cfg.Timeout.Duration().Seconds() != 0 {
|
||||
|
|
|
@ -9,6 +9,7 @@ import (
|
|||
"time"
|
||||
|
||||
"github.com/netdata/netdata/go/go.d.plugin/agent/module"
|
||||
"github.com/netdata/netdata/go/go.d.plugin/pkg/dockerhost"
|
||||
"github.com/netdata/netdata/go/go.d.plugin/pkg/web"
|
||||
|
||||
"github.com/docker/docker/api/types"
|
||||
|
@ -79,6 +80,10 @@ func (d *Docker) Configuration() any {
|
|||
}
|
||||
|
||||
func (d *Docker) Init() error {
|
||||
if addr := dockerhost.FromEnv(); addr != "" && d.Address == docker.DefaultDockerHost {
|
||||
d.Infof("using docker host from environment: %s ", addr)
|
||||
d.Address = addr
|
||||
}
|
||||
return nil
|
||||
}
|
||||
|
||||
|
|
23
src/go/collectors/go.d.plugin/pkg/dockerhost/dockerhost.go
Normal file
23
src/go/collectors/go.d.plugin/pkg/dockerhost/dockerhost.go
Normal file
|
@ -0,0 +1,23 @@
|
|||
// SPDX-License-Identifier: GPL-3.0-or-later
|
||||
|
||||
package dockerhost
|
||||
|
||||
import (
|
||||
"fmt"
|
||||
"os"
|
||||
"strings"
|
||||
)
|
||||
|
||||
func FromEnv() string {
|
||||
addr := os.Getenv("DOCKER_HOST")
|
||||
if addr == "" {
|
||||
return ""
|
||||
}
|
||||
if strings.HasPrefix(addr, "tcp://") || strings.HasPrefix(addr, "unix://") {
|
||||
return addr
|
||||
}
|
||||
if strings.HasPrefix(addr, "/") {
|
||||
return fmt.Sprintf("unix://%s", addr)
|
||||
}
|
||||
return fmt.Sprintf("tcp://%s", addr)
|
||||
}
|
Loading…
Add table
Reference in a new issue