Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

OTLP metric exporter - resource attributes not exported #3208

Closed
alanwest opened this issue Sep 20, 2022 · 9 comments · Fixed by #3218
Closed

OTLP metric exporter - resource attributes not exported #3208

alanwest opened this issue Sep 20, 2022 · 9 comments · Fixed by #3218
Assignees
Labels
area:metrics Part of OpenTelemetry Metrics bug Something isn't working pkg:SDK Related to an SDK package

Comments

@alanwest
Copy link
Member

Description

Resource attributes are not exported using the OTLP metric exporter.

Environment

  • opentelemetry-go version: v0.32.0

Steps To Reproduce

  1. Configure OTLP endpoint
  2. Run the collector locally with the following config
receivers:
  otlp:
    protocols:
      grpc:

exporters:
  logging:
    loglevel: debug

service:
  pipelines:
    metrics:
      receivers: [otlp]
      exporters: [logging]
  1. Run the following code
package main

import (
	"context"
	"log"
	"time"

	"go.opentelemetry.io/otel/exporters/otlp/otlpmetric/otlpmetricgrpc"
	"go.opentelemetry.io/otel/metric/global"
	"go.opentelemetry.io/otel/sdk/metric"
	"go.opentelemetry.io/otel/sdk/resource"

	semconv "go.opentelemetry.io/otel/semconv/v1.12.0"
)

func main() {
	ctx := context.Background()

	exporter, err := otlpmetricgrpc.New(ctx)
	if err != nil {
		log.Fatalf("%s: %v", "failed to create metric exporter", err)
	}

	reader := metric.NewPeriodicReader(
		exporter,
		metric.WithInterval(2*time.Second),
	)

	res := resource.NewWithAttributes(
		semconv.SchemaURL,

                 // This attribute is not exported
		semconv.ServiceNameKey.String("my-service"),
	)

	meterProvider := metric.NewMeterProvider(
		metric.WithResource(res),
		metric.WithReader(reader),
	)
	global.SetMeterProvider(meterProvider)

	meter := global.Meter("my-meter")

	counter, err := meter.SyncFloat64().Counter("my-counter")
	if err != nil {
		log.Panicf("failed to initialize instrument: %v", err)
	}

	counter.Add(ctx, 12.0)
	time.Sleep(5 * time.Second)
	counter.Add(ctx, 22.0)
	time.Sleep(5 * time.Second)
	counter.Add(ctx, 13.0)

	defer func() {
		err := meterProvider.Shutdown(context.Background())
		if err != nil {
			log.Fatalln(err)
		}
	}()
}
  1. Output from collector's logging exporter
2022-09-20T14:31:28.721-0700	info	MetricsExporter	{"kind": "exporter", "data_type": "metrics", "name": "logging", "#metrics": 1}
2022-09-20T14:31:28.722-0700	info	ResourceMetrics #0
Resource SchemaURL: 
ScopeMetrics #0
ScopeMetrics SchemaURL: 
InstrumentationScope my-meter 
Metric #0
Descriptor:
     -> Name: my-counter
     -> Description: 
     -> Unit: 
     -> DataType: Sum
     -> IsMonotonic: true
     -> AggregationTemporality: AGGREGATION_TEMPORALITY_CUMULATIVE
NumberDataPoints #0
StartTimestamp: 2022-09-20 21:31:26.683813 +0000 UTC
Timestamp: 2022-09-20 21:31:28.685045 +0000 UTC
Value: 12.000000
	{"kind": "exporter", "data_type": "metrics", "name": "logging"}

Expected behavior

Resource should include the attribute service.name=my-service

@alanwest alanwest added the bug Something isn't working label Sep 20, 2022
@MrAlias MrAlias added pkg:SDK Related to an SDK package area:metrics Part of OpenTelemetry Metrics labels Sep 20, 2022
@MrAlias MrAlias added this to the Metric SDK: Beta milestone Sep 20, 2022
@MrAlias
Copy link
Contributor

MrAlias commented Sep 20, 2022

@alanwest what version of the collector was used in the example?

@alanwest
Copy link
Member Author

I built and ran the collector locally from main.

@MrAlias
Copy link
Contributor

MrAlias commented Sep 20, 2022

It looks like newPipelineRegistries initializes pipelines without a resource:

pipe := &pipeline{}

cc @MadVikingGod

@MrAlias
Copy link
Contributor

MrAlias commented Sep 20, 2022

This exists for the stdoutmetric exporter as well:

package main

import (
	"context"
	"log"
	"time"

	"go.opentelemetry.io/otel/exporters/stdout/stdoutmetric"
	"go.opentelemetry.io/otel/sdk/metric"
	"go.opentelemetry.io/otel/sdk/resource"

	semconv "go.opentelemetry.io/otel/semconv/v1.12.0"
)

