Skip to content

Commit d6ccf0d

Browse files
authored
fix(sdk): option to suppress instrumentations (#392)
1 parent 6b8967e commit d6ccf0d

File tree

3 files changed

+270
-0
lines changed

3 files changed

+270
-0
lines changed
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,236 @@
1+
{
2+
"log": {
3+
"_recordingName": "Test SDK Decorators/should not create spans if suppressed",
4+
"creator": {
5+
"comment": "persister:fs",
6+
"name": "Polly.JS",
7+
"version": "6.0.6"
8+
},
9+
"entries": [
10+
{
11+
"_id": "80b48c70cae76a3c38b9af59d5273e33",
12+
"_order": 0,
13+
"cache": {},
14+
"request": {
15+
"bodySize": 139,
16+
"cookies": [],
17+
"headers": [
18+
{
19+
"_fromType": "array",
20+
"name": "content-length",
21+
"value": "139"
22+
},
23+
{
24+
"_fromType": "array",
25+
"name": "accept",
26+
"value": "application/json"
27+
},
28+
{
29+
"_fromType": "array",
30+
"name": "content-type",
31+
"value": "application/json"
32+
},
33+
{
34+
"_fromType": "array",
35+
"name": "user-agent",
36+
"value": "OpenAI/JS 4.51.0"
37+
},
38+
{
39+
"_fromType": "array",
40+
"name": "x-stainless-lang",
41+
"value": "js"
42+
},
43+
{
44+
"_fromType": "array",
45+
"name": "x-stainless-package-version",
46+
"value": "4.51.0"
47+
},
48+
{
49+
"_fromType": "array",
50+
"name": "x-stainless-os",
51+
"value": "MacOS"
52+
},
53+
{
54+
"_fromType": "array",
55+
"name": "x-stainless-arch",
56+
"value": "arm64"
57+
},
58+
{
59+
"_fromType": "array",
60+
"name": "x-stainless-runtime",
61+
"value": "node"
62+
},
63+
{
64+
"_fromType": "array",
65+
"name": "x-stainless-runtime-version",
66+
"value": "v20.11.1"
67+
},
68+
{
69+
"_fromType": "array",
70+
"name": "accept-encoding",
71+
"value": "gzip,deflate"
72+
},
73+
{
74+
"name": "host",
75+
"value": "api.openai.com"
76+
}
77+
],
78+
"headersSize": 470,
79+
"httpVersion": "HTTP/1.1",
80+
"method": "POST",
81+
"postData": {
82+
"mimeType": "application/json",
83+
"params": [],
84+
"text": "{\n \"messages\": [\n {\n \"role\": \"user\",\n \"content\": \"Tell me a joke about OpenTelemetry\"\n }\n ],\n \"model\": \"gpt-3.5-turbo\"\n}"
85+
},
86+
"queryString": [],
87+
"url": "https://api.openai.com/v1/chat/completions"
88+
},
89+
"response": {
90+
"bodySize": 499,
91+
"content": {
92+
"encoding": "base64",
93+
"mimeType": "application/json",
94+
"size": 499,
95+
"text": "[\"H4sIAAAAAAAAA1RRu04DMRDs7ysW1wnKgwBJg4SEQgECJBAFQpHPt7kzsb3G3os4onR8BB2/yCcgX16icTGzs56ZXWUAQhdiAkJVkpX1pjt+L06Nmt7kT+dLOZ5e3T/fTvXiQZWf17YWnaSg/A0V71THiqw3yJrchlYBJWPa2j8bDIbD3kl/1BKWCjRJVnruDo9HXa5DTt1efzDaKivSCqOYwEsGALBq3+TRFfghJtDr7BCLMcoSxWQ/BCACmYQIGaOOLB2LzoFU5Bhd\",\"a/u5aqDQBXCFcOfRPaJBixwaKHCJhjwGKAnyQAu8gEtUso6YphtYoGeI0nqjXQlzCuDRuQY4SIXxCH5/vr/E9tP13q2h0gfKUzJXG7PH59rpWM0CykguOYtMfiNfZwCvbSv1v6DCB7KeZ0wLdGnhptw24O4OB3KwbUwwsTQHfDjKtv5EbCKjnc21KzH4oNuKkstsnf0BAAD//wMAi42JOCECAAA=\"]"
96+
},
97+
"cookies": [
98+
{
99+
"domain": ".api.openai.com",
100+
"expires": "2024-07-30T09:36:56.000Z",
101+
"httpOnly": true,
102+
"name": "__cf_bm",
103+
"path": "/",
104+
"sameSite": "None",
105+
"secure": true,
106+
"value": "7yfFgD11PHUHXiuI.j5WKR4_vB0i337vFTXLhvTQohI-1722330416-1.0.1.1-NTSJx4MCTIYtOR4IvAlljVC7bdqfNb4R70mABe1KfYWEl03Gonwq.Dn92GhbZ1TMlc72828a.4kIgg5D.aoklA"
107+
},
108+
{
109+
"domain": ".api.openai.com",
110+
"httpOnly": true,
111+
"name": "_cfuvid",
112+
"path": "/",
113+
"sameSite": "None",
114+
"secure": true,
115+
"value": "HCtNHwBc2sAt7qjjA6_R7IaoZTYB1AQbWWe80TsuyFk-1722330416349-0.0.1.1-604800000"
116+
}
117+
],
118+
"headers": [
119+
{
120+
"name": "date",
121+
"value": "Tue, 30 Jul 2024 09:06:56 GMT"
122+
},
123+
{
124+
"name": "content-type",
125+
"value": "application/json"
126+
},
127+
{
128+
"name": "transfer-encoding",
129+
"value": "chunked"
130+
},
131+
{
132+
"name": "connection",
133+
"value": "keep-alive"
134+
},
135+
{
136+
"name": "openai-organization",
137+
"value": "traceloop"
138+
},
139+
{
140+
"name": "openai-processing-ms",
141+
"value": "397"
142+
},
143+
{
144+
"name": "openai-version",
145+
"value": "2020-10-01"
146+
},
147+
{
148+
"name": "strict-transport-security",
149+
"value": "max-age=15552000; includeSubDomains; preload"
150+
},
151+
{
152+
"name": "x-ratelimit-limit-requests",
153+
"value": "5000"
154+
},
155+
{
156+
"name": "x-ratelimit-limit-tokens",
157+
"value": "4000000"
158+
},
159+
{
160+
"name": "x-ratelimit-remaining-requests",
161+
"value": "4999"
162+
},
163+
{
164+
"name": "x-ratelimit-remaining-tokens",
165+
"value": "3999973"
166+
},
167+
{
168+
"name": "x-ratelimit-reset-requests",
169+
"value": "12ms"
170+
},
171+
{
172+
"name": "x-ratelimit-reset-tokens",
173+
"value": "0s"
174+
},
175+
{
176+
"name": "x-request-id",
177+
"value": "req_2beb9889740c816c4027aff5e1228f29"
178+
},
179+
{
180+
"name": "cf-cache-status",
181+
"value": "DYNAMIC"
182+
},
183+
{
184+
"_fromType": "array",
185+
"name": "set-cookie",
186+
"value": "__cf_bm=7yfFgD11PHUHXiuI.j5WKR4_vB0i337vFTXLhvTQohI-1722330416-1.0.1.1-NTSJx4MCTIYtOR4IvAlljVC7bdqfNb4R70mABe1KfYWEl03Gonwq.Dn92GhbZ1TMlc72828a.4kIgg5D.aoklA; path=/; expires=Tue, 30-Jul-24 09:36:56 GMT; domain=.api.openai.com; HttpOnly; Secure; SameSite=None"
187+
},
188+
{
189+
"_fromType": "array",
190+
"name": "set-cookie",
191+
"value": "_cfuvid=HCtNHwBc2sAt7qjjA6_R7IaoZTYB1AQbWWe80TsuyFk-1722330416349-0.0.1.1-604800000; path=/; domain=.api.openai.com; HttpOnly; Secure; SameSite=None"
192+
},
193+
{
194+
"name": "x-content-type-options",
195+
"value": "nosniff"
196+
},
197+
{
198+
"name": "server",
199+
"value": "cloudflare"
200+
},
201+
{
202+
"name": "cf-ray",
203+
"value": "8ab43208fe3609bb-HFA"
204+
},
205+
{
206+
"name": "content-encoding",
207+
"value": "gzip"
208+
},
209+
{
210+
"name": "alt-svc",
211+
"value": "h3=\":443\"; ma=86400"
212+
}
213+
],
214+
"headersSize": 1143,
215+
"httpVersion": "HTTP/1.1",
216+
"redirectURL": "",
217+
"status": 200,
218+
"statusText": "OK"
219+
},
220+
"startedDateTime": "2024-07-30T09:06:55.348Z",
221+
"time": 885,
222+
"timings": {
223+
"blocked": -1,
224+
"connect": -1,
225+
"dns": -1,
226+
"receive": 0,
227+
"send": 0,
228+
"ssl": -1,
229+
"wait": 885
230+
}
231+
}
232+
],
233+
"pages": [],
234+
"version": "1.2"
235+
}
236+
}

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

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,5 @@
11
import { Span, context } from "@opentelemetry/api";
2+
import { suppressTracing } from "@opentelemetry/core";
23
import {
34
ASSOCATION_PROPERTIES_KEY,
45
ENTITY_NAME_KEY,
@@ -20,6 +21,7 @@ export type DecoratorConfig = {
2021
associationProperties?: { [name: string]: string };
2122
traceContent?: boolean;
2223
inputParameters?: unknown[];
24+
suppressTracing?: boolean;
2325
};
2426

2527
function withEntity<
@@ -33,6 +35,7 @@ function withEntity<
3335
associationProperties,
3436
traceContent: overrideTraceContent,
3537
inputParameters,
38+
suppressTracing: shouldSuppressTracing,
3639
}: DecoratorConfig,
3740
fn: F,
3841
thisArg?: ThisParameterType<F>,
@@ -68,6 +71,10 @@ function withEntity<
6871
);
6972
}
7073

