Skip to content

Commit 013487f

Browse files
sean-breendhurley
andauthored
Move datasource/host to pkg (#1182)
* move internal/datasource/host to pkg * update fakes * warn when creating grpc connection * fix lint errors * Update error handling * update comment, move methods on Info together * fix import paths * fix lint issue --------- Co-authored-by: Donal Hurley <[email protected]>
1 parent 0e2f06b commit 013487f

23 files changed

+274
-213
lines changed

go.mod

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -78,7 +78,6 @@ require (
7878
go.uber.org/multierr v1.11.0
7979
go.uber.org/zap v1.27.0
8080
golang.org/x/mod v0.23.0
81-
golang.org/x/sync v0.13.0
8281
google.golang.org/protobuf v1.36.6
8382
)
8483

@@ -292,6 +291,7 @@ require (
292291
go.opentelemetry.io/proto/otlp v1.5.0 // indirect
293292
golang.org/x/arch v0.12.0 // indirect
294293
golang.org/x/exp v0.0.0-20250210185358-939b2ce775ac // indirect
294+
golang.org/x/sync v0.13.0 // indirect
295295
golang.org/x/tools v0.30.0 // indirect
296296
gonum.org/v1/gonum v0.16.0 // indirect
297297
google.golang.org/genproto/googleapis/api v0.0.0-20250303144028-a0af3efb3deb // indirect

internal/config/config.go

Lines changed: 7 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -20,8 +20,9 @@ import (
2020
"strings"
2121
"time"
2222

23+
"github.com/nginx/agent/v3/pkg/host"
24+
2325
"github.com/nginx/agent/v3/internal/datasource/file"
24-
"github.com/nginx/agent/v3/internal/datasource/host"
2526
"github.com/nginx/agent/v3/internal/logger"
2627

2728
"github.com/goccy/go-yaml"
@@ -285,7 +286,11 @@ func addDefaultProcessors(collector *Collector) {
285286
}
286287

287288
func addDefaultHostMetricsReceiver(collector *Collector) {
288-
if host.NewInfo().IsContainer() {
289+
isContainer, err := host.NewInfo().IsContainer()
290+
if err != nil {
291+
slog.Debug("No container information found", "error", err)
292+
}
293+
if isContainer {
289294
addDefaultContainerHostMetricsReceiver(collector)
290295
} else {
291296
addDefaultVMHostMetricsReceiver(collector)

internal/datasource/nginx/process.go

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -13,8 +13,8 @@ import (
1313
"regexp"
1414
"strings"
1515

16-
"github.com/nginx/agent/v3/internal/datasource/host/exec"
1716
"github.com/nginx/agent/v3/internal/model"
17+
"github.com/nginx/agent/v3/pkg/host/exec"
1818
"github.com/nginx/agent/v3/pkg/nginxprocess"
1919
)
2020

internal/datasource/nginx/process_test.go

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -11,7 +11,7 @@ import (
1111
"errors"
1212
"testing"
1313

14-
"github.com/nginx/agent/v3/internal/datasource/host/exec/execfakes"
14+
"github.com/nginx/agent/v3/pkg/host/exec/execfakes"
1515
"github.com/stretchr/testify/assert"
1616
)
1717

internal/grpc/grpc.go

Lines changed: 7 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -16,6 +16,8 @@ import (
1616
"strings"
1717
"sync"
1818

19+
"github.com/nginx/agent/v3/pkg/host"
20+
1921
"github.com/nginx/agent/v3/internal/datasource/file"
2022

2123
"github.com/cenkalti/backoff/v4"
@@ -27,7 +29,6 @@ import (
2729

2830
mpi "github.com/nginx/agent/v3/api/grpc/mpi/v1"
2931
"github.com/nginx/agent/v3/internal/config"
30-
"github.com/nginx/agent/v3/internal/datasource/host"
3132
"google.golang.org/grpc"
3233
"google.golang.org/grpc/codes"
3334
"google.golang.org/grpc/keepalive"
@@ -87,10 +88,13 @@ func NewGrpcConnection(ctx context.Context, agentConfig *config.Config,
8788

8889
slog.InfoContext(ctx, "Dialing grpc server", "server_addr", serverAddr)
8990

91+
var err error
9092
info := host.NewInfo()
91-
resourceID := info.ResourceID(ctx)
93+
resourceID, err := info.ResourceID(ctx)
94+
if err != nil {
95+
slog.WarnContext(ctx, "Failed to get ResourceID from host info", "error", err.Error())
96+
}
9297

93-
var err error
9498
grpcConnection.mutex.Lock()
9599
grpcConnection.conn, err = grpc.NewClient(serverAddr, DialOptions(agentConfig, commandConfig, resourceID)...)
96100
grpcConnection.mutex.Unlock()

internal/resource/nginx_instance_operator.go

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -16,9 +16,10 @@ import (
1616
"github.com/nginx/agent/v3/internal/backoff"
1717
"github.com/nginx/agent/v3/pkg/nginxprocess"
1818

19+
"github.com/nginx/agent/v3/pkg/host/exec"
20+
1921
mpi "github.com/nginx/agent/v3/api/grpc/mpi/v1"
2022
"github.com/nginx/agent/v3/internal/config"
21-
"github.com/nginx/agent/v3/internal/datasource/host/exec"
2223
)
2324

2425
type NginxInstanceOperator struct {

internal/resource/nginx_instance_operator_test.go

Lines changed: 3 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -15,15 +15,13 @@ import (
1515
"testing"
1616
"time"
1717

18+
"github.com/nginx/agent/v3/internal/config"
1819
"github.com/nginx/agent/v3/internal/resource/resourcefakes"
19-
20-
"github.com/nginx/agent/v3/test/stub"
21-
2220
"github.com/nginx/agent/v3/pkg/nginxprocess"
21+
"github.com/nginx/agent/v3/test/stub"
2322

24-
"github.com/nginx/agent/v3/internal/config"
23+
"github.com/nginx/agent/v3/pkg/host/exec/execfakes"
2524

26-
"github.com/nginx/agent/v3/internal/datasource/host/exec/execfakes"
2725
"github.com/nginx/agent/v3/test/helpers"
2826
"github.com/nginx/agent/v3/test/protos"
2927
"github.com/nginx/agent/v3/test/types"

internal/resource/nginx_instance_process_operator.go

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -10,8 +10,8 @@ import (
1010
"errors"
1111
"log/slog"
1212

13-
"github.com/nginx/agent/v3/internal/datasource/host/exec"
1413
"github.com/nginx/agent/v3/internal/datasource/nginx"
14+
"github.com/nginx/agent/v3/pkg/host/exec"
1515
"github.com/nginx/agent/v3/pkg/id"
1616

1717
"github.com/nginx/agent/v3/pkg/nginxprocess"

internal/resource/nginx_instance_process_operator_test.go

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -13,7 +13,7 @@ import (
1313
"testing"
1414
"time"
1515

16-
"github.com/nginx/agent/v3/internal/datasource/host/exec/execfakes"
16+
"github.com/nginx/agent/v3/pkg/host/exec/execfakes"
1717
"github.com/nginx/agent/v3/pkg/nginxprocess"
1818
"github.com/stretchr/testify/assert"
1919
"github.com/stretchr/testify/require"

internal/resource/resource_service.go

Lines changed: 19 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -19,10 +19,11 @@ import (
1919
"strings"
2020
"sync"
2121

22-
"github.com/nginx/agent/v3/internal/datasource/host/exec"
23-
22+
"github.com/nginx/agent/v3/pkg/host/exec"
2423
"github.com/nginx/agent/v3/pkg/nginxprocess"
2524

25+
"github.com/nginx/agent/v3/pkg/host"
26+
2627
parser "github.com/nginx/agent/v3/internal/datasource/config"
2728
datasource "github.com/nginx/agent/v3/internal/datasource/proto"
2829
"github.com/nginx/agent/v3/internal/model"
@@ -34,8 +35,6 @@ import (
3435

3536
"github.com/nginx/agent/v3/internal/config"
3637

37-
"github.com/nginx/agent/v3/internal/datasource/host"
38-
3938
mpi "github.com/nginx/agent/v3/api/grpc/mpi/v1"
4039
)
4140

@@ -398,12 +397,25 @@ func (r *ResourceService) updateResourceInfo(ctx context.Context) {
398397
r.resourceMutex.Lock()
399398
defer r.resourceMutex.Unlock()
400399

401-
if r.info.IsContainer() {
402-
r.resource.Info = r.info.ContainerInfo(ctx)
400+
isContainer, err := r.info.IsContainer()
401+
if err != nil {
402+
slog.WarnContext(ctx, "Failed to check if resource is container", "error", err)
403+
}
404+
405+
if isContainer {
406+
r.resource.Info, err = r.info.ContainerInfo(ctx)
407+
if err != nil {
408+
slog.ErrorContext(ctx, "Failed to get container info", "error", err)
409+
return
410+
}
403411
r.resource.ResourceId = r.resource.GetContainerInfo().GetContainerId()
404412
r.resource.Instances = []*mpi.Instance{}
405413
} else {
406-
r.resource.Info = r.info.HostInfo(ctx)
414+
r.resource.Info, err = r.info.HostInfo(ctx)
415+
if err != nil {
416+
slog.ErrorContext(ctx, "Failed to get host info", "error", err)
417+
return
418+
}
407419
r.resource.ResourceId = r.resource.GetHostInfo().GetHostId()
408420
r.resource.Instances = []*mpi.Instance{}
409421
}

0 commit comments

Comments
 (0)