-
Notifications
You must be signed in to change notification settings - Fork 2.4k
/
metadata.go
50 lines (40 loc) · 1.4 KB
/
metadata.go
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
// Copyright The OpenTelemetry Authors
// SPDX-License-Identifier: Apache-2.0
package docker // import "github.com/open-telemetry/opentelemetry-collector-contrib/internal/metadataproviders/docker"
import (
"context"
"fmt"
"github.com/docker/docker/client"
"github.com/open-telemetry/opentelemetry-collector-contrib/internal/metadataproviders/internal"
)
type Provider interface {
// Hostname returns the OS hostname
Hostname(context.Context) (string, error)
// OSType returns the host operating system
OSType(context.Context) (string, error)
}
type dockerProviderImpl struct {
dockerClient *client.Client
}
func NewProvider(opts ...client.Opt) (Provider, error) {
opts = append(opts, client.FromEnv, client.WithAPIVersionNegotiation())
cli, err := client.NewClientWithOpts(opts...)
if err != nil {
return nil, fmt.Errorf("could not initialize Docker client: %w", err)
}
return &dockerProviderImpl{dockerClient: cli}, nil
}
func (d *dockerProviderImpl) Hostname(ctx context.Context) (string, error) {
info, err := d.dockerClient.Info(ctx)
if err != nil {
return "", fmt.Errorf("failed to fetch Docker information: %w", err)
}
return info.Name, nil
}
func (d *dockerProviderImpl) OSType(ctx context.Context) (string, error) {
info, err := d.dockerClient.Info(ctx)
if err != nil {
return "", fmt.Errorf("failed to fetch Docker OS type: %w", err)
}
return internal.GOOSToOSType(info.OSType), nil
}