Skip to content

Commit 1714a9f

Browse files
committed
refactor: improve OpenTelemetry instrumentation
1 parent 1b0b275 commit 1714a9f

File tree

6 files changed

+22
-24
lines changed

6 files changed

+22
-24
lines changed

.gitignore

+1-1
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
# custom
2-
/agentico-project
2+
/agentico-project**
33

44
# Logs
55
logs

src/index.ts

+1-1
Original file line numberDiff line numberDiff line change
@@ -216,7 +216,7 @@ async function createServer(directory: string, options: any = {}) {
216216
// Configuration based on user answers, or default values
217217
const config = {
218218
// Remove special characters from name
219-
name: (options.name || answers.name).replace(/[^a-zA-Z0-9_]/g, ''),
219+
name: lodash.kebabCase(options.name || answers.name).toLowerCase(),
220220
description: options.description || answers.description,
221221
tool: options.tool || answers.tool,
222222
installForClaude: options.installForClaude || answers.installForClaude,

template/instrumentation/scripts/agenticotel-config.yaml

+2-2
Original file line numberDiff line numberDiff line change
@@ -10,8 +10,8 @@ processors:
1010
timeout: 5s
1111

1212
exporters:
13-
# otlp: # Exports traces to Jaeger
14-
# endpoint: "http://host.docker.internal:14250"
13+
# otlp: # Exports traces to Jaeger - in case you want to use Jaeger at the same time
14+
# endpoint: "http://jaeger:14250"
1515
# tls:
1616
# insecure: true
1717
otlphttp:

template/project/package.json.ejs

+1-1
Original file line numberDiff line numberDiff line change
@@ -23,7 +23,7 @@
2323
"build": "tsc && node -e \"require('fs').chmodSync('build/index.js', '755')\"",
2424
"prepare": "npm run build",
2525
"watch": "tsc --watch",
26-
"inspector": "npx @modelcontextprotocol/inspector build/index.js"
26+
"inspector": "npx @modelcontextprotocol/inspector@0.3.0 build/index.js"
2727
},
2828
"dependencies": {
2929
"@agentico/mcp-server": "^0.3.0",

template/project/src/AgenticoTool.ts.ejs

+10-14
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,6 @@
11
import { z } from "zod";
22
import { zodToJsonSchema } from 'zod-to-json-schema';
33
import { Tool } from "@agentico/mcp-server";
4-
<% if (instrumentation) { %>import opentelemetry from '@opentelemetry/api';<% } %>
54

65
// @todo Adapt the schema and input type to match the tool's input you want to create
76
const <%= toolPascalCase %>Schema = z.object({
@@ -22,34 +21,31 @@ export class <%= toolPascalCase %>Tool extends Tool {
2221
// @todo - Implement any initialization logic here
2322
// console.error("<%= toolPascalCase %>Tool initialized");
2423
}
25-
execute(input: <%= toolPascalCase %>Input): Promise<any> {
24+
async execute(input: <%= toolPascalCase %>Input): Promise<any> {
2625
// @todo - Implement the tool logic here - this is where the tool does its work
2726
<% if (instrumentation) { %>
2827
// OpenTelemetry instrumentation example - @todo - adapt to your needs
29-
const tracer = opentelemetry.trace.getTracer('Agentico-tracer');
30-
const message = input.message.length > 20 ? `${input.message.substring(0, 20)}...` : input.message;
31-
const rootSpan = tracer.startSpan(`<%= toolPascalCase %>Tool Execution - ${message}`);
28+
this.otel.startSpan(`Tool Execution - ${input.message}`);
3229
3330
try {
3431
// Start another span. In this example, the main method already started a
35-
// span, so that'll be the parent span, and this will be a child span.
36-
const ctx = opentelemetry.trace.setSpan(opentelemetry.context.active(), rootSpan);
3732
// simulate 3 steps
38-
const step1 = tracer.startSpan('<%= toolPascalCase %> Execution Step 1', undefined, ctx);
39-
rootSpan.addEvent('<%= toolPascalCase %> Execution Started - Step 1');
33+
const step1 = this.otel.startSpan('Execution Step 1');
34+
this.otel.addEvent('Execution Started - Step 1', step1);
4035
await new Promise((r) => setTimeout(r, 700));
4136
step1.end();
42-
rootSpan.addEvent('<%= toolPascalCase %> Execution Ended - Step 1');
43-
const step2 = tracer.startSpan('<%= toolPascalCase %> Execution Step 2', undefined, ctx);
37+
// unporpose, no span for this event - see the result in the trace ;)
38+
this.otel.addEvent('Execution Ended - Step 1');
39+
const step2 = this.otel.startSpan('Execution Step 2');
4440
await new Promise((r) => setTimeout(r, 1000));
4541
step2.end();
46-
const step3 = tracer.startSpan('<%= toolPascalCase %> Execution Step 3', undefined, ctx);
42+
const step3 = this.otel.startSpan('Execution Step 3');
4743
await new Promise((r) => setTimeout(r, 800));
4844
step3.end();
4945
} catch (error) {
50-
console.error('Automation failed:', error);
46+
this.otel.addErrorEvent(error as Error);
5147
} finally {
52-
rootSpan.end();
48+
this.otel.endSpan();
5349
}
5450
<% } %>
5551
// echoing, just return the input message

template/project/src/index.ts.ejs

+7-5
Original file line numberDiff line numberDiff line change
@@ -11,15 +11,17 @@ import { <%= toolPascalCase %>Tool } from "./AgenticoTool.js";
1111
/**
1212
* Create an MCP server with capabilities for an echo tool.
1313
*/
14-
const myServer = new MCPServer("<%= name %>", "0.1.0");
14+
const myServer = new MCPServer(
15+
"<%= name %>",
16+
"0.1.0",
17+
<% if (instrumentation) { -%>
18+
"<%= traceExporterUrl %>"
19+
<% } -%>
20+
);
1521

1622
async function main() {
1723
// Register tools
1824
myServer.registerTool("<%= tool %>", new <%= toolPascalCase %>Tool());
19-
<% if (instrumentation) { %>
20-
// Initialize OpenTelemetry
21-
otlpSDK.start();
22-
<% } -%>
2325
await myServer.run();
2426
}
2527

0 commit comments

Comments
 (0)