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

Fix default docker_observer endpoint on Windows #34358

Merged
Merged
Show file tree
Hide file tree
Changes from 2 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
6 changes: 3 additions & 3 deletions extension/observer/dockerobserver/README.md
Original file line number Diff line number Diff line change
Expand Up @@ -19,15 +19,15 @@ This observer watches the Docker engine's stream of events to dynamically create
Requires Docker API Version 1.24+.

The collector will need permissions to access the Docker Engine API, specifically it will need
read access to the Docker socket (default `unix:///var/run/docker.sock`).
read access to the Docker socket (default `unix:///var/run/docker.sock` on non-Windows and `npipe:////./pipe/docker_engine` on Windows).


## Example Config

```yaml
extensions:
docker_observer:
# url of the docker socket, default to unix:///var/run/docker.sock
# url of the docker socket, defaults to unix:///var/run/docker.sock on non-Windows and npipe:////./pipe/docker_engine on Windows
endpoint: my/path/to/docker.sock
# list of container image names to exclude
excluded_images: ['redis', 'another_image_name']
Expand All @@ -53,7 +53,7 @@ receivers:

The URL of the docker server.

default: `unix:///var/run/docker.sock`
default: `unix:///var/run/docker.sock` on non-Windows and `npipe:////./pipe/docker_engine` on Windows

### `timeout`

