Skip to content

Commit cf0a56a

Browse files
authored
fix(chronicleexporter): fix metrics bugs and simplify logic (#3272)
fix(chronicleexporter): recordSent bugs and cleanup
1 parent 24eb777 commit cf0a56a

2 files changed

Lines changed: 60 additions & 63 deletions

File tree

exporter/chronicleexporter/grpc_exporter.go

Lines changed: 0 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -188,11 +188,6 @@ func (exp *grpcExporter) countAndReportBatchBytes(ctx context.Context, payloads
188188
}
189189

190190
func (exp *grpcExporter) uploadToChronicle(ctx context.Context, request *api.BatchCreateLogsRequest) error {
191-
if exp.metrics != nil {
192-
totalLogs := int64(len(request.GetBatch().GetEntries()))
193-
defer exp.metrics.recordSent(totalLogs)
194-
}
195-
196191
// Track request latency
197192
start := time.Now()
198193

exporter/chronicleexporter/hostmetrics.go

Lines changed: 60 additions & 58 deletions
Original file line numberDiff line numberDiff line change
@@ -36,16 +36,13 @@ type hostMetricsReporter struct {
3636
wg sync.WaitGroup
3737
send sendMetricsFunc
3838

39-
mutex sync.Mutex
40-
agentID []byte
41-
customerID []byte
42-
exporterID string
43-
namespace string
44-
startTime *timestamppb.Timestamp
45-
stats *api.AgentStatsEvent
46-
logsDropped int64
47-
logsSent int64
48-
licenseType string
39+
mutex sync.Mutex
40+
agentID []byte
41+
exporterID string
42+
source *api.EventSource
43+
44+
startTime *timestamppb.Timestamp
45+
agentStats *api.AgentStatsEvent
4946
}
5047

5148
type sendMetricsFunc func(context.Context, *api.BatchCreateEventsRequest) error
@@ -58,7 +55,6 @@ func newHostMetricsReporter(cfg *Config, set component.TelemetrySettings, export
5855

5956
agentID := uuid.New()
6057
if sid, ok := set.Resource.Attributes().Get(string(semconv.ServiceInstanceIDKey)); ok {
61-
var err error
6258
serviceID, err := uuid.Parse(sid.AsString())
6359
if err != nil {
6460
set.Logger.Error("Failed to parse service instance ID, using random ID", zap.String("service_instance_id", sid.AsString()), zap.Error(err))
@@ -68,21 +64,21 @@ func newHostMetricsReporter(cfg *Config, set component.TelemetrySettings, export
6864
}
6965

7066
now := timestamppb.Now()
71-
return &hostMetricsReporter{
67+
hmr := &hostMetricsReporter{
7268
set: set,
7369
send: send,
7470
agentID: agentID[:],
7571
exporterID: exporterID,
76-
startTime: now,
77-
customerID: customerID[:],
78-
namespace: cfg.Namespace,
79-
stats: &api.AgentStatsEvent{
80-
AgentId: agentID[:],
81-
WindowStartTime: now,
82-
StartTime: now,
72+
source: &api.EventSource{
73+
CustomerId: customerID[:],
74+
CollectorId: getCollectorID(cfg.LicenseType),
75+
Namespace: cfg.Namespace,
8376
},
84-
licenseType: cfg.LicenseType,
85-
}, nil
77+
startTime: now,
78+
}
79+
80+
hmr.resetWindow(now)
81+
return hmr, nil
8682
}
8783

8884
func (hmr *hostMetricsReporter) start() {
@@ -107,48 +103,44 @@ func (hmr *hostMetricsReporter) start() {
107103
if err != nil {
108104
hmr.set.Logger.Error("Failed to collect host metrics", zap.Error(err))
109105
}
110-
request := hmr.getAndReset()
111-
if err = hmr.send(ctx, request); err != nil {
106+
request := hmr.buildRequest()
107+
err = hmr.send(ctx, request)
108+
if err != nil {
112109
hmr.set.Logger.Error("Failed to upload host metrics", zap.Error(err))
110+
} else {
111+
hmr.resetWindow(timestamppb.Now())
113112
}
114113
}
115114
}
116115
}()
117116
}
118117

119-
func (hmr *hostMetricsReporter) getAndReset() *api.BatchCreateEventsRequest {
118+
// buildRequest builds the create events request object
119+
func (hmr *hostMetricsReporter) buildRequest() *api.BatchCreateEventsRequest {
120120
hmr.mutex.Lock()
121121
defer hmr.mutex.Unlock()
122122

123123
now := timestamppb.Now()
124124
batchID := uuid.New()
125-
source := &api.EventSource{
126-
CollectorId: []byte(getCollectorID(hmr.licenseType)),
127-
Namespace: hmr.namespace,
128-
CustomerId: hmr.customerID,
129-
}
130125

131-
request := &api.BatchCreateEventsRequest{
126+
return &api.BatchCreateEventsRequest{
132127
Batch: &api.EventBatch{
133128
Id: batchID[:],
134-
Source: source,
129+
Source: hmr.source,
135130
Type: api.EventBatch_AGENT_STATS,
136131
StartTime: hmr.startTime,
137132
Events: []*api.Event{
138133
{
139134
Timestamp: now,
140135
CollectionTime: now,
141-
Source: source,
136+
Source: hmr.source,
142137
Payload: &api.Event_AgentStats{
143-
AgentStats: hmr.stats,
138+
AgentStats: hmr.agentStats,
144139
},
145140
},
146141
},
147142
},
148143
}
149-
150-
hmr.resetStats()
151-
return request
152144
}
153145

154146
func (hmr *hostMetricsReporter) shutdown() {
@@ -158,23 +150,7 @@ func (hmr *hostMetricsReporter) shutdown() {
158150
}
159151
}
160152

161-
func (hmr *hostMetricsReporter) resetStats() {
162-
hmr.stats = &api.AgentStatsEvent{
163-
ExporterStats: []*api.ExporterStats{
164-
{
165-
Name: hmr.exporterID,
166-
AcceptedSpans: hmr.logsSent,
167-
RefusedSpans: hmr.logsDropped,
168-
},
169-
},
170-
AgentId: hmr.agentID,
171-
StartTime: hmr.startTime,
172-
WindowStartTime: timestamppb.Now(),
173-
}
174-
hmr.logsDropped = 0
175-
hmr.logsSent = 0
176-
}
177-
153+
// collectHostMetrics collects the host metrics and updates the agent stats object
178154
func (hmr *hostMetricsReporter) collectHostMetrics() error {
179155
hmr.mutex.Lock()
180156
defer hmr.mutex.Unlock()
@@ -190,14 +166,14 @@ func (hmr *hostMetricsReporter) collectHostMetrics() error {
190166
if err != nil {
191167
return fmt.Errorf("get cpu times: %w", err)
192168
}
193-
hmr.stats.ProcessCpuSeconds = int64(cpuTimes.User + cpuTimes.System)
169+
hmr.agentStats.ProcessCpuSeconds = int64(cpuTimes.User + cpuTimes.System)
194170

195171
// Collect memory usage (RSS)
196172
memInfo, err := proc.MemoryInfo()
197173
if err != nil {
198174
return fmt.Errorf("get memory info: %w", err)
199175
}
200-
hmr.stats.ProcessMemoryRss = int64(memInfo.RSS / 1024) // Convert bytes to kilobytes
176+
hmr.agentStats.ProcessMemoryRss = int64(memInfo.RSS / 1024) // Convert bytes to kilobytes
201177

202178
// Calculate process uptime
203179
startTimeMs, err := proc.CreateTime()
@@ -206,14 +182,40 @@ func (hmr *hostMetricsReporter) collectHostMetrics() error {
206182
}
207183
startTimeSec := startTimeMs / 1000
208184
currentTimeSec := time.Now().Unix()
209-
hmr.stats.ProcessUptime = currentTimeSec - startTimeSec
185+
hmr.agentStats.ProcessUptime = currentTimeSec - startTimeSec
210186

211187
return nil
212188
}
213189

190+
// resetWindow resets the agent stats object and sets the window start time
191+
func (hmr *hostMetricsReporter) resetWindow(windowStartTime *timestamppb.Timestamp) {
192+
hmr.mutex.Lock()
193+
defer hmr.mutex.Unlock()
194+
195+
hmr.agentStats = &api.AgentStatsEvent{
196+
AgentId: hmr.agentID,
197+
StartTime: hmr.startTime,
198+
WindowStartTime: windowStartTime,
199+
ExporterStats: []*api.ExporterStats{
200+
{
201+
Name: hmr.exporterID,
202+
},
203+
},
204+
}
205+
}
206+
214207
func (hmr *hostMetricsReporter) recordSent(count int64) {
215208
hmr.mutex.Lock()
216209
defer hmr.mutex.Unlock()
217-
hmr.logsSent += count
218-
hmr.stats.LastSuccessfulUploadTime = timestamppb.Now()
210+
211+
if len(hmr.agentStats.ExporterStats) == 0 {
212+
hmr.agentStats.ExporterStats = []*api.ExporterStats{
213+
{
214+
Name: hmr.exporterID,
215+
},
216+
}
217+
}
218+
219+
hmr.agentStats.ExporterStats[0].AcceptedSpans += count
220+
hmr.agentStats.LastSuccessfulUploadTime = timestamppb.Now()
219221
}

0 commit comments

Comments
 (0)