opentelemetry-java icon indicating copy to clipboard operation
opentelemetry-java copied to clipboard

Prometheus Exporters do not interact with Resource attributes

Open yangtaoran opened this issue 3 years ago • 5 comments

https://github.com/open-telemetry/opentelemetry-java/blob/main/sdk/metrics/src/main/java/io/opentelemetry/sdk/metrics/SdkMeterProviderBuilder.java#L56 Here,I set setResource to all meter.I think all metrics will add attributes from the resource,but it not. I init SdkMeterProvider by follow:

SdkMeterProvider.builder().setResource(Resource.create(Attributes.of(
                AttributeKey.stringKey("instance"), "xxxx")))
                .registerMetricReader(
                        PrometheusHttpServer.builder().setHost("127.0.0.1").setPort(9092).newMetricReaderFactory())
                .buildAndRegisterGlobal();
Meter meter = GlobalMeterProvider.get().get("test");
LongCounter testCounter = meter.counterBuilder("meter_test").setUnit("1").setDescription("test").build();
testCounter.add(1, Attributes.of(AttributeKey.stringKey("hello"), "world"));

I scrap the metrics like this:

meter_test_total{hello="world",} 1.0 1641357854770

I think it will be ”meter_test_total{hello="world",instance="xxxx"} 1.0 1641357854770”

yangtaoran avatar Jan 05 '22 04:01 yangtaoran

Hi @yangtaoran - there is currently not any support for mapping resource properties into metric attributes for Prometheus exporter. Are you using the prometheus exporter in this repository or are you using the OpenTelemetry collector? Currently I believe the way to achieve what you want is to use resource_to_telemetry_conversion in the prometheus exporter in the collector

https://github.com/open-telemetry/opentelemetry-collector-contrib/tree/main/exporter/prometheusexporter#getting-started

anuraaga avatar Jan 05 '22 05:01 anuraaga

Hi @yangtaoran - there is currently not any support for mapping resource properties into metric attributes for Prometheus exporter. Are you using the prometheus exporter in this repository or are you using the OpenTelemetry collector? Currently I believe the way to achieve what you want is to use resource_to_telemetry_conversion in the prometheus exporter in the collector

https://github.com/open-telemetry/opentelemetry-collector-contrib/tree/main/exporter/prometheusexporter#getting-started

I am using the prometheus exporter in this repository. So what can do for the SdkMeterProviderBuilder setResource method?According to you, I can use OTLP to export metrics data to the collector, and the collector configure prometheus exporter with resource_to_telemetry_conversion. So the prometheus exporter will export the metrics data with the resource.Is my understanding right?

yangtaoran avatar Jan 06 '22 11:01 yangtaoran

Yup I think using OTLP exporter from SDK to collector and using that setting is probably the best bet for now. We need to think of how to better support this in the SDK natively.

anuraaga avatar Jan 06 '22 11:01 anuraaga

I think It's necessary that the Prometheus exporter from SDK should export metrics data with resource.Although I can do like above,it's complicated.If it is possible,I can push a PR.

yangtaoran avatar Jan 06 '22 12:01 yangtaoran

@yangtaoran The prometheus exporter is not fully specified in OTEL yet. I fully expect resource -> prometheus labels to part of that final specification, but we're still discussing the details of what would be done in SDKs.

You can see:

  • https://github.com/open-telemetry/opentelemetry-specification/issues/1782
  • https://github.com/open-telemetry/opentelemetry-specification/issues/1906
  • https://github.com/open-telemetry/opentelemetry-specification/issues/2251

@anuraaga's workaround should be good for now, but expect improvements in the SDK-exporter going forward.

jsuereth avatar Jan 08 '22 18:01 jsuereth