Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

feat(docker-win): add main for windows #271

Open
wants to merge 3 commits into
base: feat/windows-support
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 1 addition & 1 deletion src/config/config.go
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,7 @@ type ArgumentList struct {
args.DefaultArgumentList
HostRoot string `default:"" help:"If the integration is running from a container, the mounted folder pointing to the host root folder"`
Fargate bool `default:"false" help:"Enables fetching metrics from ECS Fargate. If enabled no metrics are collected from cgroups. Defaults to false"`
UseDockerAPI bool `default:"false" help:"Enables fetching metrics from Docker API. If enabled no metrics are collected from cgroups. This option is ignored if cgroupsV1 are detected"`
UseDockerAPI bool `default:"false" help:"Enables fetching metrics from Docker API. If enabled no metrics are collected from cgroups. For Linux: This option is ignored if cgroupsV1 are detected. For OSes other than Linux: This option is always ignored and defaults to true"`
ExitedContainersTTL string `default:"24h" help:"Enables to integration to stop reporting Exited containers that are older than the set TTL. Possible values are time-strings: 1s, 1m, 1h"`
DockerClientVersion string `default:"1.24" help:"Optional. Specify the version of the docker client. Used for compatibility."`
DisableStorageMetrics bool `default:"false" help:"Disables storage driver metrics collection."`
Expand Down
97 changes: 12 additions & 85 deletions src/docker.go
Original file line number Diff line number Diff line change
@@ -1,24 +1,15 @@
// Copyright 2025 New Relic Corporation. All rights reserved.
// SPDX-License-Identifier: Apache-2.0

package main

import (
"context"
"fmt"
"os"
"runtime"

"github.com/docker/docker/api/types/system"
"github.com/docker/docker/client"
"github.com/newrelic/infra-integrations-sdk/v3/integration"
"github.com/newrelic/infra-integrations-sdk/v3/log"
"github.com/newrelic/nri-docker/src/config"
"github.com/newrelic/nri-docker/src/nri"
"github.com/newrelic/nri-docker/src/raw"
"github.com/newrelic/nri-docker/src/raw/aws"
"github.com/newrelic/nri-docker/src/raw/dockerapi"
)

const (
integrationName = "com.newrelic.docker"
"github.com/newrelic/nri-docker/src/util"
)

var (
Expand All @@ -29,87 +20,23 @@ var (

func main() {
args := config.ArgumentList{}
i, err := integration.New(integrationName, integrationVersion, integration.Args(&args))
exitOnErr(err)
// we always use the docker API for OSes other than Linux
args.UseDockerAPI = util.ForceTrueForOSOtherThanLinux(args.UseDockerAPI)
i, err := integration.New(util.IntegrationName, integrationVersion, integration.Args(&args))
util.ExitOnErr(err)

if args.ShowVersion {
printVersion()
util.PrintVersion(integrationVersion, gitCommit, buildDate)
os.Exit(0)
}

log.SetupLogging(args.Verbose)

if args.Fargate {
populateFromFargate(i, args)
util.PopulateFromFargate(i, args)
} else {
populateFromDocker(i, args)
}

exitOnErr(i.Publish())
}

func populateFromFargate(i *integration.Integration, args config.ArgumentList) {
metadataBaseURL, err := aws.GetMetadataBaseURL()
exitOnErr(err)

fargateFetcher, err := aws.NewFargateFetcher(metadataBaseURL)
exitOnErr(err)

fargateDockerClient, err := aws.NewFargateInspector(metadataBaseURL)
exitOnErr(err)

sampler, err := nri.NewSampler(fargateFetcher, fargateDockerClient, args)
exitOnErr(err)
// Info is currently used to get the Storage Driver stats that is not present on Fargate.
exitOnErr(sampler.SampleAll(context.Background(), i, system.Info{}))
}

func populateFromDocker(i *integration.Integration, args config.ArgumentList) {
withVersionOpt := client.WithVersion(args.DockerClientVersion)
dockerClient, err := client.NewClientWithOpts(client.FromEnv, withVersionOpt)
exitOnErr(err)
defer dockerClient.Close()

cgroupInfo, err := dockerClient.Info(context.Background())
exitOnErr(err)

var fetcher raw.Fetcher
if useDockerAPI(args.UseDockerAPI, cgroupInfo.CgroupVersion) {
fetcher = dockerapi.NewFetcher(dockerClient)
} else { // use cgroups as source of data
fetcher, err = raw.NewCgroupFetcher(args.HostRoot, cgroupInfo)
exitOnErr(err)
util.PopulateFromDocker(i, args)
}

sampler, err := nri.NewSampler(fetcher, dockerClient, args)
exitOnErr(err)
exitOnErr(sampler.SampleAll(context.Background(), i, cgroupInfo))
}

func useDockerAPI(dockerAPIRequested bool, version string) bool {
if dockerAPIRequested {
if version == raw.CgroupV2 {
return true
}
log.Debug("UseDockerAPI config is not available on CgroupV1")
return false
}
return false
}

func exitOnErr(err error) {
if err != nil {
log.Error(err.Error())
os.Exit(-1)
}
}

func printVersion() {
fmt.Printf(
"New Relic Docker integration Version: %s, Platform: %s, GoVersion: %s, GitCommit: %s, BuildDate: %s\n",
integrationVersion,
fmt.Sprintf("%s/%s", runtime.GOOS, runtime.GOARCH),
runtime.Version(),
gitCommit,
buildDate)
util.ExitOnErr(i.Publish())
}
2 changes: 2 additions & 0 deletions src/raw/cgroup.go
Original file line number Diff line number Diff line change
@@ -1,3 +1,5 @@
//go:build linux
rahulreddy15 marked this conversation as resolved.
Show resolved Hide resolved

package raw

import (
Expand Down
2 changes: 2 additions & 0 deletions src/raw/cgroup_detect.go
Original file line number Diff line number Diff line change
@@ -1,3 +1,5 @@
//go:build linux

package raw

import (
Expand Down
2 changes: 2 additions & 0 deletions src/raw/cgroup_test.go
Original file line number Diff line number Diff line change
@@ -1,3 +1,5 @@
//go:build linux

package raw

import (
Expand Down
2 changes: 2 additions & 0 deletions src/raw/cgroupv1.go
Original file line number Diff line number Diff line change
@@ -1,3 +1,5 @@
//go:build linux

// Package raw fetches raw system-level metrics as they are presented by the operating system
package raw

Expand Down
2 changes: 2 additions & 0 deletions src/raw/cgroupv1_detect.go
Original file line number Diff line number Diff line change
@@ -1,3 +1,5 @@
//go:build linux

package raw

import (
Expand Down
2 changes: 2 additions & 0 deletions src/raw/cgroupv1_detect_test.go
Original file line number Diff line number Diff line change
@@ -1,3 +1,5 @@
//go:build linux

package raw

import (
Expand Down
2 changes: 2 additions & 0 deletions src/raw/cgroupv2.go
Original file line number Diff line number Diff line change
@@ -1,3 +1,5 @@
//go:build linux

// Package raw fetches raw system-level metrics as they are presented by the operating system
package raw

Expand Down
2 changes: 2 additions & 0 deletions src/raw/cgroupv2_detect.go
Original file line number Diff line number Diff line change
@@ -1,3 +1,5 @@
//go:build linux

package raw

import (
Expand Down
2 changes: 2 additions & 0 deletions src/raw/cgroupv2_detect_test.go
Original file line number Diff line number Diff line change
@@ -1,3 +1,5 @@
//go:build linux

package raw

import (
Expand Down
2 changes: 2 additions & 0 deletions src/raw/system_cpu_reader.go
Original file line number Diff line number Diff line change
@@ -1,3 +1,5 @@
//go:build linux

package raw

import (
Expand Down
2 changes: 2 additions & 0 deletions src/raw/system_cpu_reader_test.go
Original file line number Diff line number Diff line change
@@ -1,3 +1,5 @@
//go:build linux

package raw

import (
Expand Down
55 changes: 55 additions & 0 deletions src/util/util.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,55 @@
// Copyright 2025 New Relic Corporation. All rights reserved.
// SPDX-License-Identifier: Apache-2.0

package util

import (
"context"
"fmt"
"os"
"runtime"

"github.com/docker/docker/api/types/system"
"github.com/newrelic/infra-integrations-sdk/v3/integration"
"github.com/newrelic/infra-integrations-sdk/v3/log"
"github.com/newrelic/nri-docker/src/config"
"github.com/newrelic/nri-docker/src/nri"
"github.com/newrelic/nri-docker/src/raw/aws"
)

const (
IntegrationName = "com.newrelic.docker"
rahulreddy15 marked this conversation as resolved.
Show resolved Hide resolved
)

func ExitOnErr(err error) {
if err != nil {
log.Error(err.Error())
os.Exit(-1)
}
}

func PrintVersion(integrationVersion, gitCommit, buildDate string) {
fmt.Printf(
"New Relic Docker integration Version: %s, Platform: %s, GoVersion: %s, GitCommit: %s, BuildDate: %s\n",
integrationVersion,
fmt.Sprintf("%s/%s", runtime.GOOS, runtime.GOARCH),
runtime.Version(),
gitCommit,
buildDate)
}

func PopulateFromFargate(i *integration.Integration, args config.ArgumentList) {
metadataBaseURL, err := aws.GetMetadataBaseURL()
ExitOnErr(err)

fargateFetcher, err := aws.NewFargateFetcher(metadataBaseURL)
ExitOnErr(err)

fargateDockerClient, err := aws.NewFargateInspector(metadataBaseURL)
ExitOnErr(err)

sampler, err := nri.NewSampler(fargateFetcher, fargateDockerClient, args)
ExitOnErr(err)
// Info is currently used to get the Storage Driver stats that is not present on Fargate.
ExitOnErr(sampler.SampleAll(context.Background(), i, system.Info{}))
}
36 changes: 36 additions & 0 deletions src/util/util_all.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,36 @@
// Copyright 2025 New Relic Corporation. All rights reserved.
// SPDX-License-Identifier: Apache-2.0

//go:build !linux
rahulreddy15 marked this conversation as resolved.
Show resolved Hide resolved

package util

import (
"context"

"github.com/docker/docker/api/types/system"
"github.com/docker/docker/client"
"github.com/newrelic/infra-integrations-sdk/v3/integration"
"github.com/newrelic/nri-docker/src/config"
"github.com/newrelic/nri-docker/src/nri"
"github.com/newrelic/nri-docker/src/raw/dockerapi"
)

// ForceTrueForOSOtherThanLinux returns true always for OSes other than Linux
func ForceTrueForOSOtherThanLinux(dockerAPIArg bool) bool {
return true
}

func PopulateFromDocker(i *integration.Integration, args config.ArgumentList) {
withVersionOpt := client.WithVersion(args.DockerClientVersion)
dockerClient, err := client.NewClientWithOpts(client.FromEnv, withVersionOpt)
ExitOnErr(err)
defer dockerClient.Close()

fetcher := dockerapi.NewFetcher(dockerClient)

sampler, err := nri.NewSampler(fetcher, dockerClient, args)
ExitOnErr(err)
// always use dockerAPI for windows
ExitOnErr(sampler.SampleAll(context.Background(), i, system.Info{}))
}
54 changes: 54 additions & 0 deletions src/util/util_linux.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,54 @@
// Copyright 2025 New Relic Corporation. All rights reserved.
// SPDX-License-Identifier: Apache-2.0

package util
rahulreddy15 marked this conversation as resolved.
Show resolved Hide resolved

import (
"context"

"github.com/docker/docker/client"
"github.com/newrelic/infra-integrations-sdk/v3/integration"
"github.com/newrelic/infra-integrations-sdk/v3/log"
"github.com/newrelic/nri-docker/src/config"
"github.com/newrelic/nri-docker/src/nri"
"github.com/newrelic/nri-docker/src/raw"
"github.com/newrelic/nri-docker/src/raw/dockerapi"
)

// ForceTrueForOSOtherThanLinux returns the value of the dockerAPIArg without any modification
func ForceTrueForOSOtherThanLinux(dockerAPIArg bool) bool {
return dockerAPIArg
}

func UseDockerAPI(dockerAPIRequested bool, version string) bool {
if dockerAPIRequested {
if version == raw.CgroupV2 {
return true
}
log.Debug("UseDockerAPI config is not available on CgroupV1")
return false
}
return false
}

func PopulateFromDocker(i *integration.Integration, args config.ArgumentList) {
withVersionOpt := client.WithVersion(args.DockerClientVersion)
dockerClient, err := client.NewClientWithOpts(client.FromEnv, withVersionOpt)
ExitOnErr(err)
defer dockerClient.Close()

cgroupInfo, err := dockerClient.Info(context.Background())
ExitOnErr(err)

var fetcher raw.Fetcher
if UseDockerAPI(args.UseDockerAPI, cgroupInfo.CgroupVersion) {
fetcher = dockerapi.NewFetcher(dockerClient)
} else { // use cgroups as source of data
fetcher, err = raw.NewCgroupFetcher(args.HostRoot, cgroupInfo)
ExitOnErr(err)
}

sampler, err := nri.NewSampler(fetcher, dockerClient, args)
ExitOnErr(err)
ExitOnErr(sampler.SampleAll(context.Background(), i, cgroupInfo))
}
2 changes: 2 additions & 0 deletions test/integration/metrics_cgroupsv2_test.go
Original file line number Diff line number Diff line change
@@ -1,3 +1,5 @@
//go:build linux

package integration

import (
Expand Down
2 changes: 2 additions & 0 deletions test/integration/metrics_test.go
Original file line number Diff line number Diff line change
@@ -1,3 +1,5 @@
//go:build linux

package integration

import (
Expand Down
2 changes: 2 additions & 0 deletions test/integration/mocks.go
Original file line number Diff line number Diff line change
@@ -1,3 +1,5 @@
//go:build linux

package integration

import (
Expand Down
Loading