Skip to content

Commit

Permalink
Add a general isEmpty method
Browse files Browse the repository at this point in the history
  • Loading branch information
Achooo committed May 10, 2023
1 parent be0b01b commit 02be2a6
Show file tree
Hide file tree
Showing 2 changed files with 18 additions and 2 deletions.
3 changes: 1 addition & 2 deletions agent/hcp/telemetry/otel_exporter.go
Original file line number Diff line number Diff line change
Expand Up @@ -53,8 +53,7 @@ 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 {
otlpMetrics := transformOTLP(&metrics)
emptyMetrics := len(otlpMetrics.ScopeMetrics) == 0 || len(metrics.ScopeMetrics[0].Metrics) == 0
if emptyMetrics {
if isEmpty(otlpMetrics) {
return nil
}
return e.client.ExportMetrics(ctx, otlpMetrics, e.url.String())
Expand Down
17 changes: 17 additions & 0 deletions agent/hcp/telemetry/otlp_transform.go
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,23 @@ import (
rpb "go.opentelemetry.io/proto/otlp/resource/v1"
)

func isEmpty(rm *mpb.ResourceMetrics) bool {
// No ScopeMetrics
if len(rm.ScopeMetrics) == 0 {
return true
}

// If any inner metrics contain data, return false.
for _, v := range rm.ScopeMetrics {
if len(v.Metrics) != 0 {
return false
}
}

// All inner metrics are empty.
return true
}

// TransformOTLP returns an OTLP ResourceMetrics generated from OTEL metrics. If rm
// contains invalid ScopeMetrics, an error will be returned along with an OTLP
// ResourceMetrics that contains partial OTLP ScopeMetrics.
Expand Down

0 comments on commit 02be2a6

Please sign in to comment.