Skip to content

Conversation

@realark
Copy link
Collaborator

@realark realark commented Dec 29, 2025

No description provided.

@realark realark added the enhancement New feature or request label Dec 29, 2025
@realark realark force-pushed the ark/langchain4j-instrumentation branch 2 times, most recently from 8db59a5 to 8c94b9c Compare December 29, 2025 18:03
@realark realark marked this pull request as ready for review December 29, 2025 18:34
@realark realark force-pushed the ark/langchain4j-instrumentation branch from 8c94b9c to f09d73b Compare December 29, 2025 18:51
@SneakyThrows
void testSyncChatCompletion() {
// Mock the OpenAI API response
wireMock.stubFor(
Copy link
Collaborator Author

@realark realark Dec 29, 2025

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

TODO: my next quality-of-life change will be to switch out stubs for VCR-like replay (wiremock supports this apparently). Some time in the next week or two

@realark realark requested review from clutchski and delner December 29, 2025 19:02
Copy link

@delner delner left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Some questions but nothing blocking!

public record Options(String providerName) {}

@SuppressWarnings("unchecked")
private static <T> T getPrivateField(Object obj, String fieldName)
Copy link

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

This question is just for my own education: it looks like we're using reflection to access the private fields in order to instrument them, correct? What are the performance/stability risks associated with reflection? Are there other practical alternatives for instrumentation?

In the Ruby world, we generally would avoid accessing private fields because of the potential for instability (e.g. someone in a patch version changes the API.)

Copy link
Collaborator Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Yes that's right, we're using reflection. There isn't much risk in this case because we'll just fail to apply instrumentation if something goes wrong

Performance is pretty good with reflection, but even if it wasn't this is only done once during client build

There isn't a viable alternative right now, but once we get into auto instrumentation for java we'll have more options

Span span = startNewSpan(getSpanName(providerInfo));
try (Scope scope = span.makeCurrent()) {
tagSpan(span, request, providerInfo);
final long startTime = System.nanoTime();
Copy link

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

What is nanoTime? Is it wall clock time or is it something else?

Copy link
Collaborator Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Basically wall clock time. It's an increasing nanosecond counter from an arbitrary starting point

+ " after %d attempts",
minSpanCount, spans.size(), attempts));
}
Thread.sleep(1000);
Copy link

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Why do you need to wait & sleep? To read off the OTel thread? Is there a faster, more directly way to do this synchronously in the test suite?

Copy link
Collaborator Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Usually waiting isn't needed but some of the streaming tests finish their spans after this method is invoked for the first time

I feel like there should be a better way to do this, but the only gotcha is I'm using the built in otel utils to collect spans so I'm not sure what hooks I would have to insert concurrency signaling stuff

I'm making some other changes to the test harness in another branch. I'll add this to that work. At the very least I can dial down the sleep time (10ms should be plenty)

@realark realark merged commit 1b58fef into main Dec 30, 2025
1 check passed
@realark realark deleted the ark/langchain4j-instrumentation branch December 30, 2025 09:30
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

enhancement New feature or request

Projects

None yet

Development

Successfully merging this pull request may close these issues.

3 participants