74+
if (shouldSuppressTracing) {
75+
entityContext = suppressTracing(entityContext);
76+
}
77+
7178
return context.with(entityContext, () =>
7279
getTracer().startActiveSpan(
7380
`${name}.${type}`,

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

Lines changed: 27 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -145,6 +145,33 @@ describe("Test SDK Decorators", () => {
145145
);
146146
});
147147

148+
it("should not create spans if suppressed", async () => {
149+
const jokeSubject = "OpenTelemetry";
150+
const result = await traceloop.withWorkflow(
151+
{
152+
name: "sample_chat",
153+
associationProperties: { userId: "123" },
154+
suppressTracing: true,
155+
},
156+
async () => {
157+
const chatCompletion = await openai.chat.completions.create({
158+
messages: [
159+
{ role: "user", content: `Tell me a joke about ${jokeSubject}` },
160+
],
161+
model: "gpt-3.5-turbo",
162+
});
163+
164+
return chatCompletion.choices[0].message.content;
165+
},
166+
{ jokeSubject },
167+
);
168+
169+
assert.ok(result);
170+
171+
const spans = memoryExporter.getFinishedSpans();
172+
assert.strictEqual(spans.length, 0);
173+
});
174+
148175
it("should create spans for workflows using decoration syntax", async () => {
149176
class TestOpenAI {
150177
@traceloop.workflow({ name: "sample_chat", version: 2 })

0 commit comments

Comments
 (0)