Skip to content

Commit

Permalink
adding observation in messaging and data
Browse files Browse the repository at this point in the history
  • Loading branch information
salaboy committed Oct 4, 2024
1 parent 8b17f5a commit 5590b89
Show file tree
Hide file tree
Showing 11 changed files with 258 additions and 105 deletions.
18 changes: 4 additions & 14 deletions dapr-spring/dapr-spring-boot-autoconfigure/pom.xml
Original file line number Diff line number Diff line change
Expand Up @@ -31,6 +31,10 @@
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-web</artifactId>
</dependency>
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-autoconfigure</artifactId>
</dependency>
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-autoconfigure-processor</artifactId>
Expand All @@ -44,20 +48,6 @@
<groupId>io.opentelemetry</groupId>
<artifactId>opentelemetry-exporter-otlp</artifactId>
</dependency>
<dependency>
<groupId>io.opentelemetry.instrumentation</groupId>
<artifactId>opentelemetry-instrumentation-annotations</artifactId>
<version>2.8.0</version>
</dependency>
<dependency>
<groupId>io.micrometer</groupId>
<artifactId>micrometer-registry-otlp</artifactId>
</dependency>
<dependency>
<groupId>net.ttddyy.observation</groupId>
<artifactId>datasource-micrometer-spring-boot</artifactId>
<version>1.0.5</version>
</dependency>
<dependency>
<groupId>org.springframework.data</groupId>
<artifactId>spring-data-keyvalue</artifactId>
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,81 @@
package io.dapr.spring.data.observation;

import io.micrometer.observation.transport.SenderContext;

import java.util.HashMap;
import java.util.Map;


/**
* {@link SenderContext} for Dapr KeyValue context.
*
*/
public final class DaprKeyValueContext extends SenderContext<DaprKeyValueContext.KeyValueHolder> {

private final String beanName;

private final String keyValueStore;


private DaprKeyValueContext(KeyValueHolder keyValueHolder, String keyValueStore, String beanName) {
super((carrier, key, value) -> keyValueHolder.property(key, value));
setCarrier(keyValueHolder);
this.beanName = beanName;
this.keyValueStore = keyValueStore;
}

/**
* Create a new context.
* @param kvStore KVStore to be used
* @param beanName name of the bean used usually (typically a {@code DaprMessagingTemplate})
* @return DaprMessageSenderContext
*/
public static DaprKeyValueContext newContext(String kvStore, String beanName) {
KeyValueHolder keyValueHolder = new KeyValueHolder();
return new DaprKeyValueContext(keyValueHolder, kvStore, beanName);
}

public Map<String, String> properties() {
return getCarrier().properties();
}


/**
* The name of the bean sending the message (typically a {@code DaprMessagingTemplate}).
* @return the name of the bean sending the message
*/
public String getBeanName() {
return this.beanName;
}

/**
* The destination topic for the message.
* @return the topic the message is being sent to
*/
public String getKeyValueStore() {
return this.keyValueStore;
}


/**
* Acts as a carrier for a Dapr KeyValue and records the propagated properties for
* later access by the Dapr.
*/
public static final class KeyValueHolder {

private final Map<String, String> properties = new HashMap<>();

private KeyValueHolder() {
}

public void property(String key, String value) {
this.properties.put(key, value);
}

public Map<String, String> properties() {
return this.properties;
}

}

}
Original file line number Diff line number Diff line change
@@ -0,0 +1,56 @@
package io.dapr.spring.data.observation;

import io.micrometer.common.docs.KeyName;
import io.micrometer.observation.Observation;
import io.micrometer.observation.Observation.Context;
import io.micrometer.observation.ObservationConvention;
import io.micrometer.observation.docs.ObservationDocumentation;

/**
* An {@link Observation} for {@link io.dapr.spring.data.DaprKeyValueTemplate}.
*
*/
public enum DaprKeyValueTemplateObservation implements ObservationDocumentation {

/**
* Observation created when a Dapr template interacts with a KVStore.
*/
TEMPLATE_OBSERVATION {

@Override
public Class<? extends ObservationConvention<? extends Context>> getDefaultConvention() {
return DefaultDaprKeyValueTemplateObservationConvention.class;
}

@Override
public String getPrefix() {
return "spring.dapr.data.template";
}

@Override
public KeyName[] getLowCardinalityKeyNames() {
return TemplateLowCardinalityTags.values();
}

};

/**
* Low cardinality tags.
*/
public enum TemplateLowCardinalityTags implements KeyName {

/**
* Bean name of the template that interacts with the kv store.
*/
BEAN_NAME {

@Override
public String asString() {
return "spring.dapr.data.template.name";
}

}

}

}
Original file line number Diff line number Diff line change
@@ -0,0 +1,23 @@
package io.dapr.spring.data.observation;


import io.micrometer.observation.Observation.Context;
import io.micrometer.observation.ObservationConvention;

/**
* {@link ObservationConvention} for Dapr KV template .
*
*/
public interface DaprKeyValueTemplateObservationConvention extends ObservationConvention<DaprKeyValueContext> {

@Override
default boolean supportsContext(Context context) {
return context instanceof DaprKeyValueContext;
}

@Override
default String getName() {
return "spring.dapr.data.template";
}

}
Original file line number Diff line number Diff line change
@@ -0,0 +1,35 @@
package io.dapr.spring.data.observation;

import io.micrometer.common.KeyValues;

/**
* Default {@link DefaultDaprKeyValueTemplateObservationConvention} for Dapr template key values.
*
*/
public class DefaultDaprKeyValueTemplateObservationConvention implements DaprKeyValueTemplateObservationConvention {

/**
* A singleton instance of the convention.
*/
public static final DefaultDaprKeyValueTemplateObservationConvention INSTANCE =
new DefaultDaprKeyValueTemplateObservationConvention();

@Override
public KeyValues getLowCardinalityKeyValues(DaprKeyValueContext context) {
return KeyValues.of(DaprKeyValueTemplateObservation.TemplateLowCardinalityTags.BEAN_NAME.asString(),
context.getBeanName());
}

// Remove once addressed:
// https://github.com/micrometer-metrics/micrometer-docs-generator/issues/30
@Override
public String getName() {
return "spring.dapr.data.template";
}

@Override
public String getContextualName(DaprKeyValueContext context) {
return context.getKeyValueStore() + " store";
}

}
4 changes: 4 additions & 0 deletions dapr-spring/dapr-spring-messaging/pom.xml
Original file line number Diff line number Diff line change
Expand Up @@ -17,6 +17,10 @@
<groupId>io.opentelemetry</groupId>
<artifactId>opentelemetry-api</artifactId>
</dependency>
<dependency>
<groupId>io.micrometer</groupId>
<artifactId>micrometer-tracing</artifactId>
</dependency>
</dependencies>
<packaging>jar</packaging>

Expand Down
Loading

0 comments on commit 5590b89

Please sign in to comment.