-
Notifications
You must be signed in to change notification settings - Fork 142
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
feat(observe): implement OpenTelemetry instrumentation
Signed-off-by: GALLLASMILAN <[email protected]>
- Loading branch information
1 parent
1c6d3c1
commit 2cc1ef4
Showing
29 changed files
with
1,644 additions
and
13 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,98 @@ | ||
# OpenTelemetry Instrumentation in Bee-Agent-Framework | ||
|
||
This document provides an overview of the OpenTelemetry instrumentation setup in the Bee-Agent-Framework. | ||
The implementation is designed to [create telemetry spans](https://opentelemetry.io/docs/languages/js/instrumentation/#create-spans) for observability when instrumentation is enabled. | ||
|
||
## Overview | ||
|
||
OpenTelemetry instrumentation allows you to collect telemetry data, such as traces and metrics, to monitor the performance of your services. | ||
This setup involves creating middleware to handle instrumentation automatically when the `INSTRUMENTATION_ENABLED` flag is active. | ||
|
||
## Setting up OpenTelemetry | ||
|
||
Follow the official OpenTelemetry [Node.js Getting Started Guide](https://opentelemetry.io/docs/languages/js/getting-started/nodejs/) to initialize and configure OpenTelemetry in your application. | ||
|
||
## Instrumentation Configuration | ||
|
||
### Environment Variable | ||
|
||
Use the environment variable `BEE_FRAMEWORK_INSTRUMENTATION_ENABLED` to enable or disable instrumentation. | ||
|
||
```bash | ||
# Enable instrumentation | ||
export BEE_FRAMEWORK_INSTRUMENTATION_ENABLED=true | ||
# Ignore sensitive keys from collected events data | ||
export INSTRUMENTATION_IGNORED_KEYS="apiToken,accessToken" | ||
``` | ||
|
||
If `BEE_FRAMEWORK_INSTRUMENTATION_ENABLED` is false or unset, the framework will run without instrumentation. | ||
|
||
## Creating Custom Spans | ||
|
||
You can manually create spans during the `run` process to track specific parts of the execution. This is useful for adding custom telemetry to enhance observability. | ||
|
||
Example of creating a span: | ||
|
||
```ts | ||
import { trace } from "@opentelemetry/api"; | ||
|
||
const tracer = trace.getTracer("bee-agent-framework"); | ||
|
||
function exampleFunction() { | ||
const span = tracer.startSpan("example-function-span"); | ||
try { | ||
// Your code logic here | ||
} catch (error) { | ||
span.recordException(error); | ||
throw error; | ||
} finally { | ||
span.end(); | ||
} | ||
} | ||
``` | ||
|
||
## Verifying Instrumentation | ||
|
||
Once you have enabled the instrumentation, you can view telemetry data using any [compatible OpenTelemetry backend](https://opentelemetry.io/docs/languages/js/exporters/), such as [Jaeger](https://www.jaegertracing.io/), [Zipkin](https://zipkin.io/), [Prometheus](https://prometheus.io/docs/prometheus/latest/feature_flags/#otlp-receiver), etc... | ||
Ensure your OpenTelemetry setup is properly configured to export trace data to your chosen backend. | ||
|
||
## Run examples | ||
|
||
> the right version of node.js must be correctly set | ||
``` | ||
nvm use | ||
``` | ||
|
||
### Agent instrumentation | ||
|
||
Running the Instrumented Application (`examples/agents/bee_instrumentation.js`) file. | ||
|
||
```bash | ||
## the telemetry example is run on built js files | ||
yarn start:telemetry ./examples/agents/bee_instrumentation.ts | ||
``` | ||
|
||
### LLM instrumentation | ||
|
||
Running (`./examples/llms/instrumentation.js`) file. | ||
|
||
```bash | ||
## the telemetry example is run on built js files | ||
|
||
yarn start:telemetry ./examples/llms/instrumentation.ts | ||
``` | ||
|
||
### Tool instrumentation | ||
|
||
Running (`./examples/tools/instrumentation.js`) file. | ||
|
||
```bash | ||
## the telemetry example is run on built js files | ||
yarn start:telemetry ./examples/tools/instrumentation.ts | ||
``` | ||
|
||
## Conclusion | ||
|
||
This setup provides basic OpenTelemetry instrumentation with the flexibility to enable or disable it as needed. | ||
By creating custom spans and using `createTelemetryMiddleware`, you can capture detailed telemetry for better observability and performance insights. |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,46 @@ | ||
//////////////////////////////////////////////////////////////////////////////////////////////////////// | ||
/////// RUN THIS EXAMPLE VIA `yarn start:telemetry ./examples/agents/bee_instrumentation.ts` /////////// | ||
//////////////////////////////////////////////////////////////////////////////////////////////////////// | ||
|
||
import { BeeAgent } from "bee-agent-framework/agents/bee/agent"; | ||
import { FrameworkError } from "bee-agent-framework/errors"; | ||
import { TokenMemory } from "bee-agent-framework/memory/tokenMemory"; | ||
import { Logger } from "bee-agent-framework/logger/logger"; | ||
import { DuckDuckGoSearchTool } from "bee-agent-framework/tools/search/duckDuckGoSearch"; | ||
import { WikipediaTool } from "bee-agent-framework/tools/search/wikipedia"; | ||
import { OpenMeteoTool } from "bee-agent-framework/tools/weather/openMeteo"; | ||
import { OllamaChatLLM } from "bee-agent-framework/adapters/ollama/chat"; | ||
|
||
Logger.root.level = "silent"; // disable internal logs | ||
const logger = new Logger({ name: "app", level: "trace" }); | ||
|
||
const llm = new OllamaChatLLM({ | ||
modelId: "llama3.1", // llama3.1:70b for better performance | ||
}); | ||
|
||
const agent = new BeeAgent({ | ||
llm, | ||
memory: new TokenMemory({ llm }), | ||
tools: [ | ||
new DuckDuckGoSearchTool(), | ||
new WikipediaTool(), | ||
new OpenMeteoTool(), // weather tool | ||
], | ||
}); | ||
|
||
try { | ||
const response = await agent.run( | ||
{ prompt: "what is the weather like in Granada?" }, | ||
{ | ||
execution: { | ||
maxRetriesPerStep: 3, | ||
totalMaxRetries: 10, | ||
maxIterations: 20, | ||
}, | ||
}, | ||
); | ||
|
||
logger.info(`Agent 🤖 : ${response.result.text}`); | ||
} catch (error) { | ||
logger.error(FrameworkError.ensure(error).dump()); | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,15 @@ | ||
import "@opentelemetry/instrumentation/hook.mjs"; | ||
import { NodeSDK } 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"; | ||
|
||
const sdk = new NodeSDK({ | ||
resource: new Resource({ | ||
[ATTR_SERVICE_NAME]: "bee-agent-framework", | ||
[ATTR_SERVICE_VERSION]: "0.0.1", | ||
}), | ||
traceExporter: new ConsoleSpanExporter(), | ||
}); | ||
|
||
await sdk.start(); |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,27 @@ | ||
////////////////////////////////////////////////////////////////////////////////////////////////// | ||
/////// RUN THIS EXAMPLE VIA `yarn start:telemetry ./examples/llms/instrumentation.ts` /////////// | ||
////////////////////////////////////////////////////////////////////////////////////////////////// | ||
import { BaseMessage, Role } from "bee-agent-framework/llms/primitives/message"; | ||
import { OllamaChatLLM } from "bee-agent-framework/adapters/ollama/chat"; | ||
import { Logger } from "bee-agent-framework/logger/logger"; | ||
|
||
Logger.root.level = "silent"; // disable internal logs | ||
const logger = new Logger({ name: "app", level: "trace" }); | ||
|
||
const llm = new OllamaChatLLM({ | ||
modelId: "llama3.1", // llama3.1:70b for better performance | ||
}); | ||
|
||
const response = await llm.generate([ | ||
BaseMessage.of({ | ||
role: Role.USER, | ||
text: "hello", | ||
}), | ||
]); | ||
|
||
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 |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,21 @@ | ||
/////////////////////////////////////////////////////////////////////////////////////////////////// | ||
/////// RUN THIS EXAMPLE VIA `yarn start:telemetry ./examples/tools/instrumentation.ts` /////////// | ||
/////////////////////////////////////////////////////////////////////////////////////////////////// | ||
import { OpenMeteoTool } from "bee-agent-framework/tools/weather/openMeteo"; | ||
import { Logger } from "bee-agent-framework/logger/logger"; | ||
|
||
Logger.root.level = "silent"; // disable internal logs | ||
const logger = new Logger({ name: "app", level: "trace" }); | ||
|
||
const tool = new OpenMeteoTool(); | ||
const result = await tool.run({ | ||
location: { name: "New York" }, | ||
start_date: "2024-10-10", | ||
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 |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,28 @@ | ||
/** | ||
* Copyright 2024 IBM Corp. | ||
* | ||
* Licensed under the Apache License, Version 2.0 (the "License"); | ||
* you may not use this file except in compliance with the License. | ||
* You may obtain a copy of the License at | ||
* | ||
* http://www.apache.org/licenses/LICENSE-2.0 | ||
* | ||
* Unless required by applicable law or agreed to in writing, software | ||
* distributed under the License is distributed on an "AS IS" BASIS, | ||
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. | ||
* See the License for the specific language governing permissions and | ||
* limitations under the License. | ||
*/ | ||
|
||
import { parseEnv } from "@/internals/env.js"; | ||
import { z } from "zod"; | ||
|
||
export const INSTRUMENTATION_ENABLED = parseEnv.asBoolean("BEE_FRAMEWORK_INSTRUMENTATION_ENABLED"); | ||
|
||
export const INSTRUMENTATION_IGNORED_KEYS = parseEnv( | ||
"BEE_FRAMEWORK_INSTRUMENTATION_IGNORED_KEYS", | ||
z.string(), | ||
"", | ||
) | ||
.split(",") | ||
.filter(Boolean); |
Oops, something went wrong.