Skip to content

Commit

Permalink
[hostmetrics] Add warning on startup if running in docker with no oth…
Browse files Browse the repository at this point in the history
…er root defined. (open-telemetry#17290)

In the event that the container is started in a docker container but
doesn't have a root path, it is likely that it will report the
container's stats which is misleading and hard to diagnose

Co-authored-by: Dmitrii Anoshin <[email protected]>
  • Loading branch information
MovieStoreGuy and dmitryax committed Jan 4, 2023
1 parent d7f5509 commit 7677186
Showing 1 changed file with 20 additions and 0 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -16,6 +16,7 @@ package filesystemscraper // import "github.com/open-telemetry/opentelemetry-col

import (
"context"
"os"

"go.opentelemetry.io/collector/receiver"
"go.opentelemetry.io/collector/receiver/scraperhelper"
Expand Down Expand Up @@ -54,6 +55,25 @@ func (f *Factory) CreateMetricsScraper(
config internal.Config,
) (scraperhelper.Scraper, error) {
cfg := config.(*Config)

if cfg.RootPath == "" {
inContainer := os.Getpid() == 1
for _, p := range []string{
"/.dockerenv", // Mounted by dockerd when starting a container by default
"/run/.containerenv", // Mounted by podman as described here: https://github.com/containers/podman/blob/ecbb52cb478309cfd59cc061f082702b69f0f4b7/docs/source/markdown/podman-run.1.md.in#L31
} {
if _, err := os.Stat(p); err == nil {

break
}
}
if inContainer {
settings.Logger.Warn(
"No `root_path` config set when running in docker environment, will report container filesystem stats." +
" See https://github.com/open-telemetry/opentelemetry-collector-contrib/tree/main/receiver/hostmetricsreceiver#collecting-host-metrics-from-inside-a-container-linux-only")
}
}

s, err := newFileSystemScraper(ctx, settings, cfg)
if err != nil {
return nil, err
Expand Down

0 comments on commit 7677186

Please sign in to comment.