opentelemetry-java
opentelemetry-java copied to clipboard
How to add multiple attributes to a metrics instrument?
What are you trying to achieve? (Java) I want to measure the results of an sql query every X minutes, to export as a metric. I have determined that one way to do so is to use a an async gauge. However, the value I want to measure (one of the query result cells) is associated with other attributes (cells in same row).
How do I make a group of attributes to pass as an argument here?
Using the "getting started" example:
meter .gaugeBuilder("cpu_usage") .setDescription("CPU Usage") .setUnit("ms") .buildWithCallback(measurement -> { measurement.record(getCpuUsage(), attributes); });
Transferring to opentelemetry-java.
You can create Attributes as follows:
Attributes.builder().put("key1", "value1").put("key2", "value2").build()Attributes.of(AttributeKey.stringKey("key1"), "value1", AttributeKey.stringKey("key2"), "value2")
Thank you! Are those two lines both necessary, or are they alternatives to each other?
In regards to the second line you sent: how would you create these dynamically, when you don't know how many attributes you're getting from the start?
(Edit: sorry for closing the issue, it was not my intention)
They are alternatives to each other. The second option is best for when you know all the attribute keys ahead of time.
This issue was marked stale due to lack of author feedback. It will be closed in 7 days.
Thank you for your help!