Skip to content

Commit

Permalink
feat: pr review
Browse files Browse the repository at this point in the history
Signed-off-by: GALLLASMILAN <[email protected]>
  • Loading branch information
GALLLASMILAN authored and Milan Gallas committed Jan 23, 2025
1 parent 11826aa commit d20938b
Show file tree
Hide file tree
Showing 16 changed files with 84 additions and 64 deletions.
1 change: 1 addition & 0 deletions .env.test
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
BEE_FRAMEWORK_INSTRUMENTATION_METRICS_ENABLED=false
2 changes: 1 addition & 1 deletion README.md
Original file line number Diff line number Diff line change
Expand Up @@ -213,7 +213,7 @@ All content in these repositories including code has been provided by IBM under

## Telemetry

Some metrics are collected by default. See the [Native Telemetry](./docs/native-telemetry.md) for more detail.
Some metrics are collected by default. See the [Native Telemetry](/docs/native-telemetry.md) for more detail.

## Contributors

Expand Down
4 changes: 4 additions & 0 deletions docs/README.md
Original file line number Diff line number Diff line change
Expand Up @@ -211,6 +211,10 @@ This project and everyone participating in it are governed by the [Code of Condu

All content in these repositories including code has been provided by IBM under the associated open source software license and IBM is under no obligation to provide enhancements, updates, or support. IBM developers produced this code as an open source project (not as an IBM product), and IBM makes no assertions as to the level of quality nor security, and will not be maintaining this code going forward.

## Telemetry

Some metrics are collected by default. See the [Native Telemetry](/docs/native-telemetry.md) for more detail.

## Contributors

Special thanks to our contributors for helping us improve Bee Agent Framework.
Expand Down
4 changes: 2 additions & 2 deletions docs/instrumentation.md
Original file line number Diff line number Diff line change
Expand Up @@ -34,9 +34,9 @@ You can manually create spans during the `run` process to track specific parts o
Example of creating a span:

```ts
import { trace } from "@opentelemetry/api";
import { api } from "@opentelemetry/sdk-node";

const tracer = trace.getTracer("bee-agent-framework");
const tracer = api.trace.getTracer("bee-agent-framework");

function exampleFunction() {
const span = tracer.startSpan("example-function-span");
Expand Down
2 changes: 1 addition & 1 deletion docs/native-telemetry.md
Original file line number Diff line number Diff line change
Expand Up @@ -22,5 +22,5 @@ We value your privacy and ensure that **no sensitive data** is collected through
We understand that not all users want to send telemetry data. You can easily disable this feature by setting an environment variable:

```bash
BEE_FRAMEWORK_INSTRUMENTATION_METRICS_DISABLED=true
BEE_FRAMEWORK_INSTRUMENTATION_METRICS_ENABLED=false
```
5 changes: 2 additions & 3 deletions examples/helpers/telemetry.js
Original file line number Diff line number Diff line change
@@ -1,12 +1,11 @@
import "@opentelemetry/instrumentation/hook.mjs";
import { NodeSDK } from "@opentelemetry/sdk-node";
import { NodeSDK, resources } from "@opentelemetry/sdk-node";
import { ConsoleSpanExporter } from "@opentelemetry/sdk-trace-node";
import { Resource } from "@opentelemetry/resources";
import { ATTR_SERVICE_NAME, ATTR_SERVICE_VERSION } from "@opentelemetry/semantic-conventions";
import { Version } from "bee-agent-framework/version";

const sdk = new NodeSDK({
resource: new Resource({
resource: new resources.Resource({
[ATTR_SERVICE_NAME]: "bee-agent-framework",
[ATTR_SERVICE_VERSION]: Version,
}),
Expand Down
5 changes: 0 additions & 5 deletions examples/llms/instrumentation.ts
Original file line number Diff line number Diff line change
Expand Up @@ -20,8 +20,3 @@ const response = await llm.generate([
]);

logger.info(`LLM 🤖 (txt) : ${response.getTextContent()}`);

// Wait briefly to ensure all telemetry data has been processed
setTimeout(() => {
logger.info("Process exiting after OpenTelemetry flush.");
}, 5_000); // Adjust the delay as needed
5 changes: 0 additions & 5 deletions examples/tools/instrumentation.ts
Original file line number Diff line number Diff line change
Expand Up @@ -14,8 +14,3 @@ const result = await tool.run({
end_date: "2024-10-10",
});
logger.info(`OpenMeteoTool 🤖 (txt) : ${result.getTextContent()}`);

// Wait briefly to ensure all telemetry data has been processed
setTimeout(() => {
logger.info("Process exiting after OpenTelemetry flush.");
}, 5_000); // Adjust the delay as needed
17 changes: 8 additions & 9 deletions package.json
Original file line number Diff line number Diff line change
Expand Up @@ -151,13 +151,13 @@
"lint:fix": "yarn eslint --fix",
"format": "yarn prettier --check .",
"format:fix": "yarn prettier --write .",
"test:unit": "BEE_FRAMEWORK_INSTRUMENTATION_METRICS_DISABLED=true vitest run src",
"test:unit:watch": "BEE_FRAMEWORK_INSTRUMENTATION_METRICS_DISABLED=true vitest run src",
"test:e2e": "BEE_FRAMEWORK_INSTRUMENTATION_METRICS_DISABLED=true vitest run tests/e2e",
"test:e2e:watch": "BEE_FRAMEWORK_INSTRUMENTATION_METRICS_DISABLED=true vitest watch tests/e2e",
"test:unit": "vitest run src",
"test:unit:watch": "vitest run src",
"test:e2e": "vitest run tests/e2e",
"test:e2e:watch": "vitest watch tests/e2e",
"test:all": "yarn test:unit && yarn test:e2e && yarn test:examples",
"test:examples": "BEE_FRAMEWORK_INSTRUMENTATION_METRICS_DISABLED=true vitest --config ./examples/vitest.examples.config.ts run tests/examples",
"test:examples:watch": "BEE_FRAMEWORK_INSTRUMENTATION_METRICS_DISABLED=true vitest --config ./examples/vitest.examples.config.ts watch tests/examples",
"test:examples": "vitest --config ./examples/vitest.examples.config.ts run tests/examples",
"test:examples:watch": "vitest --config ./examples/vitest.examples.config.ts watch tests/examples",
"prepare": "husky",
"copyright": "./scripts/copyright.sh",
"copyright:check": "TYPE=check ./scripts/copyright.sh",
Expand All @@ -167,10 +167,9 @@
},
"dependencies": {
"@ai-zen/node-fetch-event-source": "^2.1.4",
"@opentelemetry/api": "^1.9.0",
"@connectrpc/connect": "^1.6.1",
"@connectrpc/connect-node": "^1.6.1",
"@opentelemetry/exporter-metrics-otlp-http": "^0.54.2",
"@opentelemetry/resources": "^1.27.0",
"@opentelemetry/sdk-metrics": "^1.27.0",
"@opentelemetry/sdk-node": "^0.54.2",
"@opentelemetry/semantic-conventions": "^1.27.0",
"@streamparser/json": "^0.0.21",
Expand Down
5 changes: 3 additions & 2 deletions src/instrumentation/config.ts
Original file line number Diff line number Diff line change
Expand Up @@ -18,8 +18,9 @@ import { parseEnv } from "@/internals/env.js";
import { z } from "zod";

export const INSTRUMENTATION_ENABLED = parseEnv.asBoolean("BEE_FRAMEWORK_INSTRUMENTATION_ENABLED");
export const INSTRUMENTATION_METRICS_ENABLED = !parseEnv.asBoolean(
"BEE_FRAMEWORK_INSTRUMENTATION_METRICS_DISABLED",
export const INSTRUMENTATION_METRICS_ENABLED = parseEnv.asBoolean(
"BEE_FRAMEWORK_INSTRUMENTATION_METRICS_ENABLED",
true,
);

export const INSTRUMENTATION_IGNORED_KEYS = parseEnv(
Expand Down
5 changes: 4 additions & 1 deletion src/instrumentation/create-telemetry-metrics-middleware.ts
Original file line number Diff line number Diff line change
Expand Up @@ -53,7 +53,10 @@ export function createTelemetryMetricsMiddleware() {
// send metrics to the public collector
emitter.match(
(event) => event.path === `${basePath}.run.${finishEventName}`,
async () => await metricReader.forceFlush(),
async () => {
activeTracesMap.delete(traceId);
await metricReader.forceFlush();
},
);
};
}
6 changes: 3 additions & 3 deletions src/instrumentation/helpers/create-span.ts
Original file line number Diff line number Diff line change
Expand Up @@ -14,15 +14,15 @@
* limitations under the License.
*/

import { SpanStatusCode, TimeInput } from "@opentelemetry/api";
import { api } from "@opentelemetry/sdk-node";
import { FrameworkSpan } from "@/instrumentation/types.js";
import { isEmpty } from "remeda";

interface CreateSpanProps {
id: string;
name: string;
target: string;
startedAt: TimeInput;
startedAt: api.TimeInput;
ctx?: any;
data?: any;
error?: string;
Expand Down Expand Up @@ -51,7 +51,7 @@ export function createSpan({
},
parent_id: parent?.id,
status: {
code: error ? SpanStatusCode.ERROR : SpanStatusCode.OK,
code: error ? api.SpanStatusCode.ERROR : api.SpanStatusCode.OK,
message: error ? error : "",
},
start_time: startedAt,
Expand Down
12 changes: 6 additions & 6 deletions src/instrumentation/opentelemetry.ts
Original file line number Diff line number Diff line change
Expand Up @@ -15,7 +15,7 @@
*/

import { Version } from "@/version.js";
import opentelemetry, { SpanStatusCode, TimeInput } from "@opentelemetry/api";
import { api } from "@opentelemetry/sdk-node";
import { FrameworkSpan, GeneratedResponse } from "./types.js";
import { BaseAgent } from "@/agents/base.js";
import { Tool } from "@/tools/base.js";
Expand All @@ -24,8 +24,8 @@ import os from "os";

const name = "bee-agent-framework";

export const meter = opentelemetry.metrics.getMeter(name, Version);
export const tracer = opentelemetry.trace.getTracer(name, Version);
export const meter = api.metrics.getMeter(name, Version);
export const tracer = api.trace.getTracer(name, Version);
const moduleUsageGauge = meter.createGauge("module_usage");

interface ComputeTreeProps {
Expand All @@ -36,8 +36,8 @@ interface ComputeTreeProps {
traceId: string;
version: string;
runErrorSpanKey: string;
startTime: TimeInput;
endTime: TimeInput;
startTime: api.TimeInput;
endTime: api.TimeInput;
source: string;
}

Expand Down Expand Up @@ -116,7 +116,7 @@ export function buildTraceTree({
if (runErrorSpan) {
activeSpan.setStatus(runErrorSpan.status);
} else {
activeSpan.setStatus({ code: SpanStatusCode.OK });
activeSpan.setStatus({ code: api.SpanStatusCode.OK });
}

// set nested spans
Expand Down
8 changes: 3 additions & 5 deletions src/instrumentation/sdk.ts
Original file line number Diff line number Diff line change
Expand Up @@ -16,9 +16,7 @@

import { Version } from "@/version.js";
import { OTLPMetricExporter } from "@opentelemetry/exporter-metrics-otlp-http";
import { Resource } from "@opentelemetry/resources";
import { PeriodicExportingMetricReader } from "@opentelemetry/sdk-metrics";
import { NodeSDK } from "@opentelemetry/sdk-node";
import { NodeSDK, metrics, resources } from "@opentelemetry/sdk-node";
import { ATTR_SERVICE_NAME, ATTR_SERVICE_VERSION } from "@opentelemetry/semantic-conventions";
import { INSTRUMENTATION_METRICS_ENABLED } from "./config.js";

Expand All @@ -28,12 +26,12 @@ const metricExporter = new OTLPMetricExporter({
url: "https://bee-collector.apps.fmaas-backend.fmaas.res.ibm.com/v1/metrics",
});

export const metricReader = new PeriodicExportingMetricReader({
export const metricReader = new metrics.PeriodicExportingMetricReader({
exporter: metricExporter,
});

export const sdk = new NodeSDK({
resource: new Resource({
resource: new resources.Resource({
[ATTR_SERVICE_NAME]: "bee-agent-framework",
[ATTR_SERVICE_VERSION]: Version,
}),
Expand Down
12 changes: 6 additions & 6 deletions src/instrumentation/types.ts
Original file line number Diff line number Diff line change
Expand Up @@ -14,22 +14,22 @@
* limitations under the License.
*/

import { Attributes, SpanStatus, TimeInput } from "@opentelemetry/api";
import { api } from "@opentelemetry/sdk-node";

export interface FrameworkSpan {
attributes: {
ctx?: Attributes | null;
data?: Attributes | null;
ctx?: api.Attributes | null;
data?: api.Attributes | null;
target: string;
};
context: {
span_id: string;
};
end_time: TimeInput;
end_time: api.TimeInput;
name: string;
parent_id?: string;
start_time: TimeInput;
status: SpanStatus;
start_time: api.TimeInput;
status: api.SpanStatus;
}

export interface GeneratedResponse {
Expand Down
55 changes: 40 additions & 15 deletions yarn.lock
Original file line number Diff line number Diff line change
Expand Up @@ -733,6 +733,27 @@ __metadata:
languageName: node
linkType: hard

"@connectrpc/connect-node@npm:^1.6.1":
version: 1.6.1
resolution: "@connectrpc/connect-node@npm:1.6.1"
dependencies:
undici: "npm:^5.28.4"
peerDependencies:
"@bufbuild/protobuf": ^1.10.0
"@connectrpc/connect": 1.6.1
checksum: 10c0/9891bbbe5ec155d16141e378c120dd6d4c47e1517656d4676aca762d70426a9eb3d9ec92595a7cfc4f5cbe40ff5be572d0c3d9010058107854e7f62ee05fb46e
languageName: node
linkType: hard

"@connectrpc/connect@npm:^1.6.1":
version: 1.6.1
resolution: "@connectrpc/connect@npm:1.6.1"
peerDependencies:
"@bufbuild/protobuf": ^1.10.0
checksum: 10c0/35c6fd3e33c3a1ff9dce230b059ecd7991ef0dc60c16fb898e5c46b930a01077ac0b34d53d6742cc8ed079f20f8eacc7c77a8620aeec9efaf68950494f387011
languageName: node
linkType: hard

"@conventional-changelog/git-client@npm:^1.0.0":
version: 1.0.1
resolution: "@conventional-changelog/git-client@npm:1.0.1"
Expand Down Expand Up @@ -1383,6 +1404,13 @@ __metadata:
languageName: node
linkType: hard

"@fastify/busboy@npm:^2.0.0":
version: 2.1.1
resolution: "@fastify/busboy@npm:2.1.1"
checksum: 10c0/6f8027a8cba7f8f7b736718b013f5a38c0476eea67034c94a0d3c375e2b114366ad4419e6a6fa7ffc2ef9c6d3e0435d76dd584a7a1cbac23962fda7650b579e3
languageName: node
linkType: hard

"@gar/promisify@npm:^1.0.1":
version: 1.1.3
resolution: "@gar/promisify@npm:1.1.3"
Expand Down Expand Up @@ -2302,7 +2330,7 @@ __metadata:
languageName: node
linkType: hard

"@opentelemetry/api@npm:1.x, @opentelemetry/api@npm:^1.3.0, @opentelemetry/api@npm:^1.9.0":
"@opentelemetry/api@npm:1.x, @opentelemetry/api@npm:^1.3.0":
version: 1.9.0
resolution: "@opentelemetry/api@npm:1.9.0"
checksum: 10c0/9aae2fe6e8a3a3eeb6c1fdef78e1939cf05a0f37f8a4fae4d6bf2e09eb1e06f966ece85805626e01ba5fab48072b94f19b835449e58b6d26720ee19a58298add
Expand Down Expand Up @@ -2700,18 +2728,6 @@ __metadata:
languageName: node
linkType: hard

"@opentelemetry/sdk-metrics@npm:^1.27.0":
version: 1.30.1
resolution: "@opentelemetry/sdk-metrics@npm:1.30.1"
dependencies:
"@opentelemetry/core": "npm:1.30.1"
"@opentelemetry/resources": "npm:1.30.1"
peerDependencies:
"@opentelemetry/api": ">=1.3.0 <1.10.0"
checksum: 10c0/7e60178e61eaf49db5d74f6c3701706762d71d370044253c72bb5668dba3a435030ed6847605ee55d0e1b8908ad123a2517b5f00415a2fb3d98468a0a318e3c0
languageName: node
linkType: hard

"@opentelemetry/sdk-node@npm:^0.56.0":
version: 0.56.0
resolution: "@opentelemetry/sdk-node@npm:0.56.0"
Expand Down Expand Up @@ -4799,6 +4815,8 @@ __metadata:
"@aws-sdk/client-bedrock-runtime": "npm:^3.706.0"
"@commitlint/cli": "npm:^19.6.0"
"@commitlint/config-conventional": "npm:^19.6.0"
"@connectrpc/connect": "npm:^1.6.1"
"@connectrpc/connect-node": "npm:^1.6.1"
"@elastic/elasticsearch": "npm:^8.16.2"
"@eslint/js": "npm:^9.16.0"
"@eslint/markdown": "npm:^6.2.1"
Expand All @@ -4812,11 +4830,9 @@ __metadata:
"@langchain/langgraph": "npm:^0.2.39"
"@langchain/ollama": "npm:^0.1.4"
"@modelcontextprotocol/sdk": "npm:^1.0.4"
"@opentelemetry/api": "npm:^1.9.0"
"@opentelemetry/exporter-metrics-otlp-http": "npm:^0.54.2"
"@opentelemetry/instrumentation": "npm:^0.56.0"
"@opentelemetry/resources": "npm:^1.29.0"
"@opentelemetry/sdk-metrics": "npm:^1.27.0"
"@opentelemetry/sdk-node": "npm:^0.56.0"
"@opentelemetry/sdk-trace-node": "npm:^1.29.0"
"@opentelemetry/semantic-conventions": "npm:^1.28.0"
Expand Down Expand Up @@ -13649,6 +13665,15 @@ __metadata:
languageName: node
linkType: hard

"undici@npm:^5.28.4":
version: 5.28.5
resolution: "undici@npm:5.28.5"
dependencies:
"@fastify/busboy": "npm:^2.0.0"
checksum: 10c0/4dfaa13089fe4c0758f84ec0d34b257e58608e6be3aa540f493b9864b39e3fdcd0a1ace38e434fe79db55f833aa30bcfddd8d6cbe3e0982b0dcae8ec17b65e08
languageName: node
linkType: hard

"undici@npm:^6.21.1":
version: 6.21.1
resolution: "undici@npm:6.21.1"
Expand Down

0 comments on commit d20938b

Please sign in to comment.