Skip to content

Commit

Permalink
Merge branch 'main' into thiagohora/batch_spans_creation
Browse files Browse the repository at this point in the history
  • Loading branch information
thiagohora authored Sep 10, 2024
2 parents 1d26361 + ef4c23d commit 0b80bc1
Show file tree
Hide file tree
Showing 15 changed files with 159 additions and 14 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -347,12 +347,12 @@ public Mono<Long> save(@NonNull UUID datasetId, @NonNull List<DatasetItem> items
return Mono.empty();
}

return asyncTemplate
.nonTransaction(connection -> mapAndInsert(datasetId, items, connection, INSERT_DATASET_ITEM));
return asyncTemplate.nonTransaction(connection -> mapAndInsert(
datasetId, items, connection, INSERT_DATASET_ITEM));
}

private Mono<Long> mapAndInsert(UUID datasetId, List<DatasetItem> items, Connection connection,
String sqlTemplate) {
private Mono<Long> mapAndInsert(
UUID datasetId, List<DatasetItem> items, Connection connection, String sqlTemplate) {

List<QueryItem> queryItems = getQueryItemPlaceHolder(items.size());

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -203,7 +203,7 @@ INSERT INTO spans(
:metadata as metadata,
:tags as tags,
mapFromArrays(:usage_keys, :usage_values) as usage,
now64() as created_at,
now64(9) as created_at,
:user_name as created_by,
:user_name as last_updated_by
) as new_span
Expand Down Expand Up @@ -374,7 +374,7 @@ INSERT INTO spans(
<if(metadata)> :metadata <else> '' <endif> as metadata,
<if(tags)> :tags <else> [] <endif> as tags,
<if(usage)> CAST((:usageKeys, :usageValues), 'Map(String, Int64)') <else> mapFromArrays([], []) <endif> as usage,
now64() as created_at,
now64(9) as created_at,
:user_name as created_by,
:user_name as last_updated_by
) as new_span
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -675,8 +675,8 @@ private void bindSearchCriteria(TraceSearchCriteria traceSearchCriteria, Stateme

@Override
@com.newrelic.api.agent.Trace(dispatcher = true)
public Mono<List<WorkspaceAndResourceId>> getTraceWorkspace(@NonNull Set<UUID> traceIds,
@NonNull Connection connection) {
public Mono<List<WorkspaceAndResourceId>> getTraceWorkspace(
@NonNull Set<UUID> traceIds, @NonNull Connection connection) {

if (traceIds.isEmpty()) {
return Mono.just(List.of());
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,7 @@ sidebar_label: Log Distributed Traces

# Log Distributed Traces

When working with complex LLM applications, it is common to need to track a traces across multiple services. Comet supports distributed tracing out of the box when integrating using function annotators using a mechanism that is similar to how OpenTelemetry implements distributed tracing.
When working with complex LLM applications, it is common to need to track a traces across multiple services. Comet supports distributed tracing out of the box when integrating using function decorators using a mechanism that is similar to how OpenTelemetry implements distributed tracing.

For the purposes of this guide, we will assume that you have a simple LLM application that is made up of two services: a client and a server. We will assume that the client will create the trace and span, while the server will add a nested span. In order to do this, the `trace_id` and `span_id` will be passed in the headers of the request from the client to the server.

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -15,15 +15,15 @@ To log traces to the Comet LLM Evaluation platform using the Python SDK, you wil
pip install opik
```

Once the SDK is installed, you can log traces to using one our Comet's integration, function annotations or manually.
Once the SDK is installed, you can log traces to using one our Comet's integration, function decorators or manually.

:::tip
Opik has a number of integrations for popular LLM frameworks like LangChain or OpenAI, checkout a full list of integrations in the [integrations](/tracing/integrations/overview.md) section.
:::

## Log using function annotators
## Log using function decorators

If you are manually defining your LLM chains and not using LangChain for example, you can use the `track` function annotators to track LLM calls:
If you are manually defining your LLM chains and not using LangChain for example, you can use the `track` function decorators to track LLM calls:

```python
from opik import track
Expand Down Expand Up @@ -61,7 +61,7 @@ print(result)
```

:::tip
If the `track` function annotators are used in conjunction with the `track_openai` or `CometTracer` callbacks, the LLM calls will be automatically logged to the corresponding trace.
If the `track` function decorators are used in conjunction with the `track_openai` or `CometTracer` callbacks, the LLM calls will be automatically logged to the corresponding trace.
:::

## Log traces and spans manually
Expand Down
1 change: 1 addition & 0 deletions apps/opik-frontend/index.html
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,7 @@
<link rel="icon" type="image/svg+xml" href="/favicon.ico" />
<meta name="viewport" content="width=device-width, initial-scale=1.0" />
<title>Comet Opik</title>
<script type='text/javascript' src='../../config.js?version=1725550071774'></script>
</head>
<body class="size-full">
<div class="size-full" id="root"></div>
Expand Down
6 changes: 6 additions & 0 deletions apps/opik-frontend/package-lock.json

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

1 change: 1 addition & 0 deletions apps/opik-frontend/package.json
Original file line number Diff line number Diff line change
Expand Up @@ -48,6 +48,7 @@
"@tanstack/react-router": "^1.36.3",
"@tanstack/react-table": "^8.17.3",
"@types/md5": "^2.3.5",
"@types/segment-analytics": "^0.0.38",
"@uiw/react-codemirror": "^4.23.0",
"axios": "^1.7.2",
"class-variance-authority": "^0.7.0",
Expand Down
1 change: 1 addition & 0 deletions apps/opik-frontend/public/config.js
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
window.environmentVariablesOverwrite = {};
3 changes: 3 additions & 0 deletions apps/opik-frontend/src/plugins/comet/WorkspacePreloader.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -13,6 +13,7 @@ import { MoveLeft } from "lucide-react";
import useUser from "./useUser";
import { buildUrl } from "./utils";
import imageLogoUrl from "/images/logo_and_text.png";
import useSegment from "@/plugins/comet/analytics/useSegment";

type WorkspacePreloaderProps = {
children: React.ReactNode;
Expand All @@ -29,6 +30,8 @@ const WorkspacePreloader: React.FunctionComponent<WorkspacePreloaderProps> = ({
});
const isRootPath = matchRoute({ to: "/" });

useSegment(user?.userName);

if (isLoading) {
return <Loader />;
}
Expand Down
16 changes: 16 additions & 0 deletions apps/opik-frontend/src/plugins/comet/analytics/index.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,16 @@
import initSnippet from "./snippet";

export const initAnalytics = (writeKey?: string) => {
if (writeKey) {
initSnippet(writeKey);
}
};

export const trackEvent = (
name: string,
properties: Record<string, unknown>,
) => {
if (window.analytics) {
window.analytics.track(name, properties);
}
};
83 changes: 83 additions & 0 deletions apps/opik-frontend/src/plugins/comet/analytics/snippet.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,83 @@
export default function initSnippet(writeKey) {
var i = "analytics",
analytics = (window[i] = window[i] || []);
if (!analytics.initialize)
if (analytics.invoked)
window.console &&
console.error &&
console.error("Segment snippet included twice.");
else {
analytics.invoked = !0;
analytics.methods = [
"trackSubmit",
"trackClick",
"trackLink",
"trackForm",
"pageview",
"identify",
"reset",
"group",
"track",
"ready",
"alias",
"debug",
"page",
"screen",
"once",
"off",
"on",
"addSourceMiddleware",
"addIntegrationMiddleware",
"setAnonymousId",
"addDestinationMiddleware",
"register",
];
analytics.factory = function (e) {
return function () {
if (window[i].initialized)
return window[i][e].apply(window[i], arguments);
var n = Array.prototype.slice.call(arguments);
if (
["track", "screen", "alias", "group", "page", "identify"].indexOf(
e,
) > -1
) {
var c = document.querySelector("link[rel='canonical']");
n.push({
__t: "bpc",
c: (c && c.getAttribute("href")) || void 0,
p: location.pathname,
u: location.href,
s: location.search,
t: document.title,
r: document.referrer,
});
}
n.unshift(e);
analytics.push(n);
return analytics;
};
};
for (var n = 0; n < analytics.methods.length; n++) {
var key = analytics.methods[n];
analytics[key] = analytics.factory(key);
}
analytics.load = function (key, n) {
var t = document.createElement("script");
t.type = "text/javascript";
t.async = !0;
t.setAttribute("data-global-segment-analytics-key", i);
t.src =
"https://cdn.segment.com/analytics.js/v1/" +
key +
"/analytics.min.js";
var r = document.getElementsByTagName("script")[0];
r.parentNode.insertBefore(t, r);
analytics._loadOptions = n;
};
analytics._writeKey = writeKey;
analytics.SNIPPET_VERSION = "5.2.0";
analytics.load(writeKey);
analytics.page();
}
}
11 changes: 11 additions & 0 deletions apps/opik-frontend/src/plugins/comet/analytics/useSegment.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,11 @@
import { useEffect } from "react";

const useSegment = (username?: string) => {
useEffect(() => {
if (window.analytics && username) {
window.analytics.identify(username);
}
}, [username]);
};

export default useSegment;
16 changes: 16 additions & 0 deletions apps/opik-frontend/src/plugins/comet/init.tsx
Original file line number Diff line number Diff line change
@@ -0,0 +1,16 @@
/// <reference types="@types/segment-analytics" />

import { initAnalytics } from "./analytics";

type EnvironmentVariablesOverwrite = {
OPIK_SEGMENT_ID?: string;
};

declare global {
interface Window {
analytics: SegmentAnalytics.AnalyticsJS;
environmentVariablesOverwrite: EnvironmentVariablesOverwrite;
}
}

initAnalytics(window.environmentVariablesOverwrite.OPIK_SEGMENT_ID);
9 changes: 8 additions & 1 deletion apps/opik-frontend/src/store/PluginsStore.ts
Original file line number Diff line number Diff line change
Expand Up @@ -7,16 +7,23 @@ type PluginStore = {
GetStartedPage: React.ComponentType | null;
UserMenu: React.ComponentType | null;
WorkspacePreloader: React.ComponentType<{ children: React.ReactNode }> | null;
init: unknown;
setupPlugins: (folderName: string) => Promise<void>;
};

const VALID_PLUGIN_FOLDER_NAMES = ["comet"];
const PLUGIN_NAMES = ["GetStartedPage", "UserMenu", "WorkspacePreloader"];
const PLUGIN_NAMES = [
"GetStartedPage",
"UserMenu",
"WorkspacePreloader",
"init",
];

const usePluginsStore = create<PluginStore>((set) => ({
GetStartedPage: null,
UserMenu: null,
WorkspacePreloader: null,
init: null,
setupPlugins: async (folderName: string) => {
if (!VALID_PLUGIN_FOLDER_NAMES.includes(folderName)) {
return set({ WorkspacePreloader });
Expand Down

0 comments on commit 0b80bc1

Please sign in to comment.