Skip to content

Commit

Permalink
Don't send batch if no metrics were mapped (#30)
Browse files Browse the repository at this point in the history
  • Loading branch information
atanasovskib authored Aug 23, 2024
1 parent 2161cd8 commit 5e168f4
Show file tree
Hide file tree
Showing 2 changed files with 85 additions and 2 deletions.
5 changes: 5 additions & 0 deletions internal/exporter/exporter.go
Original file line number Diff line number Diff line change
Expand Up @@ -140,6 +140,11 @@ func (e *exporter) export(ctx context.Context) error {
}

batch := e.mapper.Map(metricFamilies)
if len(batch.Metrics) == 0 {
e.log.Warn("no metrics to export from activated metrics, scraped %d metrics from dcgm-exporter", len(metricFamilies))
return nil
}

if err := e.client.UploadBatch(ctx, batch); err != nil {
return fmt.Errorf("error whlie sending %d metrics to castai %w", len(batch.Metrics), err)
}
Expand Down
82 changes: 80 additions & 2 deletions internal/exporter/exporter_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -81,7 +81,13 @@ func TestExporter_Running(t *testing.T) {
},
}

batch := &pb.MetricsBatch{}
batch := &pb.MetricsBatch{
Metrics: []*pb.Metric{
{
Name: exporter.MetricGraphicsEngineActive,
},
},
}

scraper.EXPECT().Scrape(ctx, []string{"http://192.168.1.1:9400/metrics"}).Times(1).Return(metricFamilies, nil)
mapper.EXPECT().Map(metricFamilies).Times(1).Return(batch, nil)
Expand Down Expand Up @@ -148,7 +154,13 @@ func TestExporter_Running(t *testing.T) {
},
}

batch := &pb.MetricsBatch{}
batch := &pb.MetricsBatch{
Metrics: []*pb.Metric{
{
Name: exporter.MetricGraphicsEngineActive,
},
},
}

scraper.EXPECT().Scrape(ctx, []string{"http://localhost:9400/metrics"}).Times(1).Return(metricFamilies, nil)
mapper.EXPECT().Map(metricFamilies).Times(1).Return(batch, nil)
Expand All @@ -166,4 +178,70 @@ func TestExporter_Running(t *testing.T) {
r := require.New(t)
r.True(ex.Enabled())
})

t.Run("don't send empty batch of metrics", func(t *testing.T) {
ctx, cancel := context.WithCancel(context.Background())
defer cancel()

kubeClient := fake.NewSimpleClientset()

config := exporter.Config{
ExportInterval: 2 * time.Second,
DCGMExporterPort: 9400,
DCGMExporterPath: "/metrics",
DCGMExporterHost: "localhost",
Enabled: true,
}

scraper := mocks.NewMockScraper(t)
mapper := mocks.NewMockMetricMapper(t)
client := castai_mock.NewMockClient(t)

ex := exporter.NewExporter(config, kubeClient, log, scraper, mapper, client)
ex.Enable()

metricFamilies := []exporter.MetricFamilyMap{
{
"test_gauge": {
Type: dto.MetricType_GAUGE.Enum(),
Metric: []*dto.Metric{
{
Label: []*dto.LabelPair{
newLabelPair("label1", "value1"),
},
Gauge: newGauge(1.0),
},
},
},
exporter.MetricGraphicsEngineActive: {
Type: dto.MetricType_GAUGE.Enum(),
Metric: []*dto.Metric{
{
Label: []*dto.LabelPair{
newLabelPair("label1", "value1"),
},
Gauge: newGauge(1.0),
},
},
},
},
}

batch := &pb.MetricsBatch{}

scraper.EXPECT().Scrape(ctx, []string{"http://localhost:9400/metrics"}).Times(1).Return(metricFamilies, nil)
mapper.EXPECT().Map(metricFamilies).Times(1).Return(batch, nil)

go func() {
err := ex.Start(ctx)
if err != nil && !errors.Is(err, context.Canceled) {
t.Errorf("unexpected error: %v", err)
}
}()

time.Sleep(2400 * time.Millisecond)

r := require.New(t)
r.True(ex.Enabled())
})
}

0 comments on commit 5e168f4

Please sign in to comment.