Skip to content

Commit 5b58b60

Browse files
committed
Create builder for Micrometer observation collector
1 parent ec58629 commit 5b58b60

File tree

5 files changed

+100
-22
lines changed

5 files changed

+100
-22
lines changed

src/main/java/com/rabbitmq/model/observation/micrometer/DeliverContext.java

Lines changed: 7 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -29,7 +29,7 @@ public class DeliverContext extends ReceiverContext<Message> {
2929
private final String messageId;
3030
private final String correlationId;
3131

32-
public DeliverContext(String exchange, String routingKey, String queue, Message message) {
32+
DeliverContext(String exchange, String routingKey, String queue, Message message) {
3333
super(
3434
(carrier, key) -> {
3535
Object result = carrier.annotation(key);
@@ -55,27 +55,27 @@ public DeliverContext(String exchange, String routingKey, String queue, Message
5555
setCarrier(message);
5656
}
5757

58-
String exchange() {
58+
public String exchange() {
5959
return this.exchange;
6060
}
6161

62-
String routingKey() {
62+
public String routingKey() {
6363
return this.routingKey;
6464
}
6565

66-
String queue() {
66+
public String queue() {
6767
return this.queue;
6868
}
6969

70-
int payloadSizeBytes() {
70+
public int payloadSizeBytes() {
7171
return this.payloadSizeBytes;
7272
}
7373

74-
String messageId() {
74+
public String messageId() {
7575
return this.messageId;
7676
}
7777

78-
String correlationId() {
78+
public String correlationId() {
7979
return this.correlationId;
8080
}
8181
}

src/main/java/com/rabbitmq/model/observation/micrometer/MicrometerObservationCollector.java

Lines changed: 11 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -25,19 +25,24 @@
2525
import io.micrometer.observation.ObservationRegistry;
2626
import java.util.function.Function;
2727

28-
public class MicrometerObservationCollector implements ObservationCollector {
28+
class MicrometerObservationCollector implements ObservationCollector {
2929

3030
private final ObservationRegistry registry;
3131
private final PublishObservationConvention customPublishConvention, defaultPublishConvention;
3232
private final DeliverObservationConvention customProcessConvention, defaultProcessConvention;
3333

3434
@SuppressFBWarnings("EI_EXPOSE_REP2")
35-
public MicrometerObservationCollector(ObservationRegistry registry) {
36-
this.customPublishConvention = null;
37-
this.defaultPublishConvention = new DefaultPublishObservationConvention();
38-
this.customProcessConvention = null;
39-
this.defaultProcessConvention = new DefaultProcessObservationConvention();
35+
MicrometerObservationCollector(
36+
ObservationRegistry registry,
37+
PublishObservationConvention customPublishConvention,
38+
PublishObservationConvention defaultPublishConvention,
39+
DeliverObservationConvention customProcessConvention,
40+
DeliverObservationConvention defaultProcessConvention) {
4041
this.registry = registry;
42+
this.customPublishConvention = customPublishConvention;
43+
this.defaultPublishConvention = defaultPublishConvention;
44+
this.customProcessConvention = customProcessConvention;
45+
this.defaultProcessConvention = defaultProcessConvention;
4146
}
4247

4348
@Override
Lines changed: 70 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,70 @@
1+
// Copyright (c) 2024 Broadcom. All Rights Reserved.
2+
// The term "Broadcom" refers to Broadcom Inc. and/or its subsidiaries.
3+
//
4+
// Licensed under the Apache License, Version 2.0 (the "License");
5+
// you may not use this file except in compliance with the License.
6+
// You may obtain a copy of the License at
7+
//
8+
// http://www.apache.org/licenses/LICENSE-2.0
9+
//
10+
// Unless required by applicable law or agreed to in writing, software
11+
// distributed under the License is distributed on an "AS IS" BASIS,
12+
// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13+
// See the License for the specific language governing permissions and
14+
// limitations under the License.
15+
//
16+
// If you have any questions regarding licensing, please contact us at
17+
18+
package com.rabbitmq.model.observation.micrometer;
19+
20+
import com.rabbitmq.model.ObservationCollector;
21+
import io.micrometer.observation.ObservationRegistry;
22+
23+
public class MicrometerObservationCollectorBuilder {
24+
25+
private ObservationRegistry registry = ObservationRegistry.NOOP;
26+
private PublishObservationConvention customPublishConvention = null;
27+
private PublishObservationConvention defaultPublishConvention =
28+
new DefaultPublishObservationConvention();
29+
private DeliverObservationConvention customProcessConvention = null;
30+
private DeliverObservationConvention defaultProcessConvention =
31+
new DefaultProcessObservationConvention();
32+
33+
public MicrometerObservationCollectorBuilder registry(ObservationRegistry registry) {
34+
this.registry = registry;
35+
return this;
36+
}
37+
38+
public MicrometerObservationCollectorBuilder customPublishConvention(
39+
PublishObservationConvention customPublishConvention) {
40+
this.customPublishConvention = customPublishConvention;
41+
return this;
42+
}
43+
44+
public MicrometerObservationCollectorBuilder defaultPublishConvention(
45+
PublishObservationConvention defaultPublishConvention) {
46+
this.defaultPublishConvention = defaultPublishConvention;
47+
return this;
48+
}
49+
50+
public MicrometerObservationCollectorBuilder customProcessConvention(
51+
DeliverObservationConvention customProcessConvention) {
52+
this.customProcessConvention = customProcessConvention;
53+
return this;
54+
}
55+
56+
public MicrometerObservationCollectorBuilder defaultProcessConvention(
57+
DeliverObservationConvention defaultProcessConvention) {
58+
this.defaultProcessConvention = defaultProcessConvention;
59+
return this;
60+
}
61+
62+
public ObservationCollector build() {
63+
return new MicrometerObservationCollector(
64+
this.registry,
65+
this.customPublishConvention,
66+
this.defaultPublishConvention,
67+
this.customProcessConvention,
68+
this.defaultProcessConvention);
69+
}
70+
}

src/main/java/com/rabbitmq/model/observation/micrometer/PublishContext.java

Lines changed: 7 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -55,31 +55,31 @@ public class PublishContext extends SenderContext<Message> {
5555
setCarrier(message);
5656
}
5757

58-
String exchange() {
58+
public String exchange() {
5959
return this.exchange;
6060
}
6161

62-
String routingKey() {
62+
public String routingKey() {
6363
return this.routingKey;
6464
}
6565

66-
int payloadSizeBytes() {
66+
public int payloadSizeBytes() {
6767
return this.payloadSizeBytes;
6868
}
6969

70-
String messageId() {
70+
public String messageId() {
7171
return this.messageId;
7272
}
7373

74-
String correlationId() {
74+
public String correlationId() {
7575
return this.correlationId;
7676
}
7777

78-
String peerAddress() {
78+
public String peerAddress() {
7979
return this.peerAddress;
8080
}
8181

82-
int peerPort() {
82+
public int peerPort() {
8383
return this.peerPort;
8484
}
8585
}

src/test/java/com/rabbitmq/model/amqp/MicrometerObservationCollectorTest.java

Lines changed: 5 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -24,7 +24,7 @@
2424
import com.rabbitmq.model.Environment;
2525
import com.rabbitmq.model.Management;
2626
import com.rabbitmq.model.Publisher;
27-
import com.rabbitmq.model.observation.micrometer.MicrometerObservationCollector;
27+
import com.rabbitmq.model.observation.micrometer.MicrometerObservationCollectorBuilder;
2828
import io.micrometer.tracing.exporter.FinishedSpan;
2929
import io.micrometer.tracing.test.SampleTestRunner;
3030
import io.micrometer.tracing.test.reporter.BuildingBlocks;
@@ -57,7 +57,10 @@ public SampleTestRunner.SampleTestRunnerConsumer yourCode() {
5757
return (buildingBlocks, meterRegistry) -> {
5858
try (Environment env =
5959
new AmqpEnvironmentBuilder()
60-
.observationCollector(new MicrometerObservationCollector(getObservationRegistry()))
60+
.observationCollector(
61+
new MicrometerObservationCollectorBuilder()
62+
.registry(getObservationRegistry())
63+
.build())
6164
.build()) {
6265
Connection publisherConnection = env.connectionBuilder().build();
6366
Connection consumerConnection = env.connectionBuilder().build();

0 commit comments

Comments
 (0)