Skip to content
Draft
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
20 changes: 14 additions & 6 deletions exporters/prometheus/exporter.go
Original file line number Diff line number Diff line change
Expand Up @@ -42,7 +42,9 @@ const (
)

var (
errScopeInvalid = errors.New("invalid scope")
errScopeInvalid = errors.New("invalid scope")
errEmptyOtelScopeInfo = errors.New("empty otel_scope_info")
errEmptyOtelTargetInfo = errors.New("empty otel_target_info")

metricsPool = sync.Pool{
New: func() interface{} {
Expand Down Expand Up @@ -176,7 +178,7 @@ func (c *collector) Collect(ch chan<- prometheus.Metric) {
if c.targetInfo == nil && !c.disableTargetInfo {
targetInfo, err := createInfoMetric(targetInfoMetricName, targetInfoDescription, metrics.Resource)
if err != nil {
// If the target info metric is invalid, disable sending it.
// If the target info metric is invalid or empty, disable sending it.
c.disableTargetInfo = true
otel.Handle(err)
return
Expand All @@ -186,7 +188,7 @@ func (c *collector) Collect(ch chan<- prometheus.Metric) {
}
}()

if !c.disableTargetInfo {
if !c.disableTargetInfo && c.targetInfo != nil {
ch <- c.targetInfo
}

Expand All @@ -203,8 +205,8 @@ func (c *collector) Collect(ch chan<- prometheus.Metric) {

if !c.disableScopeInfo {
scopeInfo, err := c.scopeInfo(scopeMetrics.Scope)
if errors.Is(err, errScopeInvalid) {
// Do not report the same error multiple times.
if errors.Is(err, errScopeInvalid) || scopeInfo == nil {
// Do not report the same error multiple times or if scopeInfo is nil.
continue
}
if err != nil {
Expand Down Expand Up @@ -435,13 +437,19 @@ func getAttrs(attrs attribute.Set) ([]string, []string) {
}

func createInfoMetric(name, description string, res *resource.Resource) (prometheus.Metric, error) {
if res.Set().Len() == 0 {
return nil, errEmptyOtelTargetInfo
}
keys, values := getAttrs(*res.Set())
desc := prometheus.NewDesc(name, description, keys, nil)
return prometheus.NewConstMetric(desc, prometheus.GaugeValue, float64(1), values...)
}

func createScopeInfoMetric(scope instrumentation.Scope) (prometheus.Metric, error) {
attrs := make([]attribute.KeyValue, 0, scope.Attributes.Len()+2) // resource attrs + scope name + scope version
if scope.Attributes.Len() == 0 {
return nil, errEmptyOtelScopeInfo
}
attrs := make([]attribute.KeyValue, 0, scope.Attributes.Len()+2)
attrs = append(attrs, scope.Attributes.ToSlice()...)
attrs = append(attrs, attribute.String(scopeNameLabel, scope.Name))
attrs = append(attrs, attribute.String(scopeVersionLabel, scope.Version))
Expand Down
4 changes: 3 additions & 1 deletion exporters/prometheus/exporter_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -305,7 +305,9 @@ func TestPrometheusExporter(t *testing.T) {
name: "empty resource",
emptyResource: true,
expectedFile: "testdata/empty_resource.txt",
recordMetrics: func(ctx context.Context, meter otelmetric.Meter) {
recordMetrics: func(ctx context.Context, _ otelmetric.Meter) {
// No instrumentation name/version/attributes
meter := metric.NewMeterProvider().Meter("")
opt := otelmetric.WithAttributes(
attribute.Key("A").String("B"),
attribute.Key("C").String("D"),
Expand Down
9 changes: 0 additions & 9 deletions exporters/prometheus/testdata/empty_resource.txt
Original file line number Diff line number Diff line change
@@ -1,9 +0,0 @@
# HELP foo_total a simple counter
# TYPE foo_total counter
foo_total{A="B",C="D",E="true",F="42",otel_scope_name="testmeter",otel_scope_version="v0.1.0"} 24.3
# HELP otel_scope_info Instrumentation Scope metadata
# TYPE otel_scope_info gauge
otel_scope_info{fizz="buzz",otel_scope_name="testmeter",otel_scope_version="v0.1.0"} 1
# HELP target_info Target metadata
# TYPE target_info gauge
target_info 1
Loading