-
Notifications
You must be signed in to change notification settings - Fork 353
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
adds support for Micrometer Observations (#1075)
- Loading branch information
1 parent
000f6da
commit 32da131
Showing
20 changed files
with
1,664 additions
and
4 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
246 changes: 246 additions & 0 deletions
246
...examples/src/test/java/io/rsocket/integration/observation/ObservationIntegrationTest.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,246 @@ | ||
/* | ||
* Copyright 2015-2020 the original author or authors. | ||
* | ||
* Licensed under the Apache License, Version 2.0 (the "License"); | ||
* you may not use this file except in compliance with the License. | ||
* You may obtain a copy of the License at | ||
* | ||
* http://www.apache.org/licenses/LICENSE-2.0 | ||
* | ||
* Unless required by applicable law or agreed to in writing, software | ||
* distributed under the License is distributed on an "AS IS" BASIS, | ||
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. | ||
* See the License for the specific language governing permissions and | ||
* limitations under the License. | ||
*/ | ||
|
||
package io.rsocket.integration.observation; | ||
|
||
import static org.assertj.core.api.Assertions.assertThat; | ||
|
||
import io.micrometer.core.instrument.MeterRegistry; | ||
import io.micrometer.core.instrument.Tag; | ||
import io.micrometer.core.instrument.Tags; | ||
import io.micrometer.core.instrument.observation.DefaultMeterObservationHandler; | ||
import io.micrometer.core.instrument.simple.SimpleMeterRegistry; | ||
import io.micrometer.core.tck.MeterRegistryAssert; | ||
import io.micrometer.observation.Observation; | ||
import io.micrometer.observation.ObservationHandler; | ||
import io.micrometer.observation.ObservationRegistry; | ||
import io.micrometer.tracing.test.SampleTestRunner; | ||
import io.micrometer.tracing.test.reporter.BuildingBlocks; | ||
import io.micrometer.tracing.test.simple.SpansAssert; | ||
import io.rsocket.Payload; | ||
import io.rsocket.RSocket; | ||
import io.rsocket.core.RSocketConnector; | ||
import io.rsocket.core.RSocketServer; | ||
import io.rsocket.micrometer.observation.ByteBufGetter; | ||
import io.rsocket.micrometer.observation.ByteBufSetter; | ||
import io.rsocket.micrometer.observation.ObservationRequesterRSocketProxy; | ||
import io.rsocket.micrometer.observation.ObservationResponderRSocketProxy; | ||
import io.rsocket.micrometer.observation.RSocketRequesterTracingObservationHandler; | ||
import io.rsocket.micrometer.observation.RSocketResponderTracingObservationHandler; | ||
import io.rsocket.plugins.RSocketInterceptor; | ||
import io.rsocket.transport.netty.client.TcpClientTransport; | ||
import io.rsocket.transport.netty.server.CloseableChannel; | ||
import io.rsocket.transport.netty.server.TcpServerTransport; | ||
import io.rsocket.util.DefaultPayload; | ||
import java.time.Duration; | ||
import java.util.Deque; | ||
import java.util.concurrent.atomic.AtomicInteger; | ||
import java.util.function.BiConsumer; | ||
import org.awaitility.Awaitility; | ||
import org.junit.jupiter.api.AfterEach; | ||
import org.reactivestreams.Publisher; | ||
import reactor.core.publisher.Flux; | ||
import reactor.core.publisher.Mono; | ||
|
||
public class ObservationIntegrationTest extends SampleTestRunner { | ||
private static final MeterRegistry registry = new SimpleMeterRegistry(); | ||
private static final ObservationRegistry observationRegistry = ObservationRegistry.create(); | ||
|
||
static { | ||
observationRegistry | ||
.observationConfig() | ||
.observationHandler(new DefaultMeterObservationHandler(registry)); | ||
} | ||
|
||
private final RSocketInterceptor requesterInterceptor; | ||
private final RSocketInterceptor responderInterceptor; | ||
|
||
ObservationIntegrationTest() { | ||
super(SampleRunnerConfig.builder().build()); | ||
requesterInterceptor = | ||
reactiveSocket -> new ObservationRequesterRSocketProxy(reactiveSocket, observationRegistry); | ||
|
||
responderInterceptor = | ||
reactiveSocket -> new ObservationResponderRSocketProxy(reactiveSocket, observationRegistry); | ||
} | ||
|
||
private CloseableChannel server; | ||
private RSocket client; | ||
private AtomicInteger counter; | ||
|
||
@Override | ||
public BiConsumer<BuildingBlocks, Deque<ObservationHandler<? extends Observation.Context>>> | ||
customizeObservationHandlers() { | ||
return (buildingBlocks, observationHandlers) -> { | ||
observationHandlers.addFirst( | ||
new RSocketRequesterTracingObservationHandler( | ||
buildingBlocks.getTracer(), | ||
buildingBlocks.getPropagator(), | ||
new ByteBufSetter(), | ||
false)); | ||
observationHandlers.addFirst( | ||
new RSocketResponderTracingObservationHandler( | ||
buildingBlocks.getTracer(), | ||
buildingBlocks.getPropagator(), | ||
new ByteBufGetter(), | ||
false)); | ||
}; | ||
} | ||
|
||
@AfterEach | ||
public void teardown() { | ||
if (server != null) { | ||
server.dispose(); | ||
} | ||
} | ||
|
||
private void testRequest() { | ||
counter.set(0); | ||
client.requestResponse(DefaultPayload.create("REQUEST", "META")).block(); | ||
assertThat(counter).as("Server did not see the request.").hasValue(1); | ||
} | ||
|
||
private void testStream() { | ||
counter.set(0); | ||
client.requestStream(DefaultPayload.create("start")).blockLast(); | ||
|
||
assertThat(counter).as("Server did not see the request.").hasValue(1); | ||
} | ||
|
||
private void testRequestChannel() { | ||
counter.set(0); | ||
client.requestChannel(Mono.just(DefaultPayload.create("start"))).blockFirst(); | ||
assertThat(counter).as("Server did not see the request.").hasValue(1); | ||
} | ||
|
||
private void testFireAndForget() { | ||
counter.set(0); | ||
client.fireAndForget(DefaultPayload.create("start")).subscribe(); | ||
Awaitility.await().atMost(Duration.ofSeconds(50)).until(() -> counter.get() == 1); | ||
assertThat(counter).as("Server did not see the request.").hasValue(1); | ||
} | ||
|
||
@Override | ||
public SampleTestRunnerConsumer yourCode() { | ||
return (bb, meterRegistry) -> { | ||
counter = new AtomicInteger(); | ||
server = | ||
RSocketServer.create( | ||
(setup, sendingSocket) -> { | ||
sendingSocket.onClose().subscribe(); | ||
|
||
return Mono.just( | ||
new RSocket() { | ||
@Override | ||
public Mono<Payload> requestResponse(Payload payload) { | ||
payload.release(); | ||
counter.incrementAndGet(); | ||
return Mono.just(DefaultPayload.create("RESPONSE", "METADATA")); | ||
} | ||
|
||
@Override | ||
public Flux<Payload> requestStream(Payload payload) { | ||
payload.release(); | ||
counter.incrementAndGet(); | ||
return Flux.range(1, 10_000) | ||
.map(i -> DefaultPayload.create("data -> " + i)); | ||
} | ||
|
||
@Override | ||
public Flux<Payload> requestChannel(Publisher<Payload> payloads) { | ||
counter.incrementAndGet(); | ||
return Flux.from(payloads); | ||
} | ||
|
||
@Override | ||
public Mono<Void> fireAndForget(Payload payload) { | ||
payload.release(); | ||
counter.incrementAndGet(); | ||
return Mono.empty(); | ||
} | ||
}); | ||
}) | ||
.interceptors(registry -> registry.forResponder(responderInterceptor)) | ||
.bind(TcpServerTransport.create("localhost", 0)) | ||
.block(); | ||
|
||
client = | ||
RSocketConnector.create() | ||
.interceptors(registry -> registry.forRequester(requesterInterceptor)) | ||
.connect(TcpClientTransport.create(server.address())) | ||
.block(); | ||
|
||
testRequest(); | ||
|
||
testStream(); | ||
|
||
testRequestChannel(); | ||
|
||
testFireAndForget(); | ||
|
||
// @formatter:off | ||
SpansAssert.assertThat(bb.getFinishedSpans()) | ||
.haveSameTraceId() | ||
// "request_*" + "handle" x 4 | ||
.hasNumberOfSpansEqualTo(8) | ||
.hasNumberOfSpansWithNameEqualTo("handle", 4) | ||
.forAllSpansWithNameEqualTo("handle", span -> span.hasTagWithKey("rsocket.request-type")) | ||
.hasASpanWithNameIgnoreCase("request_stream") | ||
.thenASpanWithNameEqualToIgnoreCase("request_stream") | ||
.hasTag("rsocket.request-type", "REQUEST_STREAM") | ||
.backToSpans() | ||
.hasASpanWithNameIgnoreCase("request_channel") | ||
.thenASpanWithNameEqualToIgnoreCase("request_channel") | ||
.hasTag("rsocket.request-type", "REQUEST_CHANNEL") | ||
.backToSpans() | ||
.hasASpanWithNameIgnoreCase("request_fnf") | ||
.thenASpanWithNameEqualToIgnoreCase("request_fnf") | ||
.hasTag("rsocket.request-type", "REQUEST_FNF") | ||
.backToSpans() | ||
.hasASpanWithNameIgnoreCase("request_response") | ||
.thenASpanWithNameEqualToIgnoreCase("request_response") | ||
.hasTag("rsocket.request-type", "REQUEST_RESPONSE"); | ||
|
||
MeterRegistryAssert.assertThat(registry) | ||
.hasTimerWithNameAndTags( | ||
"rsocket.response", | ||
Tags.of(Tag.of("error", "none"), Tag.of("rsocket.request-type", "REQUEST_RESPONSE"))) | ||
.hasTimerWithNameAndTags( | ||
"rsocket.fnf", | ||
Tags.of(Tag.of("error", "none"), Tag.of("rsocket.request-type", "REQUEST_FNF"))) | ||
.hasTimerWithNameAndTags( | ||
"rsocket.request", | ||
Tags.of(Tag.of("error", "none"), Tag.of("rsocket.request-type", "REQUEST_RESPONSE"))) | ||
.hasTimerWithNameAndTags( | ||
"rsocket.channel", | ||
Tags.of(Tag.of("error", "none"), Tag.of("rsocket.request-type", "REQUEST_CHANNEL"))) | ||
.hasTimerWithNameAndTags( | ||
"rsocket.stream", | ||
Tags.of(Tag.of("error", "none"), Tag.of("rsocket.request-type", "REQUEST_STREAM"))); | ||
// @formatter:on | ||
}; | ||
} | ||
|
||
@Override | ||
protected MeterRegistry getMeterRegistry() { | ||
return registry; | ||
} | ||
|
||
@Override | ||
protected ObservationRegistry getObservationRegistry() { | ||
return observationRegistry; | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
36 changes: 36 additions & 0 deletions
36
rsocket-micrometer/src/main/java/io/rsocket/micrometer/observation/ByteBufGetter.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,36 @@ | ||
/* | ||
* Copyright 2013-2021 the original author or authors. | ||
* | ||
* Licensed under the Apache License, Version 2.0 (the "License"); | ||
* you may not use this file except in compliance with the License. | ||
* You may obtain a copy of the License at | ||
* | ||
* https://www.apache.org/licenses/LICENSE-2.0 | ||
* | ||
* Unless required by applicable law or agreed to in writing, software | ||
* distributed under the License is distributed on an "AS IS" BASIS, | ||
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. | ||
* See the License for the specific language governing permissions and | ||
* limitations under the License. | ||
*/ | ||
|
||
package io.rsocket.micrometer.observation; | ||
|
||
import io.micrometer.tracing.propagation.Propagator; | ||
import io.netty.buffer.ByteBuf; | ||
import io.netty.util.CharsetUtil; | ||
import io.rsocket.metadata.CompositeMetadata; | ||
|
||
public class ByteBufGetter implements Propagator.Getter<ByteBuf> { | ||
|
||
@Override | ||
public String get(ByteBuf carrier, String key) { | ||
final CompositeMetadata compositeMetadata = new CompositeMetadata(carrier, false); | ||
for (CompositeMetadata.Entry entry : compositeMetadata) { | ||
if (key.equals(entry.getMimeType())) { | ||
return entry.getContent().toString(CharsetUtil.UTF_8); | ||
} | ||
} | ||
return null; | ||
} | ||
} |
33 changes: 33 additions & 0 deletions
33
rsocket-micrometer/src/main/java/io/rsocket/micrometer/observation/ByteBufSetter.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,33 @@ | ||
/* | ||
* Copyright 2013-2021 the original author or authors. | ||
* | ||
* Licensed under the Apache License, Version 2.0 (the "License"); | ||
* you may not use this file except in compliance with the License. | ||
* You may obtain a copy of the License at | ||
* | ||
* https://www.apache.org/licenses/LICENSE-2.0 | ||
* | ||
* Unless required by applicable law or agreed to in writing, software | ||
* distributed under the License is distributed on an "AS IS" BASIS, | ||
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. | ||
* See the License for the specific language governing permissions and | ||
* limitations under the License. | ||
*/ | ||
|
||
package io.rsocket.micrometer.observation; | ||
|
||
import io.micrometer.tracing.propagation.Propagator; | ||
import io.netty.buffer.ByteBufAllocator; | ||
import io.netty.buffer.ByteBufUtil; | ||
import io.netty.buffer.CompositeByteBuf; | ||
import io.rsocket.metadata.CompositeMetadataCodec; | ||
|
||
public class ByteBufSetter implements Propagator.Setter<CompositeByteBuf> { | ||
|
||
@Override | ||
public void set(CompositeByteBuf carrier, String key, String value) { | ||
final ByteBufAllocator alloc = carrier.alloc(); | ||
CompositeMetadataCodec.encodeAndAddMetadataWithCompression( | ||
carrier, alloc, key, ByteBufUtil.writeUtf8(alloc, value)); | ||
} | ||
} |
Oops, something went wrong.