func main() {
	ctx := context.Background()

	exporter, err := stdoutmetric.New()
	if err != nil {
		log.Fatalf("failed to create metric exporter: %v", err)
	}

	reader := metric.NewPeriodicReader(
		exporter,
		metric.WithInterval(time.Second),
	)

	res := resource.NewWithAttributes(
		semconv.SchemaURL,
		// This attribute is not exported
		semconv.ServiceNameKey.String("my-service"),
	)
	log.Println("resource:", res)

	meterProvider := metric.NewMeterProvider(
		metric.WithResource(res),
		metric.WithReader(reader),
	)
	meter := meterProvider.Meter("my-meter")

	counter, err := meter.SyncFloat64().Counter("my-counter")
	if err != nil {
		log.Fatalf("failed to initialize instrument: %v", err)
	}

	counter.Add(ctx, 12.0)
	time.Sleep(2 * time.Second)

	err = meterProvider.Shutdown(ctx)
	if err != nil {
		log.Fatalln("failed to shutdown", err)
	}
}
go 1.19

require (
	go.opentelemetry.io/otel v1.10.0
	go.opentelemetry.io/otel/exporters/otlp/otlpmetric/otlpmetricgrpc v0.32.0
	go.opentelemetry.io/otel/exporters/stdout/stdoutmetric v0.32.0
	go.opentelemetry.io/otel/sdk v1.10.0
	go.opentelemetry.io/otel/sdk/metric v0.32.0
)

require (
	github.com/cenkalti/backoff/v4 v4.1.3 // indirect
	github.com/go-logr/logr v1.2.3 // indirect
	github.com/go-logr/stdr v1.2.2 // indirect
	github.com/golang/protobuf v1.5.2 // indirect
	github.com/grpc-ecosystem/grpc-gateway/v2 v2.7.0 // indirect
	go.opentelemetry.io/otel/exporters/otlp/internal/retry v1.10.0 // indirect
	go.opentelemetry.io/otel/exporters/otlp/otlpmetric v0.32.0 // indirect
	go.opentelemetry.io/otel/metric v0.32.0 // indirect
	go.opentelemetry.io/otel/trace v1.10.0 // indirect
	go.opentelemetry.io/proto/otlp v0.19.0 // indirect
	golang.org/x/net v0.0.0-20210405180319-a5a99cb37ef4 // indirect
	golang.org/x/sys v0.0.0-20210510120138-977fb7262007 // indirect
	golang.org/x/text v0.3.5 // indirect
	google.golang.org/genproto v0.0.0-20211118181313-81c1377c94b1 // indirect
	google.golang.org/grpc v1.46.2 // indirect
	google.golang.org/protobuf v1.28.0 // indirect
)

@MrAlias MrAlias self-assigned this Sep 21, 2022
MrAlias added a commit to MrAlias/opentelemetry-go that referenced this issue Sep 21, 2022
@MrAlias
Copy link
Contributor

MrAlias commented Sep 21, 2022

Validated with the above examples #3218 should resolve this.

MrAlias added a commit that referenced this issue Sep 22, 2022
* Set MeterProvider resource for all pipelines

Resolves #3208

* Add change to changelog
@fracasula
Copy link

I'm still experiencing this on the latest (main branch) when I set custom attributes like:

otelRes := resource.NewWithAttributes(
	semconv.SchemaURL,
	semconv.ServiceName("test server"),
	semconv.ServiceVersion("v0.1.0"),
	attribute.String("foo", "bar"), // we expect this to be propagated
)

I'm not seeing the "foo=bar" attribute propagated in the metric. I hacked my way through this to solve it but it doesn't feel right, please have a look at this test where I'm replicating the issue.

@fracasula
Copy link

The resource seems to be propagated as is, I checked the gRPC client as well so the fact that I don't see foo=bar on the other side must depend on the otel collector not processing extra attributes that are attached to the resource. Maybe it's processing the semconv ones only? My hack works around the problem since I'm adding those attributes manually into the data points. Can I get your 2 cents on this @MrAlias ?

@MrAlias
Copy link
Contributor

MrAlias commented May 18, 2023

@fracasula please open a new bug. The bug described by this issue is still fixed according to the validation it contains.

@fracasula
Copy link

@fracasula please open a new bug. The bug described by this issue is still fixed according to the validation it contains.

Done here @MrAlias

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
area:metrics Part of OpenTelemetry Metrics bug Something isn't working pkg:SDK Related to an SDK package
Projects
None yet
Development

Successfully merging a pull request may close this issue.

3 participants