Skip to content

Commit

Permalink
early return when there are no metrics
Browse files Browse the repository at this point in the history
  • Loading branch information
Achooo committed Apr 27, 2023
1 parent 0c190cb commit b1b4d96
Show file tree
Hide file tree
Showing 2 changed files with 36 additions and 2 deletions.
4 changes: 4 additions & 0 deletions agent/hcp/telemetry/otel_exporter.go
Original file line number Diff line number Diff line change
Expand Up @@ -41,6 +41,10 @@ func (e *OTELExporter) Aggregation(kind metric.InstrumentKind) aggregation.Aggre

// Export serializes and transmits metric data to a receiver.
func (e *OTELExporter) Export(ctx context.Context, metrics *metricdata.ResourceMetrics) error {
if len(metrics.ScopeMetrics) == 0 || len(metrics.ScopeMetrics[0].Metrics) == 0 {
return nil
}

otlpMetrics, merr := transformOTLP(metrics)
err := e.client.ExportMetrics(ctx, otlpMetrics, e.endpoint)
return multierror.Append(merr, err)
Expand Down
34 changes: 32 additions & 2 deletions agent/hcp/telemetry/otel_exporter_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -62,10 +62,35 @@ func TestExport(t *testing.T) {
metrics *metricdata.ResourceMetrics
client client.MetricsClient
}{
"earlyReturnWithoutScopeMetrics": {
client: &mockErrMetricsClient{},
metrics: &metricdata.ResourceMetrics{
Resource: resource.Empty(),
ScopeMetrics: []metricdata.ScopeMetrics{
{Metrics: []metricdata.Metrics{}},
},
},
},
"earlyReturnWithoutMetrics": {
client: &mockErrMetricsClient{},
metrics: &metricdata.ResourceMetrics{
Resource: resource.Empty(),
ScopeMetrics: []metricdata.ScopeMetrics{},
},
},
"errorWithExportFailure": {
client: &mockErrMetricsClient{},
metrics: &metricdata.ResourceMetrics{
Resource: resource.Empty(),
ScopeMetrics: []metricdata.ScopeMetrics{
{
Metrics: []metricdata.Metrics{
{
Name: "consul.raft.commitTime",
},
},
},
},
},
wantErr: "failed to export metrics",
},
Expand Down Expand Up @@ -110,8 +135,13 @@ func TestExport(t *testing.T) {
}

err := exp.Export(context.Background(), test.metrics)
require.Error(t, err)
require.Contains(t, err.Error(), test.wantErr)
if test.wantErr != "" {
require.Error(t, err)
require.Contains(t, err.Error(), test.wantErr)
return
}

require.NoError(t, err)
})
}
}
Expand Down

0 comments on commit b1b4d96

Please sign in to comment.