Skip to content

Commit

Permalink
chore: Localize chronicle exporter's metrics concerns (#2048)
Browse files Browse the repository at this point in the history
chore: Pull metrics-specific concerns into hostMetricsReporter
  • Loading branch information
djaglowski authored and Caleb-Hurshman committed Dec 17, 2024
1 parent 065923e commit f4443d6
Show file tree
Hide file tree
Showing 2 changed files with 28 additions and 44 deletions.
48 changes: 9 additions & 39 deletions exporter/chronicleexporter/exporter.go
Original file line number Diff line number Diff line change
Expand Up @@ -21,8 +21,6 @@ import (
"fmt"
"io"
"net/http"
"sync"
"time"

"github.com/google/uuid"
"github.com/observiq/bindplane-agent/exporter/chronicleexporter/protos/api"
Expand All @@ -48,17 +46,15 @@ const (
)

type chronicleExporter struct {
cfg *Config
set component.TelemetrySettings
marshaler logMarshaler
collectorID, exporterID string
cfg *Config
set component.TelemetrySettings
marshaler logMarshaler
exporterID string

// fields used for gRPC
grpcClient api.IngestionServiceV2Client
grpcConn *grpc.ClientConn
wg sync.WaitGroup
cancel context.CancelFunc
metrics *exporterMetrics
metrics *hostMetricsReporter

// fields used for HTTP
httpClient *http.Client
Expand All @@ -76,12 +72,10 @@ func newExporter(cfg *Config, params exporter.Settings, exporterID string) (*chr
}

return &chronicleExporter{
cfg: cfg,
set: params.TelemetrySettings,
metrics: newHostMetricsReporter(uuidCID[:], customerID[:], exporterID, cfg.Namespace),
marshaler: marshaller,
collectorID: collectorID,
exporterID: exporterID,
cfg: cfg,
set: params.TelemetrySettings,
marshaler: marshaller,
exporterID: exporterID,
}, nil
}

Expand Down Expand Up @@ -194,30 +188,6 @@ func (ce *chronicleExporter) buildOptions() []grpc.CallOption {
return opts
}

func (ce *chronicleExporter) startHostMetricsCollection(ctx context.Context) {
ticker := time.NewTicker(5 * time.Minute)
defer ticker.Stop()

defer ce.wg.Done()

for {
select {
case <-ctx.Done():
return
case <-ticker.C:
err := ce.metrics.collectHostMetrics()
if err != nil {
ce.set.Logger.Error("Failed to collect host metrics", zap.Error(err))
}
request := ce.metrics.getAndReset()
_, err = ce.grpcClient.BatchCreateEvents(ctx, request, ce.buildOptions()...)
if err != nil {
ce.set.Logger.Error("Failed to upload host metrics", zap.Error(err))
}
}
}
}

func (ce *chronicleExporter) logsHTTPDataPusher(ctx context.Context, ld plog.Logs) error {
payloads, err := ce.marshaler.MarshalRawLogsForHTTP(ctx, ld)
if err != nil {
Expand Down
24 changes: 19 additions & 5 deletions exporter/chronicleexporter/exporter_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -51,7 +51,25 @@ func TestLogsDataPusher(t *testing.T) {
marshaller.On("MarshalRawLogs", mock.Anything, mock.Anything).Return([]*api.BatchCreateLogsRequest{{}}, nil)
return &chronicleExporter{
cfg: &cfg,
metrics: newHostMetricsReporter([]byte{}, []byte{}, "", cfg.Namespace),
set: componenttest.NewNopTelemetrySettings(),
grpcClient: mockClient,
marshaler: marshaller,
}
},
setupMocks: func(mockClient *mocks.MockIngestionServiceV2Client) {
mockClient.EXPECT().BatchCreateLogs(gomock.Any(), gomock.Any(), gomock.Any()).Return(&api.BatchCreateLogsResponse{}, nil)
},
expectedErr: "",
},
{
desc: "successful push to Chronicle with metrics",
setupExporter: func() *chronicleExporter {
mockClient := mocks.NewMockIngestionServiceV2Client(gomock.NewController(t))
marshaller := NewMockMarshaler(t)
marshaller.On("MarshalRawLogs", mock.Anything, mock.Anything).Return([]*api.BatchCreateLogsRequest{{}}, nil)
cfg.CollectAgentMetrics = true
return &chronicleExporter{
cfg: &cfg,
set: componenttest.NewNopTelemetrySettings(),
grpcClient: mockClient,
marshaler: marshaller,
Expand All @@ -70,7 +88,6 @@ func TestLogsDataPusher(t *testing.T) {
marshaller.On("MarshalRawLogs", mock.Anything, mock.Anything).Return([]*api.BatchCreateLogsRequest{{}}, nil)
return &chronicleExporter{
cfg: &cfg,
metrics: newHostMetricsReporter([]byte{}, []byte{}, "", cfg.Namespace),
set: componenttest.NewNopTelemetrySettings(),
grpcClient: mockClient,
marshaler: marshaller,
Expand All @@ -91,7 +108,6 @@ func TestLogsDataPusher(t *testing.T) {
marshaller.On("MarshalRawLogs", mock.Anything, mock.Anything).Return([]*api.BatchCreateLogsRequest{{}}, nil)
return &chronicleExporter{
cfg: &cfg,
metrics: newHostMetricsReporter([]byte{}, []byte{}, "", cfg.Namespace),
set: componenttest.NewNopTelemetrySettings(),
grpcClient: mockClient,
marshaler: marshaller,
Expand All @@ -113,7 +129,6 @@ func TestLogsDataPusher(t *testing.T) {
marshaller.On("MarshalRawLogs", mock.Anything, mock.Anything).Return(nil, errors.New("marshal error"))
return &chronicleExporter{
cfg: &cfg,
metrics: newHostMetricsReporter([]byte{}, []byte{}, "", cfg.Namespace),
set: componenttest.NewNopTelemetrySettings(),
grpcClient: mockClient,
marshaler: marshaller,
Expand All @@ -133,7 +148,6 @@ func TestLogsDataPusher(t *testing.T) {
marshaller.On("MarshalRawLogs", mock.Anything, mock.Anything).Return([]*api.BatchCreateLogsRequest{}, nil)
return &chronicleExporter{
cfg: &cfg,
metrics: newHostMetricsReporter([]byte{}, []byte{}, "", cfg.Namespace),
set: componenttest.NewNopTelemetrySettings(),
grpcClient: mockClient,
marshaler: marshaller,
Expand Down

0 comments on commit f4443d6

Please sign in to comment.