Skip to content

Commit 4d97bc2

Browse files
authored
Fix default docker_observer endpoint on Windows (#34358)
**Description:** The default endpoint for the `docker_observer` is incorrect on Windows: it should be `npipe:////./pipe/docker_engine` on Windows. Without this change the observer fails on its default configuration on Windows. Updated `extension/observer/dockerobserver` and `internal/docker` (including respective tests) to have a per OS default value for endpoint that allows the observer to work with default config on Windows. **Link to tracking Issue:** N/A **Testing:** Manual tests using the following config: ```yaml extensions: docker_observer: receivers: receiver_creator: watch_observers: [docker_observer] receivers: nginx: rule: type == "container" and name matches "nginx" and port == 80 config: endpoint: '`endpoint`/status' collection_interval: 10s exporters: debug: verbosity: detailed service: extensions: [docker_observer] pipelines: logs: receivers: [receiver_creator] processors: [] exporters: [debug] ``` **Documentation:** - [X] Updated readme files - [X] chloggen (~~pending PR number~~)
1 parent 8c8ffb4 commit 4d97bc2

File tree

14 files changed

+112
-76
lines changed

14 files changed

+112
-76
lines changed
Lines changed: 27 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,27 @@
1+
# Use this changelog template to create an entry for release notes.
2+
3+
# One of 'breaking', 'deprecation', 'new_component', 'enhancement', 'bug_fix'
4+
change_type: 'bug_fix'
5+
6+
# The name of the component, or a single word describing the area of concern, (e.g. filelogreceiver)
7+
component: docker_observer
8+
9+
# A brief description of the change. Surround your text with quotes ("") if it needs to start with a backtick (`).
10+
note: Change default endpoint for `docker_observer` on Windows to `npipe:////./pipe/docker_engine`
11+
12+
# Mandatory: One or more tracking issues related to the change. You can use the PR number here if no issue exists.
13+
issues: [34358]
14+
15+
# (Optional) One or more lines of additional information to render under the primary note.
16+
# These lines will be padded with 2 spaces and then inserted directly into the document.
17+
# Use pipe (|) for multiline entries.
18+
subtext:
19+
20+
# If your change doesn't affect end users or the exported elements of any package,
21+
# you should instead start your pull request title with [chore] or use the "Skip Changelog" label.
22+
# Optional: The change log or logs in which this entry should be included.
23+
# e.g. '[user]' or '[user, api]'
24+
# Include 'user' if the change is relevant to end users.
25+
# Include 'api' if there is a change to a library API.
26+
# Default: '[user]'
27+
change_logs: []

extension/observer/dockerobserver/README.md

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -19,15 +19,15 @@ This observer watches the Docker engine's stream of events to dynamically create
1919
Requires Docker API Version 1.24+.
2020

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

2424

2525
## Example Config
2626

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

5454
The URL of the docker server.
5555

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

5858
### `timeout`
5959

extension/observer/dockerobserver/config.go

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -16,7 +16,8 @@ import (
1616
// Config defines configuration for docker observer
1717
type Config struct {
1818

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

2223
// The maximum amount of time to wait for docker API responses. Default is 5s

extension/observer/dockerobserver/factory.go

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -7,6 +7,7 @@ import (
77
"context"
88
"time"
99

10+
"github.com/docker/docker/client"
1011
"go.opentelemetry.io/collector/component"
1112
"go.opentelemetry.io/collector/extension"
1213

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

2627
func createDefaultConfig() component.Config {
2728
return &Config{
28-
Endpoint: "unix:///var/run/docker.sock",
29+
Endpoint: client.DefaultDockerHost,
2930
Timeout: 5 * time.Second,
3031
CacheSyncInterval: 60 * time.Minute,
3132
DockerAPIVersion: defaultDockerAPIVersion,

extension/observer/dockerobserver/go.mod

Lines changed: 1 addition & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -20,7 +20,7 @@ require (
2020
require (
2121
dario.cat/mergo v1.0.0 // indirect
2222
github.com/Azure/go-ansiterm v0.0.0-20210617225240-d185dfc1b5a1 // indirect
23-
github.com/Microsoft/go-winio v0.6.1 // indirect
23+
github.com/Microsoft/go-winio v0.6.2 // indirect
2424
github.com/Microsoft/hcsshim v0.11.4 // indirect
2525
github.com/beorn7/perks v1.0.1 // indirect
2626
github.com/cenkalti/backoff/v4 v4.2.1 // indirect
@@ -83,12 +83,9 @@ require (
8383
go.opentelemetry.io/otel/trace v1.28.0 // indirect
8484
go.uber.org/multierr v1.11.0 // indirect
8585
golang.org/x/crypto v0.24.0 // indirect
86-
golang.org/x/mod v0.17.0 // indirect
8786
golang.org/x/net v0.26.0 // indirect
88-
golang.org/x/sync v0.7.0 // indirect
8987
golang.org/x/sys v0.21.0 // indirect
9088
golang.org/x/text v0.16.0 // indirect
91-
golang.org/x/tools v0.21.1-0.20240508182429-e35e4ccd0d2d // indirect
9289
google.golang.org/genproto/googleapis/rpc v0.0.0-20240528184218-531527333157 // indirect
9390
google.golang.org/grpc v1.65.0 // indirect
9491
google.golang.org/protobuf v1.34.2 // indirect

extension/observer/dockerobserver/go.sum

Lines changed: 2 additions & 8 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

internal/docker/config.go

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -11,10 +11,12 @@ import (
1111
"time"
1212

1313
"github.com/docker/docker/api/types/versions"
14+
"github.com/docker/docker/client"
1415
)
1516

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

2022
// The maximum amount of time to wait for docker API responses. Default is 5s
@@ -43,7 +45,7 @@ func NewConfig(endpoint string, timeout time.Duration, excludedImages []string,
4345
// to be used when creating a docker client
4446
func NewDefaultConfig() *Config {
4547
cfg := &Config{
46-
Endpoint: "unix:///var/run/docker.sock",
48+
Endpoint: client.DefaultDockerHost,
4749
Timeout: 5 * time.Second,
4850
DockerAPIVersion: minimumRequiredDockerAPIVersion,
4951
}

internal/docker/docker_test.go

Lines changed: 15 additions & 39 deletions
Original file line numberDiff line numberDiff line change
@@ -1,20 +1,15 @@
11
// Copyright The OpenTelemetry Authors
22
// SPDX-License-Identifier: Apache-2.0
33

4-
//go:build !windows
5-
6-
// TODO review if tests should succeed on Windows
7-
84
package docker
95

106
import (
117
"context"
128
"fmt"
139
"io"
14-
"net"
1510
"net/http"
1611
"net/http/httptest"
17-
"os"
12+
"runtime"
1813
"strings"
1914
"sync"
2015
"testing"
@@ -47,34 +42,14 @@ func TestInvalidExclude(t *testing.T) {
4742
assert.Equal(t, "could not determine docker client excluded images: invalid glob item: unexpected end of input", err.Error())
4843
}
4944

50-
func tmpSock(t *testing.T) (net.Listener, string) {
51-
f, err := os.CreateTemp(os.TempDir(), "testsock")
52-
if err != nil {
53-
t.Fatal(err)
54-
}
55-
addr := f.Name()
56-
assert.NoError(t, os.Remove(addr))
57-
58-
listener, err := net.Listen("unix", addr)
59-
if err != nil {
60-
t.Fatal(err)
61-
}
62-
63-
return listener, addr
64-
}
65-
6645
func TestWatchingTimeouts(t *testing.T) {
67-
listener, addr := tmpSock(t)
46+
listener, addr := testListener(t)
6847
defer func() {
6948
assert.NoError(t, listener.Close())
7049
}()
7150

72-
defer func() {
73-
assert.NoError(t, os.Remove(addr))
74-
}()
75-
7651
config := &Config{
77-
Endpoint: fmt.Sprintf("unix://%s", addr),
52+
Endpoint: portableEndpoint(addr),
7853
Timeout: 50 * time.Millisecond,
7954
}
8055

@@ -109,17 +84,14 @@ func TestWatchingTimeouts(t *testing.T) {
10984
}
11085

11186
func TestFetchingTimeouts(t *testing.T) {
112-
listener, addr := tmpSock(t)
87+
listener, addr := testListener(t)
11388

11489
defer func() {
11590
assert.NoError(t, listener.Close())
11691
}()
117-
defer func() {
118-
assert.NoError(t, os.Remove(addr))
119-
}()
12092

12193
config := &Config{
122-
Endpoint: fmt.Sprintf("unix://%s", addr),
94+
Endpoint: portableEndpoint(addr),
12395
Timeout: 50 * time.Millisecond,
12496
}
12597

@@ -165,17 +137,13 @@ func TestFetchingTimeouts(t *testing.T) {
165137
}
166138

167139
func TestToStatsJSONErrorHandling(t *testing.T) {
168-
listener, addr := tmpSock(t)
140+
listener, addr := testListener(t)
169141
defer func() {
170142
assert.NoError(t, listener.Close())
171143
}()
172144

173-
defer func() {
174-
assert.NoError(t, os.Remove(addr))
175-
}()
176-
177145
config := &Config{
178-
Endpoint: fmt.Sprintf("unix://%s", addr),
146+
Endpoint: portableEndpoint(addr),
179147
Timeout: 50 * time.Millisecond,
180148
}
181149

@@ -254,3 +222,11 @@ func TestEventLoopHandlesError(t *testing.T) {
254222
return
255223
}
256224
}
225+
226+
func portableEndpoint(addr string) string {
227+
endpoint := fmt.Sprintf("unix://%s", addr)
228+
if runtime.GOOS == "windows" {
229+
endpoint = fmt.Sprintf("npipe://%s", strings.ReplaceAll(addr, "\\", "/"))
230+
}
231+
return endpoint
232+
}
Lines changed: 29 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,29 @@
1+
// Copyright The OpenTelemetry Authors
2+
// SPDX-License-Identifier: Apache-2.0
3+
//go:build !windows
4+
5+
package docker // import "github.com/open-telemetry/opentelemetry-collector-contrib/internal/docker"
6+
7+
import (
8+
"net"
9+
"os"
10+
"testing"
11+
12+
"github.com/stretchr/testify/require"
13+
)
14+
15+
func testListener(t *testing.T) (net.Listener, string) {
16+
f, err := os.CreateTemp(os.TempDir(), "testListener")
17+
if err != nil {
18+
t.Fatal(err)
19+
}
20+
addr := f.Name()
21+
require.NoError(t, os.Remove(addr))
22+
23+
listener, err := net.Listen("unix", addr)
24+
if err != nil {
25+
t.Fatal(err)
26+
}
27+
28+
return listener, addr
29+
}
Lines changed: 22 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,22 @@
1+
// Copyright The OpenTelemetry Authors
2+
// SPDX-License-Identifier: Apache-2.0
3+
//go:build windows
4+
5+
package docker // import "github.com/open-telemetry/opentelemetry-collector-contrib/internal/docker"
6+
7+
import (
8+
"net"
9+
"testing"
10+
11+
"github.com/Microsoft/go-winio"
12+
"github.com/stretchr/testify/require"
13+
)
14+
15+
func testListener(t *testing.T) (net.Listener, string) {
16+
addr := "\\\\.\\pipe\\testListener-otel-collector-contrib"
17+
18+
l, err := winio.ListenPipe(addr, nil)
19+
require.NoError(t, err)
20+
require.NotNil(t, l)
21+
return l, addr
22+
}

internal/docker/go.mod

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -3,6 +3,7 @@ module github.com/open-telemetry/opentelemetry-collector-contrib/internal/docker
33
go 1.21.0
44

55
require (
6+
github.com/Microsoft/go-winio v0.6.2
67
github.com/docker/docker v26.1.5+incompatible
78
github.com/gobwas/glob v0.2.3
89
github.com/stretchr/testify v1.9.0
@@ -11,7 +12,6 @@ require (
1112
)
1213

1314
require (
14-
github.com/Microsoft/go-winio v0.4.17 // indirect
1515
github.com/containerd/log v0.1.0 // indirect
1616
github.com/davecgh/go-spew v1.1.1 // indirect
1717
github.com/distribution/reference v0.5.0 // indirect

internal/docker/go.sum

Lines changed: 2 additions & 6 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

receiver/dockerstatsreceiver/go.mod

Lines changed: 1 addition & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -26,7 +26,7 @@ require (
2626
require (
2727
dario.cat/mergo v1.0.0 // indirect
2828
github.com/Azure/go-ansiterm v0.0.0-20210617225240-d185dfc1b5a1 // indirect
29-
github.com/Microsoft/go-winio v0.6.1 // indirect
29+
github.com/Microsoft/go-winio v0.6.2 // indirect
3030
github.com/Microsoft/hcsshim v0.11.4 // indirect
3131
github.com/beorn7/perks v1.0.1 // indirect
3232
github.com/cenkalti/backoff/v4 v4.3.0 // indirect
@@ -95,12 +95,9 @@ require (
9595
go.opentelemetry.io/otel/sdk/metric v1.28.0 // indirect
9696
go.opentelemetry.io/otel/trace v1.28.0 // indirect
9797
golang.org/x/crypto v0.24.0 // indirect
98-
golang.org/x/mod v0.17.0 // indirect
9998
golang.org/x/net v0.26.0 // indirect
100-
golang.org/x/sync v0.7.0 // indirect
10199
golang.org/x/sys v0.21.0 // indirect
102100
golang.org/x/text v0.16.0 // indirect
103-
golang.org/x/tools v0.21.1-0.20240508182429-e35e4ccd0d2d // indirect
104101
google.golang.org/genproto/googleapis/rpc v0.0.0-20240701130421-f6361c86f094 // indirect
105102
google.golang.org/grpc v1.65.0 // indirect
106103
google.golang.org/protobuf v1.34.2 // indirect

0 commit comments

Comments
 (0)