generated from teamhide/fastapi-boilerplate
-
Notifications
You must be signed in to change notification settings - Fork 11
chore: Metrics , tracks commands , errors for now #81
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
Merged
Merged
Changes from 4 commits
Commits
Show all changes
20 commits
Select commit
Hold shift + click to select a range
a8617f8
chore: starting metrics work
vinikatyal eca7af2
chore: analytics tracking
vinikatyal 62f2666
chore: remove not needed code
vinikatyal 9f2238c
chore: update req.txt
vinikatyal 4ebf2a0
Merge branch 'main' into vk/metrics
vinikatyal 87ba0d8
Merge branch 'main' into vk/metrics
vinikatyal 731adae
Merge branch 'main' into vk/metrics
vinikatyal 5c23e38
chore: connect to azure
vinikatyal f783c85
chore: remove screenshot for now
vinikatyal da90219
Merge branch 'main' into vk/metrics
vinikatyal 7b3b6c8
chore: update the tracker code
vinikatyal 371ec35
chore: track dashboard events
vinikatyal d727a2a
chore: remove cli_command
vinikatyal 63c06d5
chore: add
vinikatyal 1340fb4
chore: remove image
vinikatyal 584ec2c
chore: moved to settings
vinikatyal a303977
chore: remove logging
vinikatyal d2c1171
chore: hard coded values
vinikatyal bbb9029
chore: remove nist
vinikatyal 262b362
chore: remove print
vinikatyal 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 hidden or 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 hidden or 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 hidden or 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,110 @@ | ||
| import os | ||
| import time | ||
| from opentelemetry.sdk.metrics import MeterProvider | ||
| from opentelemetry.metrics import set_meter_provider, get_meter_provider | ||
| from opentelemetry.sdk.metrics.export import PeriodicExportingMetricReader, ConsoleMetricExporter | ||
| from opentelemetry.exporter.otlp.proto.grpc.metric_exporter import OTLPMetricExporter | ||
| from opentelemetry.exporter.prometheus import PrometheusMetricReader | ||
| from prometheus_client import start_http_server | ||
|
|
||
| # Start Prometheus scrape server (default on port 9090) | ||
| # start_http_server(port=9090) | ||
|
|
||
| # Initialize OpenTelemetry metrics | ||
| meter_provider = MeterProvider( | ||
| metric_readers=[ | ||
| # Console for local debugging | ||
| PeriodicExportingMetricReader(ConsoleMetricExporter()), | ||
|
|
||
| # # OTLP Exporter for production | ||
| PeriodicExportingMetricReader( | ||
| OTLPMetricExporter( | ||
| endpoint=os.getenv("OTEL_EXPORTER_OTLP_ENDPOINT", "http://localhost:4317") | ||
| ) | ||
| ), | ||
| # PrometheusMetricReader() | ||
| ] | ||
| ) | ||
|
|
||
| set_meter_provider(meter_provider) | ||
| meter = get_meter_provider().get_meter("compliant-llm") | ||
|
|
||
| class AnalyticsTracker: | ||
| """Class for tracking analytics and metrics.""" | ||
|
|
||
| def __init__(self): | ||
| self.total_tests = meter.create_counter( | ||
| "compliant_llm.total_tests", | ||
| description="Total number of tests executed", | ||
| unit="1", | ||
| ) | ||
| self.success_count = meter.create_counter( | ||
| "compliant_llm.success_count", | ||
| description="Number of successful attacks", | ||
| unit="1", | ||
| ) | ||
| self.test_duration = meter.create_histogram( | ||
| "compliant_llm.test_duration", | ||
| description="Duration of each test in seconds", | ||
| unit="s", | ||
| ) | ||
| self.strategy_usage = meter.create_counter( | ||
| "compliant_llm.strategy_usage", | ||
| description="Usage count per attack strategy", | ||
| unit="1", | ||
| ) | ||
| self.provider_usage = meter.create_counter( | ||
| "compliant_llm.provider_usage", | ||
| description="Usage count per LLM provider", | ||
| unit="1", | ||
| ) | ||
| self.prompt_length_hist = meter.create_histogram( | ||
| "compliant_llm.prompt_length", | ||
| description="Length of prompts in characters", | ||
| unit="chars", | ||
| ) | ||
| self.api_response_time = meter.create_histogram( | ||
| "compliant_llm.api_response_time", | ||
| description="API response time in seconds", | ||
| unit="s", | ||
| ) | ||
| self.errors = meter.create_counter( | ||
| "compliant_llm.errors", | ||
| description="Number of errors encountered", | ||
| unit="1", | ||
| ) | ||
| self.nist_compliance = meter.create_counter( | ||
| "compliant_llm.nist_compliance", | ||
| description="NIST compliance status", | ||
| unit="1", | ||
| ) | ||
|
|
||
| def track_test_start(self, strategy_name: str, provider_name: str): | ||
| self.total_tests.add(1) | ||
| self.strategy_usage.add(1, {"strategy": strategy_name}) | ||
| self.provider_usage.add(1, {"provider": provider_name}) | ||
| self.start_time = time.time() | ||
|
|
||
| def track_test_end(self, success: bool): | ||
| duration = time.time() - self.start_time | ||
| self.test_duration.record(duration) | ||
| if success: | ||
| self.success_count.add(1) | ||
|
|
||
| def track_prompt_length(self, prompt: str, prompt_type: str): | ||
| self.prompt_length_hist.record(len(prompt), {"type": prompt_type}) | ||
|
|
||
| def track_api_response(self, response_time: float, tokens: int, provider: str): | ||
| self.api_response_time.record(response_time, {"provider": provider}) | ||
|
|
||
| def track_error(self, error_type: str, error_message: str): | ||
| self.errors.add(1, {"type": error_type, "message": error_message[:100]}) | ||
|
|
||
| def track_nist_compliance(self, is_compliant: bool, category: str): | ||
| self.nist_compliance.add(1, { | ||
| "category": category, | ||
| "status": "pass" if is_compliant else "fail" | ||
| }) | ||
|
|
||
| # Initialize global tracker | ||
| analytics_tracker = AnalyticsTracker() |
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
vinikatyal marked this conversation as resolved.
Outdated
Show resolved
Hide resolved
|
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
This file contains hidden or 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 hidden or 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 hidden or 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
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.
Uh oh!
There was an error while loading. Please reload this page.