-
Notifications
You must be signed in to change notification settings - Fork 141
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
feat(instrumentation): add fw metrics #187
Draft
GALLLASMILAN
wants to merge
3
commits into
main
Choose a base branch
from
telemetry-framework-metrics
base: main
Could not load branches
Branch not found: {{ refName }}
Loading
Could not load tags
Nothing to show
Loading
Are you sure you want to change the base?
Some commits from the old base branch may be removed from the timeline,
and old review comments may become outdated.
Draft
Changes from 2 commits
Commits
Show all changes
3 commits
Select commit
Hold shift + click to select a range
File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1 @@ | ||
BEE_FRAMEWORK_INSTRUMENTATION_METRICS_ENABLED=false |
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,26 @@ | ||
# Native Telemetry in Bee Agent Framework | ||
|
||
The Bee Agent Framework comes with built-in telemetry capabilities to help users monitor and optimize their applications. This document provides an overview of the telemetry feature, including what data is collected and how to disable it if necessary. | ||
|
||
## Overview | ||
|
||
The telemetry functionality in the Bee Agent Framework collects performance metrics and operational data to provide insights into how the framework operates in real-world environments. This feature helps us: | ||
|
||
- Identify performance bottlenecks. | ||
- Improve framework stability and reliability. | ||
- Enhance user experience by understanding usage patterns. | ||
|
||
## Data Collected | ||
|
||
We value your privacy and ensure that **no sensitive data** is collected through telemetry. The following types of information are gathered: | ||
|
||
- Framework version and runtime environment details. | ||
- Anonymized usage statistics for built-in features. | ||
|
||
## Disabling Telemetry | ||
|
||
We understand that not all users want to send telemetry data. You can easily disable this feature by setting an environment variable: | ||
|
||
```bash | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. isn't there support for ```env ? There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. I haven't seen the ```env yet, I will remove the |
||
BEE_FRAMEWORK_INSTRUMENTATION_METRICS_ENABLED=false | ||
``` |
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
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
62 changes: 62 additions & 0 deletions
62
src/instrumentation/create-telemetry-metrics-middleware.ts
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,62 @@ | ||
/** | ||
* 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 { GetRunContext, RunInstance } from "@/context.js"; | ||
import { FrameworkError } from "@/errors.js"; | ||
import { buildModuleUsageMetric, isMeasurementedInstance } from "./opentelemetry.js"; | ||
import { GenerateCallbacks } from "@/llms/base.js"; | ||
import { createFullPath } from "@/emitter/utils.js"; | ||
import { metricReader } from "./sdk.js"; | ||
|
||
export const activeTracesMap = new Map<string, string>(); | ||
Tomas2D marked this conversation as resolved.
Show resolved
Hide resolved
|
||
|
||
/** | ||
* This middleware collects the usage metrics from framework entities runs and sends them to the central collector | ||
* @returns | ||
*/ | ||
export function createTelemetryMetricsMiddleware() { | ||
return (context: GetRunContext<RunInstance, unknown>) => { | ||
const traceId = context.emitter?.trace?.id; | ||
if (!traceId) { | ||
throw new FrameworkError(`Fatal error. Missing traceId`, [], { context }); | ||
} | ||
if (activeTracesMap.has(traceId)) { | ||
return; | ||
} | ||
activeTracesMap.set(traceId, context.instance.constructor.name); | ||
|
||
const { emitter } = context; | ||
const basePath = createFullPath(emitter.namespace, ""); | ||
|
||
const startEventName: keyof GenerateCallbacks = `start`; | ||
const finishEventName: keyof GenerateCallbacks = `finish`; | ||
|
||
// collect module_usage metric for llm|tool|agent start event | ||
emitter.match( | ||
(event) => event.name === startEventName && isMeasurementedInstance(event.creator), | ||
(_, meta) => buildModuleUsageMetric({ traceId, instance: meta.creator, eventId: meta.id }), | ||
); | ||
|
||
// send metrics to the public collector | ||
emitter.match( | ||
(event) => event.path === `${basePath}.run.${finishEventName}`, | ||
async () => { | ||
activeTracesMap.delete(traceId); | ||
await metricReader.forceFlush(); | ||
}, | ||
); | ||
}; | ||
} |
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,106 @@ | ||
/** | ||
* 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 { BeeAgent } from "@/agents/bee/agent.js"; | ||
import { ElasticSearchTool } from "@/tools/database/elasticsearch.js"; | ||
import { SQLTool } from "@/tools/database/sql.js"; | ||
import { GoogleSearchTool } from "@/tools/search/googleSearch.js"; | ||
import { OpenMeteoTool } from "@/tools/weather/openMeteo.js"; | ||
import { expect, describe } from "vitest"; | ||
import { isMeasurementedInstance } from "./opentelemetry.js"; | ||
import { DuckDuckGoSearchTool } from "@/tools/search/duckDuckGoSearch.js"; | ||
import { WebCrawlerTool } from "@/tools/web/webCrawler.js"; | ||
import { ArXivTool } from "@/tools/arxiv.js"; | ||
import { CalculatorTool } from "@/tools/calculator.js"; | ||
import { LLMTool } from "@/tools/llm.js"; | ||
import { OllamaLLM } from "@/adapters/ollama/llm.js"; | ||
import { WatsonXLLM } from "@/adapters/watsonx/llm.js"; | ||
import { LLM } from "@/llms/llm.js"; | ||
import { Emitter } from "@/emitter/emitter.js"; | ||
import { GenerateCallbacks, LLMMeta, BaseLLMTokenizeOutput, AsyncStream } from "@/llms/base.js"; | ||
import { OllamaChatLLM } from "@/adapters/ollama/chat.js"; | ||
import { TokenMemory } from "@/memory/tokenMemory.js"; | ||
import { GraniteBeeAgent } from "@/agents/granite/agent.js"; | ||
import { SlidingCache } from "@/cache/slidingCache.js"; | ||
|
||
export class CustomLLM extends LLM<any> { | ||
public readonly emitter = Emitter.root.child<GenerateCallbacks>({ | ||
namespace: ["bam", "llm"], | ||
creator: this, | ||
}); | ||
meta(): Promise<LLMMeta> { | ||
throw new Error("Method not implemented."); | ||
} | ||
tokenize(): Promise<BaseLLMTokenizeOutput> { | ||
throw new Error("Method not implemented."); | ||
} | ||
protected _generate(): Promise<any> { | ||
throw new Error("Method not implemented."); | ||
} | ||
protected _stream(): AsyncStream<any, void> { | ||
throw new Error("Method not implemented."); | ||
} | ||
} | ||
|
||
const llm = new OllamaChatLLM({ modelId: "llama3.1" }); | ||
const memory = new TokenMemory({ llm }); | ||
|
||
describe("opentelemetry", () => { | ||
describe("isMeasurementedInstance", () => { | ||
it.each([ | ||
// tool | ||
new OpenMeteoTool(), | ||
new GoogleSearchTool({ apiKey: "xx", cseId: "xx", maxResults: 10 }), | ||
new ElasticSearchTool({ connection: { cloud: { id: "" } } }), | ||
new SQLTool({ connection: { dialect: "mariadb" }, provider: "mysql" }), | ||
new DuckDuckGoSearchTool(), | ||
new OpenMeteoTool(), | ||
new WebCrawlerTool(), | ||
new ArXivTool(), | ||
new CalculatorTool(), | ||
new LLMTool({ llm: new OllamaLLM({ modelId: "llama3.1" }) }), | ||
// llm | ||
new OllamaLLM({ modelId: "llama3.1" }), | ||
new WatsonXLLM({ modelId: "llama3.1", apiKey: "xx" }), | ||
new CustomLLM("llama3.1"), | ||
new OllamaChatLLM({ modelId: "llama3.1" }), | ||
// agent | ||
new BeeAgent({ llm, memory, tools: [] }), | ||
new GraniteBeeAgent({ llm, memory, tools: [] }), | ||
])("Should return true for '%s'", (value) => { | ||
expect(isMeasurementedInstance(value)).toBeTruthy(); | ||
}); | ||
|
||
it.each([ | ||
null, | ||
undefined, | ||
"", | ||
0, | ||
"string", | ||
{}, | ||
memory, | ||
new SlidingCache({ | ||
size: 50, | ||
}), | ||
new Emitter({ | ||
namespace: ["app"], | ||
creator: this, | ||
}), | ||
])("Should return false for '%s'", (value) => { | ||
expect(isMeasurementedInstance(value)).toBeFalsy(); | ||
}); | ||
}); | ||
}); |
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Consider adding this document to the documentation sidebar. (
docs/_sidebar.md
)