-
Notifications
You must be signed in to change notification settings - Fork 139
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
feat(instrumentation): add fw metrics
Signed-off-by: GALLLASMILAN <[email protected]>
- Loading branch information
1 parent
d7c4060
commit 89064aa
Showing
14 changed files
with
458 additions
and
98 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
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 | ||
BEE_FRAMEWORK_INSTRUMENTATION_METRICS_DISABLED=true | ||
``` |
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
59 changes: 59 additions & 0 deletions
59
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,59 @@ | ||
/** | ||
* 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>(); | ||
|
||
/** | ||
* 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 () => 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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,108 @@ | ||
/** | ||
* 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 { BAMLLM } from "@/adapters/bam/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 BAMLLM({ 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(); | ||
}); | ||
}); | ||
}); |
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
Oops, something went wrong.