diff --git a/micrometer-observation/src/main/java/io/micrometer/observation/Observation.java b/micrometer-observation/src/main/java/io/micrometer/observation/Observation.java index e32a0ed616..1fcadd38b6 100644 --- a/micrometer-observation/src/main/java/io/micrometer/observation/Observation.java +++ b/micrometer-observation/src/main/java/io/micrometer/observation/Observation.java @@ -114,7 +114,8 @@ static Observation createNotStarted(String name, @Nullable Context context, * observation. Allows to set a custom {@link ObservationConvention} and requires to * provide a default one if a neither a custom nor a pre-configured one (via * {@link ObservationRegistry.ObservationConfig#getObservationConvention(Context, ObservationConvention)}) - * was found. + * was found. The {@link ObservationConvention} implementation can override + * {@link Observation} names (i.e. name and contextual name) and key values. * @param type of context * @param customConvention custom convention. If {@code null}, the default one will be * picked @@ -152,12 +153,13 @@ static Observation start(ObservationConvention observationConvention, @Nullab /** * Creates and starts an {@link Observation}. When no registry is passed or * observation is not applicable will return a no-op observation. + * @param type of context * @param observationConvention observation convention * @param context mutable context * @param registry observation registry * @return started observation */ - static Observation start(ObservationConvention observationConvention, @Nullable Context context, + static Observation start(ObservationConvention observationConvention, @Nullable T context, @Nullable ObservationRegistry registry) { return createNotStarted(observationConvention, context, registry).start(); } @@ -204,19 +206,34 @@ static Observation createNotStarted(ObservationConvention observationConventi * {@link Observation#start()} when you want the measurements to start. When no * registry is passed or observation is not applicable will return a no-op * observation. + *

+ * Important! If you're using the + * {@link ObservationConvention#getContextualName(Context)} method to override the + * contextual name you MUST use a non {@code null} context (i.e. the + * {@code context} parameter of this method MUST NOT be {@code null}. The + * {@link ObservationConvention#getContextualName(Context)} requires a concrete type + * of {@link Observation.Context} to be passed and if you're not providing one we + * won't be able to initialize it ourselves. + *

+ * @param type of context * @param observationConvention observation convention * @param context mutable context * @param registry observation registry * @return created but not started observation */ - static Observation createNotStarted(ObservationConvention observationConvention, @Nullable Context context, - @Nullable ObservationRegistry registry) { + static Observation createNotStarted(ObservationConvention observationConvention, + @Nullable T context, @Nullable ObservationRegistry registry) { if (registry == null || registry.isNoop() || !registry.observationConfig().isObservationEnabled(observationConvention.getName(), context) || observationConvention == NoopObservationConvention.INSTANCE) { return NoopObservation.INSTANCE; } - return new SimpleObservation(observationConvention, registry, context == null ? new Context() : context); + Context c = context == null ? new Context() : context; + SimpleObservation simpleObservation = new SimpleObservation(observationConvention, registry, c); + if (context != null) { + simpleObservation.contextualName(observationConvention.getContextualName(context)); + } + return simpleObservation; } /** @@ -939,6 +956,17 @@ interface ObservationConvention */ String getName(); + /** + * Allows to override the contextual name for an {@link Observation}. The + * {@link Observation} will be renamed only when an explicit context was passed - + * if an implicit context is used this method won't be called. + * @param context context + * @return the new, contextual name for the observation + */ + default String getContextualName(T context) { + return null; + } + } /** diff --git a/micrometer-observation/src/main/java/io/micrometer/observation/docs/DocumentedObservation.java b/micrometer-observation/src/main/java/io/micrometer/observation/docs/DocumentedObservation.java index 34156c3a77..3e79d3d1bc 100644 --- a/micrometer-observation/src/main/java/io/micrometer/observation/docs/DocumentedObservation.java +++ b/micrometer-observation/src/main/java/io/micrometer/observation/docs/DocumentedObservation.java @@ -65,8 +65,9 @@ default String getName() { } /** - * Default naming convention (sets a technical name and key values). You can set the - * name either by this method or {@link #getName()} ()}. You can't use both. + * Default naming convention (sets a technical and contextual names, and key values). + * You can set the names either by this method or {@link #getName()} and + * {@link #getContextualName()}. * @return default naming convention */ default Class> getDefaultConvention() { @@ -74,7 +75,9 @@ default Class true); @@ -85,6 +97,14 @@ public String getContextualName() { return "contextual"; } + @Override + public Class> getDefaultConvention() { + return FirstObservationConvention.class; + } + }, + + CONTEXTUAL_NAME { + @Override public Class> getDefaultConvention() { return FirstObservationConvention.class; @@ -145,4 +165,33 @@ public boolean supportsContext(Observation.Context context) { } + static class ContextualObservation extends FirstObservationConvention { + + @Override + public String getName() { + return "technical name"; + } + + @Override + public String getContextualName(Observation.Context context) { + return "contextual name"; + } + + @Override + public KeyValues getLowCardinalityKeyValues(Observation.Context context) { + return KeyValues.of("low key", "low value"); + } + + @Override + public KeyValues getHighCardinalityKeyValues(Observation.Context context) { + return KeyValues.of("high key", "high value"); + } + + @Override + public boolean supportsContext(Observation.Context context) { + return true; + } + + } + }