Skip to content

Commit

Permalink
feat: filter nextjs (#20)
Browse files Browse the repository at this point in the history
  • Loading branch information
tomer-friedman authored Dec 7, 2023
1 parent 8af9342 commit 5de607e
Show file tree
Hide file tree
Showing 4 changed files with 69 additions and 0 deletions.
1 change: 1 addition & 0 deletions packages/sample-app/package.json
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,7 @@
"run:with": "npm run build && node dist/src/sample_with.js",
"run:prompt_mgmt": "npm run build && node dist/src/sample_prompt_mgmt.js",
"run:sample_vision": "npm run build && node dist/src/sample_vision_prompt.js",
"run:sampler": "npm run build && node dist/src/sample_sampler.js",
"lint": "eslint . --ext .ts",
"lint:fix": "eslint . --ext .ts --fix"
},
Expand Down
30 changes: 30 additions & 0 deletions packages/sample-app/src/sample_sampler.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,30 @@
import * as traceloop from "@traceloop/node-server-sdk";
import { trace, Span, context } from "@opentelemetry/api";
import { ConsoleSpanExporter } from "@opentelemetry/sdk-trace-base";

trace.getTracer("traceloop.tracer");

// No spans should be printed to the console when this file runs (it should be filtered out by the sampler)

const main = async () => {
traceloop.initialize({
appName: "sample_sampler",
apiKey: process.env.TRACELOOP_API_KEY,
disableBatch: true,
traceloopSyncEnabled: false,
exporter: new ConsoleSpanExporter(),
});

trace
.getTracer("traceloop.tracer")
.startActiveSpan(
"test-span",
{ attributes: { "next.span_name": "anything" } },
context.active(),
async (span: Span) => {
span.end();
},
);
};

main();
2 changes: 2 additions & 0 deletions packages/traceloop-sdk/src/lib/tracing/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -13,6 +13,7 @@ import { OpenAIInstrumentation } from "@traceloop/instrumentation-openai";
import { SpanAttributes } from "@traceloop/ai-semantic-conventions";
import { ASSOCATION_PROPERTIES_KEY, WORKFLOW_NAME_KEY } from "./tracing";
import { Telemetry } from "../telemetry/telemetry";
import { TraceloopSampler } from "./sampler";

let _sdk: NodeSDK;
let _spanProcessor: SimpleSpanProcessor | BatchSpanProcessor;
Expand Down Expand Up @@ -90,6 +91,7 @@ export const startTracing = (options: InitializeOptions) => {
spanProcessor: _spanProcessor,
traceExporter,
instrumentations,
sampler: new TraceloopSampler(),
});

_sdk.start();
Expand Down
36 changes: 36 additions & 0 deletions packages/traceloop-sdk/src/lib/tracing/sampler.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,36 @@
import {
Sampler,
SamplingResult,
SamplingDecision,
} from "@opentelemetry/sdk-trace-base";
import { Context, SpanKind, Attributes, Link } from "@opentelemetry/api";

const FILTERED_ATTRIBUTE_KEYS = ["next.span_name"];

export class TraceloopSampler implements Sampler {
shouldSample(
_context: Context,
_traceId: string,
_spanName: string,
_spanKind: SpanKind,
attributes: Attributes,
_links: Link[],
): SamplingResult {
let filter = false;
FILTERED_ATTRIBUTE_KEYS.forEach((key) => {
if (attributes?.[key]) {
filter = true;
}
});

return {
decision: filter
? SamplingDecision.NOT_RECORD
: SamplingDecision.RECORD_AND_SAMPLED,
};
}

toString(): string {
return "TraceloopSampler";
}
}

0 comments on commit 5de607e

Please sign in to comment.