Skip to content

Commit 207f9fe

Browse files
authored
feat: introduce traceloop.entity.path instead of traceloop.entity.name chaining (#393)
1 parent 10111be commit 207f9fe

File tree

6 files changed

+35
-33
lines changed

6 files changed

+35
-33
lines changed

packages/ai-semantic-conventions/src/SemanticAttributes.ts

+1
Original file line numberDiff line numberDiff line change
@@ -51,6 +51,7 @@ export const SpanAttributes = {
5151
TRACELOOP_SPAN_KIND: "traceloop.span.kind",
5252
TRACELOOP_WORKFLOW_NAME: "traceloop.workflow.name",
5353
TRACELOOP_ENTITY_NAME: "traceloop.entity.name",
54+
TRACELOOP_ENTITY_PATH: "traceloop.entity.path",
5455
TRACELOOP_ENTITY_VERSION: "traceloop.entity.version",
5556
TRACELOOP_ASSOCIATION_PROPERTIES: "traceloop.association.properties",
5657
TRACELOOP_ENTITY_INPUT: "traceloop.entity.input",

packages/traceloop-sdk/recordings/Test-SDK-Decorators_847855269/should-create-workflow-and-tasks-spans-with-chained-entity-names_971051426/recording.har

+17-17
Original file line numberDiff line numberDiff line change
@@ -63,7 +63,7 @@
6363
{
6464
"_fromType": "array",
6565
"name": "x-stainless-runtime-version",
66-
"value": "v22.0.0"
66+
"value": "v20.9.0"
6767
},
6868
{
6969
"_fromType": "array",
@@ -87,23 +87,23 @@
8787
"url": "https://api.openai.com/v1/chat/completions"
8888
},
8989
"response": {
90-
"bodySize": 451,
90+
"bodySize": 483,
9191
"content": {
9292
"encoding": "base64",
9393
"mimeType": "application/json",
94-
"size": 451,
95-
"text": "[\"H4sIAAAAAAAAA1SQwU7DMBBE7/mKxecGNalCaS4IJJA4cQDUIoQqx9kmBsfr2luJUvXfkdO0hcseZjyzb71LAISuRQlCtZJV50w6o/v8+SFU6/l8dfWq5k/ju8efFnGxvl28iVFMUPWJio+pS0WdM8ia7MFWHiVjbM2meTYtppNZ3hsd1WhirHGcTi6LlDe+onSc5cWQbEkrDKKE9wQAYNfPyGhr/BYljEdHpcMQZIOiPD0CEJ5MVIQMQQeWlsXobCqyjLbHnrdbqHUN\",\"3CI47SUjNARMvVBJfwMvBA0ytDpAQAkGm3Ahhq79CcJQ4zxVEdhujDnpK211aJceZSAbFwYmd4jvE4CP/tjNP37hPHWOl0xfaGNhNjnUifP3/jGLwWRiac56fp0MfCJsA2O3XGnboHde95dHymSf/AIAAP//AwC01euq+AEAAA==\"]"
94+
"size": 483,
95+
"text": "[\"H4sIAAAAAAAAA1RRy07DMBC85ysWn5uqD1raXlDhAhcOgaoHhCrX2SamjteyN0Co+u/I6SNCsnyY8czOrA8JgNC5WIBQpWRVOZPO/cu6mD+9rlZovNub7996KWeNenz78s+iFxW0/UTFF1VfUeUMsiZ7opVHyRhdh3ej0WQwm91OW6KiHE2UFY7TcX+Scu23lA6Go8lZWZJWGMQC3hMAgEN7x4w2xx+xgEHvglQYgixQLK6PAIQnExEhQ9CBpWXR60hFltG2sddlA7nO\",\"gUsEp71khIKAqQUCyh1RDh4Dy9pLy/fwgErWAaGMR/pW2UApcyhiWVhmWZalrFVJeww34jz1eI1rqHCetrGarY254jttdSg3HmUgG6MFJneSHxOAj3Yt9b+mwnmqHG+Y9mij4XB8shPdR3Tk6EIysTQdPp4m53wiNIGx2uy0LdA7r9sdxZTJMfkDAAD//wMAnY2BWCICAAA=\"]"
9696
},
9797
"cookies": [
9898
{
9999
"domain": ".api.openai.com",
100-
"expires": "2024-07-23T18:26:32.000Z",
100+
"expires": "2024-08-01T11:10:46.000Z",
101101
"httpOnly": true,
102102
"name": "__cf_bm",
103103
"path": "/",
104104
"sameSite": "None",
105105
"secure": true,
106-
"value": "H91GR4kfVHvrZNLG6C4p7cTy0Rje3q95RXaNRra.E7w-1721757392-1.0.1.1-THuuXeeZjJ_kAnzn8lv.3KorJ7IgwtB9_uFiWMgJmsAZgN16Q6Y3ilq2JBkbQtS8SyVJjCY9Dup01866tcpTYQ"
106+
"value": "_uyjIAmPtprjMUnxpxWrcM4qg2SrJ2u_yKIqkOpt21E-1722508846-1.0.1.1-H8bVzK3.7NaGDI5xd4QkQnpqj5v32PfarcAkFuS0IqttkcBE7gNC_ccdt6yRXEETNwk4Cpj7ZSP_O44oN9vOXQ"
107107
},
108108
{
109109
"domain": ".api.openai.com",
@@ -112,13 +112,13 @@
112112
"path": "/",
113113
"sameSite": "None",
114114
"secure": true,
115-
"value": ".sNQn3QGP675mXVWZPtdu1NGO9R3ZBNRvsCPus1F6mY-1721757392468-0.0.1.1-604800000"
115+
"value": "DlrBSXW7FfrKzgttaW40jYSLKJqthnSdrCOCl5tOw9w-1722508846988-0.0.1.1-604800000"
116116
}
117117
],
118118
"headers": [
119119
{
120120
"name": "date",
121-
"value": "Tue, 23 Jul 2024 17:56:32 GMT"
121+
"value": "Thu, 01 Aug 2024 10:40:46 GMT"
122122
},
123123
{
124124
"name": "content-type",
@@ -138,7 +138,7 @@
138138
},
139139
{
140140
"name": "openai-processing-ms",
141-
"value": "311"
141+
"value": "433"
142142
},
143143
{
144144
"name": "openai-version",
@@ -162,7 +162,7 @@
162162
},
163163
{
164164
"name": "x-ratelimit-remaining-tokens",
165-
"value": "3999976"
165+
"value": "3999975"
166166
},
167167
{
168168
"name": "x-ratelimit-reset-requests",
@@ -174,7 +174,7 @@
174174
},
175175
{
176176
"name": "x-request-id",
177-
"value": "req_992d6f84a8255b5722ef98d224d589a3"
177+
"value": "req_86f55736ff794d16d153773083e89028"
178178
},
179179
{
180180
"name": "cf-cache-status",
@@ -183,12 +183,12 @@
183183
{
184184
"_fromType": "array",
185185
"name": "set-cookie",
186-
"value": "__cf_bm=H91GR4kfVHvrZNLG6C4p7cTy0Rje3q95RXaNRra.E7w-1721757392-1.0.1.1-THuuXeeZjJ_kAnzn8lv.3KorJ7IgwtB9_uFiWMgJmsAZgN16Q6Y3ilq2JBkbQtS8SyVJjCY9Dup01866tcpTYQ; path=/; expires=Tue, 23-Jul-24 18:26:32 GMT; domain=.api.openai.com; HttpOnly; Secure; SameSite=None"
186+
"value": "__cf_bm=_uyjIAmPtprjMUnxpxWrcM4qg2SrJ2u_yKIqkOpt21E-1722508846-1.0.1.1-H8bVzK3.7NaGDI5xd4QkQnpqj5v32PfarcAkFuS0IqttkcBE7gNC_ccdt6yRXEETNwk4Cpj7ZSP_O44oN9vOXQ; path=/; expires=Thu, 01-Aug-24 11:10:46 GMT; domain=.api.openai.com; HttpOnly; Secure; SameSite=None"
187187
},
188188
{
189189
"_fromType": "array",
190190
"name": "set-cookie",
191-
"value": "_cfuvid=.sNQn3QGP675mXVWZPtdu1NGO9R3ZBNRvsCPus1F6mY-1721757392468-0.0.1.1-604800000; path=/; domain=.api.openai.com; HttpOnly; Secure; SameSite=None"
191+
"value": "_cfuvid=DlrBSXW7FfrKzgttaW40jYSLKJqthnSdrCOCl5tOw9w-1722508846988-0.0.1.1-604800000; path=/; domain=.api.openai.com; HttpOnly; Secure; SameSite=None"
192192
},
193193
{
194194
"name": "x-content-type-options",
@@ -200,7 +200,7 @@
200200
},
201201
{
202202
"name": "cf-ray",
203-
"value": "8a7d8c339925b077-ATL"
203+
"value": "8ac53640b9ed09bf-HFA"
204204
},
205205
{
206206
"name": "content-encoding",
@@ -217,16 +217,16 @@
217217
"status": 200,
218218
"statusText": "OK"
219219
},
220-
"startedDateTime": "2024-07-23T17:56:31.166Z",
221-
"time": 1192,
220+
"startedDateTime": "2024-08-01T10:40:46.003Z",
221+
"time": 895,
222222
"timings": {
223223
"blocked": -1,
224224
"connect": -1,
225225
"dns": -1,
226226
"receive": 0,
227227
"send": 0,
228228
"ssl": -1,
229-
"wait": 1192
229+
"wait": 895
230230
}
231231
}
232232
],

packages/traceloop-sdk/src/lib/tracing/decorators.ts

+9-5
Original file line numberDiff line numberDiff line change
@@ -3,7 +3,7 @@ import { suppressTracing } from "@opentelemetry/core";
33
import {
44
ASSOCATION_PROPERTIES_KEY,
55
ENTITY_NAME_KEY,
6-
getChainedEntityName,
6+
getEntityPath,
77
getTracer,
88
WORKFLOW_NAME_KEY,
99
} from "./tracing";
@@ -49,13 +49,13 @@ function withEntity<
4949
entityContext = entityContext.setValue(WORKFLOW_NAME_KEY, name);
5050
}
5151

52-
let entityName = name;
52+
const entityPath = getEntityPath(entityContext);
5353
if (
5454
type === TraceloopSpanKindValues.TOOL ||
5555
type === TraceloopSpanKindValues.TASK
5656
) {
57-
entityName = getChainedEntityName(entityContext, name);
58-
entityContext = entityContext.setValue(ENTITY_NAME_KEY, entityName);
57+
const fullEntityName = entityPath ? `${entityPath}.${name}` : name;
58+
entityContext = entityContext.setValue(ENTITY_NAME_KEY, fullEntityName);
5959
}
6060

6161
if (overrideTraceContent != undefined) {
@@ -87,7 +87,11 @@ function withEntity<
8787
) {
8888
span.setAttribute(SpanAttributes.TRACELOOP_WORKFLOW_NAME, name);
8989
}
90-
span.setAttribute(SpanAttributes.TRACELOOP_ENTITY_NAME, entityName);
90+
span.setAttribute(SpanAttributes.TRACELOOP_ENTITY_NAME, name);
91+
span.setAttribute(
92+
SpanAttributes.TRACELOOP_ENTITY_PATH,
93+
entityPath || "",
94+
);
9195
span.setAttribute(SpanAttributes.TRACELOOP_SPAN_KIND, type);
9296

9397
if (version) {

packages/traceloop-sdk/src/lib/tracing/index.ts

+2-2
Original file line numberDiff line numberDiff line change
@@ -268,7 +268,7 @@ export const startTracing = (options: InitializeOptions) => {
268268
const entityName = context.active().getValue(ENTITY_NAME_KEY);
269269
if (entityName) {
270270
span.setAttribute(
271-
SpanAttributes.TRACELOOP_ENTITY_NAME,
271+
SpanAttributes.TRACELOOP_ENTITY_PATH,
272272
entityName as string,
273273
);
274274
}
@@ -313,7 +313,7 @@ export const startTracing = (options: InitializeOptions) => {
313313
textMapPropagator: options.propagator,
314314
traceExporter,
315315
instrumentations,
316-
// We should re-consider removing unrelevant spans here in the future
316+
// We should re-consider removing irrelevant spans here in the future
317317
// sampler: new TraceloopSampler(),
318318
});
319319

packages/traceloop-sdk/src/lib/tracing/tracing.ts

+3-6
Original file line numberDiff line numberDiff line change
@@ -12,11 +12,8 @@ export const getTracer = () => {
1212
return trace.getTracer(TRACER_NAME);
1313
};
1414

15-
export const getChainedEntityName = (
16-
entityContext: Context,
17-
entityName: string,
18-
): string => {
19-
const parentEntityName = entityContext.getValue(ENTITY_NAME_KEY);
15+
export const getEntityPath = (entityContext: Context): string | undefined => {
16+
const path = entityContext.getValue(ENTITY_NAME_KEY);
2017

21-
return parentEntityName ? `${parentEntityName}.${entityName}` : entityName;
18+
return path ? `${path}` : undefined;
2219
};

packages/traceloop-sdk/test/decorators.test.ts

+3-3
Original file line numberDiff line numberDiff line change
@@ -610,11 +610,11 @@ describe("Test SDK Decorators", () => {
610610
"joke_creation_task_wrapper",
611611
);
612612
assert.strictEqual(
613-
jokeCreationSpan.attributes[`${SpanAttributes.TRACELOOP_ENTITY_NAME}`],
614-
"joke_creation_task_wrapper.joke_creation",
613+
jokeCreationSpan.attributes[`${SpanAttributes.TRACELOOP_ENTITY_PATH}`],
614+
"joke_creation_task_wrapper",
615615
);
616616
assert.strictEqual(
617-
openAiChatSpans.attributes[`${SpanAttributes.TRACELOOP_ENTITY_NAME}`],
617+
openAiChatSpans.attributes[`${SpanAttributes.TRACELOOP_ENTITY_PATH}`],
618618
"joke_creation_task_wrapper.joke_creation",
619619
);
620620
assert.strictEqual(

0 commit comments

Comments
 (0)