-
Notifications
You must be signed in to change notification settings - Fork 1.2k
Add observability adapter for Lettuce #2439
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Closed
Closed
Changes from all commits
Commits
Show all changes
2 commits
Select commit
Hold shift + click to select a range
File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or 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 hidden or 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 hidden or 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,38 @@ | ||
| [[redis.observability]] | ||
| == Observability | ||
|
|
||
| Getting insights from an application component about its operations, timing and relation to application code is crucial to understand latency. | ||
| Spring Data Redis ships with a Micrometer integration through the Lettuce driver to collect observations during Redis interaction. | ||
| Once the integration is set up, Micrometer will create meters and spans (for distributed tracing) for each Redis command. | ||
|
|
||
| To enable the integration, apply the following configuration to `LettuceClientConfiguration`: | ||
|
|
||
| [source,java] | ||
| ---- | ||
| @Configuration | ||
| class TracingConfig { | ||
|
|
||
| @Bean | ||
| public ClientResources clientResources(ObservationRegistry observationRegistry) { | ||
|
|
||
| return ClientResources.builder() | ||
| .tracing(new MicrometerTracingAdapter(observationRegistry, "my-redis-cache")) | ||
| .build(); | ||
| } | ||
|
|
||
| @Bean | ||
| public LettuceConnectionFactory lettuceConnectionFactory(ClientResources clientResources) { | ||
|
|
||
| LettuceClientConfiguration clientConfig = LettuceClientConfiguration.builder() | ||
| .clientResources(clientResources).build(); | ||
| RedisConfiguration redisConfiguration = …; | ||
| return new LettuceConnectionFactory(redisConfiguration, clientConfig); | ||
| } | ||
| } | ||
| ---- | ||
|
|
||
| include::../../../../target/_conventions.adoc[] | ||
|
|
||
| include::../../../../target/_metrics.adoc[] | ||
|
|
||
| include::../../../../target/_spans.adoc[] |
82 changes: 82 additions & 0 deletions
82
...work/data/redis/connection/lettuce/observability/DefaultLettuceObservationConvention.java
This file contains hidden or 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,82 @@ | ||
| /* | ||
| * Copyright 2022 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 org.springframework.data.redis.connection.lettuce.observability; | ||
|
|
||
| import java.net.InetSocketAddress; | ||
| import java.util.Locale; | ||
|
|
||
| import org.springframework.data.redis.connection.lettuce.observability.RedisObservation.HighCardinalityCommandKeyNames; | ||
| import org.springframework.data.redis.connection.lettuce.observability.RedisObservation.LowCardinalityCommandKeyNames; | ||
|
|
||
| import io.lettuce.core.protocol.RedisCommand; | ||
| import io.lettuce.core.tracing.Tracing.Endpoint; | ||
| import io.micrometer.common.KeyValues; | ||
|
|
||
| /** | ||
| * Default {@link LettuceObservationConvention} implementation. | ||
| * | ||
| * @author Mark Paluch | ||
| * @since 3.0 | ||
| */ | ||
| record DefaultLettuceObservationConvention( | ||
| boolean includeCommandArgsInSpanTags) implements LettuceObservationConvention { | ||
|
|
||
| @Override | ||
| public KeyValues getLowCardinalityKeyValues(LettuceObservationContext context) { | ||
|
|
||
| Endpoint ep = context.getRequiredEndpoint(); | ||
| KeyValues keyValues = KeyValues.of(LowCardinalityCommandKeyNames.DATABASE_SYSTEM.withValue("redis"), // | ||
| LowCardinalityCommandKeyNames.REDIS_COMMAND.withValue(context.getRequiredCommand().getType().name()), // | ||
| LowCardinalityCommandKeyNames.REDIS_SERVER.withValue(ep.toString())); | ||
|
|
||
| if (ep instanceof SocketAddressEndpoint endpoint) { | ||
|
|
||
| if (endpoint.socketAddress()instanceof InetSocketAddress inet) { | ||
| keyValues = keyValues | ||
| .and(KeyValues.of(LowCardinalityCommandKeyNames.NET_PEER_NAME.withValue(inet.getHostString()), | ||
| LowCardinalityCommandKeyNames.NET_PEER_PORT.withValue("" + inet.getPort()))); | ||
| } else { | ||
| keyValues = keyValues | ||
| .and(KeyValues.of(LowCardinalityCommandKeyNames.NET_PEER_ADDR.withValue(endpoint.toString()))); | ||
| } | ||
| } | ||
|
|
||
| return keyValues; | ||
| } | ||
|
|
||
| @Override | ||
| public KeyValues getHighCardinalityKeyValues(LettuceObservationContext context) { | ||
|
|
||
| RedisCommand<?, ?, ?> command = context.getRequiredCommand(); | ||
|
|
||
| if (includeCommandArgsInSpanTags) { | ||
|
|
||
| if (command.getArgs() != null) { | ||
| return KeyValues.of(HighCardinalityCommandKeyNames.STATEMENT | ||
| .withValue(command.getType().name() + " " + command.getArgs().toCommandString())); | ||
| } | ||
|
|
||
| return KeyValues.of(HighCardinalityCommandKeyNames.STATEMENT.withValue(command.getType().name())); | ||
| } | ||
|
|
||
| return KeyValues.empty(); | ||
| } | ||
|
|
||
| @Override | ||
| public String getContextualName(LettuceObservationContext context) { | ||
| return context.getRequiredCommand().getType().name().toLowerCase(Locale.ROOT); | ||
| } | ||
| } |
72 changes: 72 additions & 0 deletions
72
...pringframework/data/redis/connection/lettuce/observability/LettuceObservationContext.java
This file contains hidden or 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,72 @@ | ||
| /* | ||
| * Copyright 2022 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 org.springframework.data.redis.connection.lettuce.observability; | ||
|
|
||
| import org.springframework.lang.Nullable; | ||
|
|
||
| import io.lettuce.core.protocol.RedisCommand; | ||
| import io.lettuce.core.tracing.Tracing.Endpoint; | ||
| import io.micrometer.observation.Observation; | ||
| import io.micrometer.observation.transport.Kind; | ||
| import io.micrometer.observation.transport.SenderContext; | ||
|
|
||
| /** | ||
| * Micrometer {@link Observation.Context} holding Lettuce contextual details. | ||
| * | ||
| * @author Mark Paluch | ||
| * @since 3.0 | ||
| */ | ||
| class LettuceObservationContext extends SenderContext<Object> { | ||
|
|
||
| private volatile @Nullable RedisCommand<?, ?, ?> command; | ||
|
|
||
| private volatile @Nullable Endpoint endpoint; | ||
|
|
||
| public LettuceObservationContext(String serviceName) { | ||
| super((carrier, key, value) -> {}, Kind.CLIENT); | ||
| setRemoteServiceName(serviceName); | ||
| } | ||
|
|
||
| public RedisCommand<?, ?, ?> getRequiredCommand() { | ||
|
|
||
| RedisCommand<?, ?, ?> local = command; | ||
|
|
||
| if (local == null) { | ||
| throw new IllegalArgumentException("LettuceObservationContext is not associated with a Command"); | ||
| } | ||
|
|
||
| return local; | ||
| } | ||
|
|
||
| public void setCommand(RedisCommand<?, ?, ?> command) { | ||
| this.command = command; | ||
| } | ||
|
|
||
| public Endpoint getRequiredEndpoint() { | ||
|
|
||
| Endpoint local = endpoint; | ||
|
|
||
| if (local == null) { | ||
| throw new IllegalArgumentException("LettuceObservationContext is not associated with a Endpoint"); | ||
| } | ||
|
|
||
| return local; | ||
| } | ||
|
|
||
| public void setEndpoint(Endpoint endpoint) { | ||
| this.endpoint = endpoint; | ||
| } | ||
| } |
34 changes: 34 additions & 0 deletions
34
...ngframework/data/redis/connection/lettuce/observability/LettuceObservationConvention.java
This file contains hidden or 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,34 @@ | ||
| /* | ||
| * Copyright 2022 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 org.springframework.data.redis.connection.lettuce.observability; | ||
|
|
||
| import io.micrometer.observation.Observation; | ||
| import io.micrometer.observation.ObservationConvention; | ||
|
|
||
| /** | ||
| * {@link ObservationConvention} for {@link LettuceObservationContext}. | ||
| * | ||
| * @author Mark Paluch | ||
| * @since 3.0 | ||
| */ | ||
| interface LettuceObservationConvention extends ObservationConvention<LettuceObservationContext> { | ||
|
|
||
| @Override | ||
| default boolean supportsContext(Observation.Context context) { | ||
| return context instanceof LettuceObservationContext; | ||
| } | ||
|
|
||
| } |
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
You can simplify this section - check our docs https://micrometer.io/docs/observation#_documentation_building