Expand Down
3 changes: 2 additions & 1 deletion extension/observer/dockerobserver/config.go
Original file line number Diff line number Diff line change
Expand Up @@ -16,7 +16,8 @@ import (
// Config defines configuration for docker observer
type Config struct {

// The URL of the docker server. Default is "unix:///var/run/docker.sock"
// The URL of the docker server. Default is "unix:///var/run/docker.sock" on non-Windows
// and "npipe:////./pipe/docker_engine" on Windows
Endpoint string `mapstructure:"endpoint"`

// The maximum amount of time to wait for docker API responses. Default is 5s
Expand Down
3 changes: 2 additions & 1 deletion extension/observer/dockerobserver/factory.go
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,7 @@ import (
"context"
"time"

"github.com/docker/docker/client"
"go.opentelemetry.io/collector/component"
"go.opentelemetry.io/collector/extension"

Expand All @@ -25,7 +26,7 @@ func NewFactory() extension.Factory {

func createDefaultConfig() component.Config {
return &Config{
Endpoint: "unix:///var/run/docker.sock",
Endpoint: client.DefaultDockerHost,
Timeout: 5 * time.Second,
CacheSyncInterval: 60 * time.Minute,
DockerAPIVersion: defaultDockerAPIVersion,
Expand Down
5 changes: 1 addition & 4 deletions extension/observer/dockerobserver/go.mod
Original file line number Diff line number Diff line change
Expand Up @@ -22,7 +22,7 @@ require (
require (
dario.cat/mergo v1.0.0 // indirect
github.com/Azure/go-ansiterm v0.0.0-20210617225240-d185dfc1b5a1 // indirect
github.com/Microsoft/go-winio v0.6.1 // indirect
github.com/Microsoft/go-winio v0.6.2 // indirect
github.com/Microsoft/hcsshim v0.11.4 // indirect
github.com/beorn7/perks v1.0.1 // indirect
github.com/cenkalti/backoff/v4 v4.2.1 // indirect
Expand Down Expand Up @@ -83,12 +83,9 @@ require (
go.opentelemetry.io/otel/sdk/metric v1.28.0 // indirect
go.uber.org/multierr v1.11.0 // indirect
golang.org/x/crypto v0.24.0 // indirect
golang.org/x/mod v0.17.0 // indirect
golang.org/x/net v0.26.0 // indirect
golang.org/x/sync v0.7.0 // indirect
golang.org/x/sys v0.21.0 // indirect
golang.org/x/text v0.16.0 // indirect
golang.org/x/tools v0.21.1-0.20240508182429-e35e4ccd0d2d // indirect
google.golang.org/genproto/googleapis/rpc v0.0.0-20240528184218-531527333157 // indirect
google.golang.org/grpc v1.65.0 // indirect
google.golang.org/protobuf v1.34.2 // indirect
Expand Down
10 changes: 2 additions & 8 deletions extension/observer/dockerobserver/go.sum

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

4 changes: 3 additions & 1 deletion internal/docker/config.go
Original file line number Diff line number Diff line change
Expand Up @@ -11,10 +11,12 @@ import (
"time"

"github.com/docker/docker/api/types/versions"
"github.com/docker/docker/client"
)

type Config struct {
// The URL of the docker server. Default is "unix:///var/run/docker.sock"
// on non-Windows and "npipe:////./pipe/docker_engine" on Windows
Endpoint string `mapstructure:"endpoint"`

// The maximum amount of time to wait for docker API responses. Default is 5s
Expand Down Expand Up @@ -43,7 +45,7 @@ func NewConfig(endpoint string, timeout time.Duration, excludedImages []string,
// to be used when creating a docker client
func NewDefaultConfig() *Config {
cfg := &Config{
Endpoint: "unix:///var/run/docker.sock",
Endpoint: client.DefaultDockerHost,
Timeout: 5 * time.Second,
DockerAPIVersion: minimumRequiredDockerAPIVersion,
}
Expand Down
54 changes: 15 additions & 39 deletions internal/docker/docker_test.go
Original file line number Diff line number Diff line change
@@ -1,20 +1,15 @@
// Copyright The OpenTelemetry Authors
// SPDX-License-Identifier: Apache-2.0

//go:build !windows

// TODO review if tests should succeed on Windows

package docker

import (
"context"
"fmt"
"io"
"net"
"net/http"
"net/http/httptest"
"os"
"runtime"
"strings"
"sync"
"testing"
Expand Down Expand Up @@ -47,34 +42,14 @@ func TestInvalidExclude(t *testing.T) {
assert.Equal(t, "could not determine docker client excluded images: invalid glob item: unexpected end of input", err.Error())
}

func tmpSock(t *testing.T) (net.Listener, string) {
f, err := os.CreateTemp(os.TempDir(), "testsock")
if err != nil {
t.Fatal(err)
}
addr := f.Name()
assert.NoError(t, os.Remove(addr))

listener, err := net.Listen("unix", addr)
if err != nil {
t.Fatal(err)
}

return listener, addr
}

func TestWatchingTimeouts(t *testing.T) {
listener, addr := tmpSock(t)
listener, addr := testListener(t)
defer func() {
assert.NoError(t, listener.Close())
}()

defer func() {
assert.NoError(t, os.Remove(addr))
}()

config := &Config{
Endpoint: fmt.Sprintf("unix://%s", addr),
Endpoint: portableEndpoint(addr),
Timeout: 50 * time.Millisecond,
}

Expand Down Expand Up @@ -109,17 +84,14 @@ func TestWatchingTimeouts(t *testing.T) {
}

func TestFetchingTimeouts(t *testing.T) {
listener, addr := tmpSock(t)
listener, addr := testListener(t)

defer func() {
assert.NoError(t, listener.Close())
}()
defer func() {
assert.NoError(t, os.Remove(addr))
}()

config := &Config{
Endpoint: fmt.Sprintf("unix://%s", addr),
Endpoint: portableEndpoint(addr),
Timeout: 50 * time.Millisecond,
}

Expand Down Expand Up @@ -165,17 +137,13 @@ func TestFetchingTimeouts(t *testing.T) {
}

func TestToStatsJSONErrorHandling(t *testing.T) {
listener, addr := tmpSock(t)
listener, addr := testListener(t)
defer func() {
assert.NoError(t, listener.Close())
}()

defer func() {
assert.NoError(t, os.Remove(addr))
}()

config := &Config{
Endpoint: fmt.Sprintf("unix://%s", addr),
Endpoint: portableEndpoint(addr),
Timeout: 50 * time.Millisecond,
}

Expand Down Expand Up @@ -254,3 +222,11 @@ func TestEventLoopHandlesError(t *testing.T) {
return
}
}

func portableEndpoint(addr string) string {
endpoint := fmt.Sprintf("unix://%s", addr)
if runtime.GOOS == "windows" {
endpoint = fmt.Sprintf("npipe://%s", strings.ReplaceAll(addr, "\\", "/"))
}
return endpoint
}
29 changes: 29 additions & 0 deletions internal/docker/docker_test_listener.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,29 @@
// Copyright The OpenTelemetry Authors
// SPDX-License-Identifier: Apache-2.0
//go:build !windows

package docker

import (
"net"
"os"
"testing"

"github.com/stretchr/testify/require"
)

func testListener(t *testing.T) (net.Listener, string) {
f, err := os.CreateTemp(os.TempDir(), "testListener")
if err != nil {
t.Fatal(err)
}
addr := f.Name()
require.NoError(t, os.Remove(addr))

listener, err := net.Listen("unix", addr)
if err != nil {
t.Fatal(err)
}

return listener, addr
}
22 changes: 22 additions & 0 deletions internal/docker/docker_test_listener_windows.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,22 @@
// Copyright The OpenTelemetry Authors
// SPDX-License-Identifier: Apache-2.0
//go:build windows

package docker

import (
"net"
"testing"

"github.com/Microsoft/go-winio"
"github.com/stretchr/testify/require"
)

func testListener(t *testing.T) (net.Listener, string) {
addr := "\\\\.\\pipe\\testListener-otel-collector-contrib"

l, err := winio.ListenPipe(addr, nil)
require.NoError(t, err)
require.NotNil(t, l)
return l, addr
}
2 changes: 1 addition & 1 deletion internal/docker/go.mod
Original file line number Diff line number Diff line change
Expand Up @@ -11,7 +11,7 @@ require (
)

require (
github.com/Microsoft/go-winio v0.4.17 // indirect
github.com/Microsoft/go-winio v0.6.2 // indirect
github.com/containerd/log v0.1.0 // indirect
github.com/davecgh/go-spew v1.1.1 // indirect
github.com/distribution/reference v0.5.0 // indirect
Expand Down
8 changes: 2 additions & 6 deletions internal/docker/go.sum

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

Loading