Skip to content
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

Restructure recorded metrics #155

Merged
merged 1 commit into from
Dec 14, 2023
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
Expand Up @@ -111,6 +111,7 @@ public class AiServicesProcessor {
private static final MethodDescriptor QUARKUS_AI_SERVICES_CONTEXT_REMOVE_CHAT_MEMORY_IDS = MethodDescriptor.ofMethod(
QuarkusAiServiceContext.class, "removeChatMemoryIds", void.class, Object[].class);
public static final DotName CDI_INSTANCE = DotName.createSimple(Instance.class);
private static final String[] EMPTY_STRING_ARRAY = new String[0];

@BuildStep
public void nativeSupport(CombinedIndexBuildItem indexBuildItem,
Expand Down Expand Up @@ -775,7 +776,12 @@ private Optional<AiServiceMethodCreateInfo.MetricsInfo> gatherMetricsInfo(Method
return Optional.empty();
}

String name = defaultAiServiceMetricName(method);
String name = "langchain4j.aiservices";
List<String> tags = new ArrayList<>();
tags.add("aiservice");
tags.add(method.declaringClass().name().withoutPackagePrefix());
tags.add("method");
tags.add(method.name());

AnnotationInstance timedInstance = method.annotation(MICROMETER_TIMED);
if (timedInstance == null) {
Expand All @@ -784,7 +790,8 @@ private Optional<AiServiceMethodCreateInfo.MetricsInfo> gatherMetricsInfo(Method

if (timedInstance == null) {
// we default to having all AiServices being timed
return Optional.of(new AiServiceMethodCreateInfo.MetricsInfo.Builder(name).build());
return Optional.of(new AiServiceMethodCreateInfo.MetricsInfo.Builder(name)
.setExtraTags(tags.toArray(EMPTY_STRING_ARRAY)).build());
}

AnnotationValue nameValue = timedInstance.value();
Expand All @@ -799,8 +806,9 @@ private Optional<AiServiceMethodCreateInfo.MetricsInfo> gatherMetricsInfo(Method

AnnotationValue extraTagsValue = timedInstance.value("extraTags");
if (extraTagsValue != null) {
builder.setExtraTags(extraTagsValue.asStringArray());
tags.addAll(Arrays.asList(extraTagsValue.asStringArray()));
}
builder.setExtraTags(tags.toArray(EMPTY_STRING_ARRAY));

AnnotationValue longTaskValue = timedInstance.value("longTask");
if (longTaskValue != null) {
Expand Down Expand Up @@ -838,10 +846,6 @@ private Optional<AiServiceMethodCreateInfo.SpanInfo> gatherSpanInfo(MethodInfo m
return Optional.of(new AiServiceMethodCreateInfo.SpanInfo(name));
}

private String defaultAiServiceMetricName(MethodInfo method) {
return "langchain4j.aiservices." + method.declaringClass().name().withoutPackagePrefix() + "." + method.name();
}

private String defaultAiServiceSpanName(MethodInfo method) {
return "langchain4j.aiservices." + method.declaringClass().name().withoutPackagePrefix() + "." + method.name();
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -146,10 +146,6 @@ public static class MetricsInfo {
private final boolean histogram;
private final String description;

public MetricsInfo(String name) {
this(name, false, null, null, false, null);
}

@RecordableConstructor
public MetricsInfo(String name, boolean longTask, String[] extraTags, double[] percentiles, boolean histogram,
String description) {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -43,7 +43,8 @@ public void noTimedAnnotations() throws InterruptedException {
.statusCode(200)
.body(containsString("MockGPT"));

waitForMeters(registry.find("langchain4j.aiservices.AssistantResourceWithMetrics$Assistant1.chat").timers(), 1);
waitForMeters(registry.find("langchain4j.aiservices").tag("aiservice", "AssistantResourceWithMetrics$Assistant1")
.tag("method", "chat").timers(), 1);
}

@Test
Expand All @@ -55,8 +56,8 @@ public void timedAnnotationOnClass() throws InterruptedException {
.statusCode(200)
.body(containsString("MockGPT"));

waitForMeters(registry.find("langchain4j.aiservices.AssistantResourceWithMetrics$Assistant2.chat").tag("key", "value")
.timers(), 1);
waitForMeters(registry.find("langchain4j.aiservices").tag("aiservice", "AssistantResourceWithMetrics$Assistant2")
.tag("method", "chat").tag("key", "value").timers(), 1);
}

@Test
Expand Down