|
17 | 17 | import java.util.Collections; |
18 | 18 | import java.util.List; |
19 | 19 | import javax.annotation.Nullable; |
| 20 | +import javax.management.InstanceNotFoundException; |
| 21 | +import javax.management.MBeanRegistrationException; |
20 | 22 | import javax.management.MBeanServer; |
21 | 23 | import javax.management.MBeanServerFactory; |
22 | 24 | import javax.management.MalformedObjectNameException; |
23 | 25 | import javax.management.ObjectName; |
| 26 | +import org.assertj.core.api.Assertions; |
24 | 27 | import org.junit.jupiter.api.AfterAll; |
25 | 28 | import org.junit.jupiter.api.AfterEach; |
26 | 29 | import org.junit.jupiter.api.BeforeAll; |
@@ -72,7 +75,19 @@ void before() { |
72 | 75 | } |
73 | 76 |
|
74 | 77 | @AfterEach |
75 | | - void after() { |
| 78 | + void after() throws Exception { |
| 79 | + ObjectName objectName = new ObjectName( |
| 80 | + "otel.jmx.test:type=" + Hello.class.getSimpleName() + ",*"); |
| 81 | + theServer.queryMBeans(objectName, null).forEach( |
| 82 | + instance -> { |
| 83 | + try { |
| 84 | + theServer.unregisterMBean(instance.getObjectName()); |
| 85 | + } catch (InstanceNotFoundException | MBeanRegistrationException e) { |
| 86 | + throw new RuntimeException(e); |
| 87 | + } |
| 88 | + } |
| 89 | + ); |
| 90 | + |
76 | 91 | if (sdk != null) { |
77 | 92 | sdk.getSdkMeterProvider().close(); |
78 | 93 | } |
@@ -102,42 +117,76 @@ void singleInstance() throws Exception { |
102 | 117 | ObjectName bean = getObjectName(null, null); |
103 | 118 | theServer.registerMBean(new Hello(42), bean); |
104 | 119 |
|
105 | | - Collection<MetricData> data = testMetric(bean.toString()); |
| 120 | + Collection<MetricData> data = testMetric(bean.toString(), Collections.emptyList()); |
106 | 121 | checkSingleValue(data, 42); |
107 | 122 | } |
108 | 123 |
|
109 | 124 | @Test |
110 | | - void multipleInstancesAggregated_twoInstances() throws Exception { |
| 125 | + void aggregateOneParam() throws Exception { |
111 | 126 | theServer.registerMBean(new Hello(42), getObjectName("value1", null)); |
112 | 127 | theServer.registerMBean(new Hello(37), getObjectName("value2", null)); |
113 | 128 |
|
114 | 129 | String bean = getObjectName("*", null).toString(); |
115 | | - Collection<MetricData> data = testMetric(bean); |
| 130 | + Collection<MetricData> data = testMetric(bean, Collections.emptyList()); |
116 | 131 | checkSingleValue(data, 79); |
117 | 132 | } |
118 | 133 |
|
119 | 134 | @Test |
120 | | - void multipleInstancesAggregated_fourInstances() throws Exception { |
121 | | - theServer.registerMBean(new Hello(1), getObjectName("1", "a")); |
122 | | - theServer.registerMBean(new Hello(2), getObjectName("2", "b")); |
123 | | - theServer.registerMBean(new Hello(3), getObjectName("3", "a")); |
124 | | - theServer.registerMBean(new Hello(5), getObjectName("4", "b")); |
| 135 | + void aggregateMultipleParams() throws Exception { |
| 136 | + theServer.registerMBean(new Hello(1), getObjectName("1", "x")); |
| 137 | + theServer.registerMBean(new Hello(2), getObjectName("2", "y")); |
| 138 | + theServer.registerMBean(new Hello(3), getObjectName("3", "x")); |
| 139 | + theServer.registerMBean(new Hello(4), getObjectName("4", "y")); |
| 140 | + |
| 141 | + String bean = getObjectName("*", "*").toString(); |
| 142 | + Collection<MetricData> data = testMetric(bean, Collections.emptyList()); |
| 143 | + checkSingleValue(data, 10); |
| 144 | + } |
| 145 | + |
| 146 | + @Test |
| 147 | + void partialAggregateMultipleParams() throws Exception { |
| 148 | + theServer.registerMBean(new Hello(1), getObjectName("1", "x")); |
| 149 | + theServer.registerMBean(new Hello(2), getObjectName("2", "y")); |
| 150 | + theServer.registerMBean(new Hello(3), getObjectName("3", "x")); |
| 151 | + theServer.registerMBean(new Hello(4), getObjectName("4", "y")); |
125 | 152 |
|
126 | 153 | String bean = getObjectName("*", "*").toString(); |
127 | | - Collection<MetricData> data = testMetric(bean); |
128 | | - checkSingleValue(data, 11); |
| 154 | + |
| 155 | + List<MetricAttribute> attributes = Collections.singletonList( |
| 156 | + new MetricAttribute("test.metric.param", |
| 157 | + MetricAttributeExtractor.fromObjectNameParameter("b"))); |
| 158 | + Collection<MetricData> data = testMetric(bean, attributes); |
| 159 | + |
| 160 | + assertThat(data) |
| 161 | + .isNotEmpty() |
| 162 | + .satisfiesExactlyInAnyOrder( |
| 163 | + metric -> { |
| 164 | + assertThat(metric.getName()).isEqualTo("test.metric"); |
| 165 | + Collection<LongPointData> points = metric.getLongSumData().getPoints(); |
| 166 | + assertThat(points).hasSize(1); |
| 167 | + LongPointData pointData = points.stream().findFirst().get(); |
| 168 | + assertThat(pointData.getAttributes()).containsEntry("b", "y"); |
| 169 | + assertThat(pointData.getValue()).isEqualTo(6); |
| 170 | + }, metric -> { |
| 171 | + Assertions.assertThat(metric.getName()).isEqualTo("test.metric"); |
| 172 | + Collection<LongPointData> points = metric.getLongSumData().getPoints(); |
| 173 | + assertThat(points).hasSize(1); |
| 174 | + LongPointData pointData = points.stream().findFirst().get(); |
| 175 | + assertThat(pointData.getAttributes()).containsEntry("b", "x"); |
| 176 | + assertThat(pointData.getValue()).isEqualTo(4); |
| 177 | + }); |
129 | 178 | } |
130 | 179 |
|
131 | | - private Collection<MetricData> testMetric(String mbean) throws MalformedObjectNameException { |
| 180 | + private Collection<MetricData> testMetric(String mbean, List<MetricAttribute> attributes) |
| 181 | + throws MalformedObjectNameException { |
132 | 182 | JmxMetricInsight metricInsight = JmxMetricInsight.createService(sdk, 0); |
133 | 183 | MetricConfiguration metricConfiguration = new MetricConfiguration(); |
134 | 184 | List<MetricExtractor> extractors = new ArrayList<>(); |
135 | 185 |
|
136 | 186 | MetricInfo metricInfo = |
137 | 187 | new MetricInfo("test.metric", "description", null, "1", MetricInfo.Type.COUNTER); |
138 | 188 | BeanAttributeExtractor beanExtractor = BeanAttributeExtractor.fromName("Value"); |
139 | | - MetricExtractor extractor = |
140 | | - new MetricExtractor(beanExtractor, metricInfo, Collections.emptyList()); |
| 189 | + MetricExtractor extractor = new MetricExtractor(beanExtractor, metricInfo, attributes); |
141 | 190 | extractors.add(extractor); |
142 | 191 | MetricDef metricDef = |
143 | 192 | new MetricDef(BeanGroup.forBeans(Collections.singletonList(mbean)), extractors); |
|
0 commit comments