-
Notifications
You must be signed in to change notification settings - Fork 2
Export metrics to OTel collector and define custom metrics #649
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 all commits
Commits
Show all changes
16 commits
Select commit
Hold shift + click to select a range
241c6a7
Enable sending metrics to otlp endpoint
p-j-smith 3c52c3f
Add a core.metrics module to define custom metrics
p-j-smith e2b50f5
Add a core.telemetry.configure_metrics function
p-j-smith a9a6ee5
call record_study_exported in the export api after a study is success…
p-j-smith 95efbcf
Make linters happy
p-j-smith 88fe5fc
Add pixl_metrics.studies_exported metric, used in the orthanc-plugin
p-j-smith 162c5c3
Make linters happy
p-j-smith aa52315
Make linters happy
p-j-smith 2e5975e
Record the failure message and type separately
p-j-smith aba6130
Add docs on adding new custom metrics
p-j-smith 8fbb6c8
Add record_instance_deidentification_failure metric and use it in ort…
p-j-smith c880965
Add metrics from rabbitmq
p-j-smith 00e362f
Add unit tests for custom metrics
p-j-smith 93733a5
Make linters happy
p-j-smith e7106ce
Define RABBITMQ_METRICS_PORT for the system tests
p-j-smith a9f74a5
Make linters happy
p-j-smith 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
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,142 @@ | ||
| # Copyright (c) University College London Hospitals NHS Foundation Trust | ||
| # | ||
| # 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. | ||
| """Define custom metrics for PIXL.""" | ||
|
|
||
| from __future__ import annotations | ||
|
|
||
| from dataclasses import dataclass | ||
|
|
||
| from opentelemetry import metrics | ||
|
|
||
| __all__ = [ | ||
| "initialise_metrics", | ||
| "record_instance_deidentification_failure", | ||
| "record_study_deidentification_failure", | ||
| "record_study_exported", | ||
| ] | ||
|
|
||
|
|
||
| @dataclass | ||
| class PixlMetrics: | ||
| """Custom metrics for PIXL.""" | ||
|
|
||
| studies_exported: metrics.Counter | None = None | ||
| deidentification_failures: metrics.Counter | None = None | ||
| instance_deidentification_failures: metrics.Counter | None = None | ||
|
|
||
|
|
||
| pixl_metrics = PixlMetrics() | ||
|
|
||
|
|
||
| def initialise_metrics() -> None: | ||
| """ | ||
| Initialise custom metrics for PIXL. | ||
|
|
||
| This must be done after the metrics provider has been set up, | ||
| otherwise the metrics will be no-ops. | ||
| """ | ||
| meter = metrics.get_meter(__name__) | ||
|
|
||
| pixl_metrics.studies_exported = meter.create_counter( | ||
| name="pixl.studies.exported", | ||
| description="Number of studies exported, by project.", | ||
| unit="1", | ||
| ) | ||
|
|
||
| description = ( | ||
| "Number of studies that failed to be de-identified, by project and failure reason." | ||
| ) | ||
| pixl_metrics.deidentification_failures = meter.create_counter( | ||
| name="pixl.studies.deidentification.failures", | ||
| unit="1", | ||
| description=description, | ||
| ) | ||
|
|
||
| pixl_metrics.instance_deidentification_failures = meter.create_counter( | ||
| name="pixl.instances.deidentification.failures", | ||
| unit="1", | ||
| description=( | ||
| "Number of instances that failed to be de-identified, by project and failure reason." | ||
| ), | ||
| ) | ||
|
|
||
|
|
||
| def record_study_exported(project_name: str) -> None: | ||
| """ | ||
| Record a study exported metric. | ||
|
|
||
| Args: | ||
| project_name (str): The name of the project for which the study was exported. | ||
|
|
||
| """ | ||
| if pixl_metrics.studies_exported is None: | ||
| return | ||
|
|
||
| pixl_metrics.studies_exported.add( | ||
| amount=1, | ||
| attributes={"project_name": project_name}, | ||
| ) | ||
|
|
||
|
|
||
| def record_study_deidentification_failure( | ||
| project_name: str, | ||
| failure_type: str, | ||
| message: str, | ||
| ) -> None: | ||
| """ | ||
| Record a study de-identification failure metric. | ||
|
|
||
| Args: | ||
| project_name: The name of the project for which the de-identification failure occurred. | ||
| failure_type: The type of the failure. | ||
| message (str): The message for the de-identification failure. | ||
|
|
||
| """ | ||
| if pixl_metrics.deidentification_failures is None: | ||
| return | ||
|
|
||
| pixl_metrics.deidentification_failures.add( | ||
| amount=1, | ||
| attributes={"project_name": project_name, "type": failure_type, "message": message}, | ||
| ) | ||
|
|
||
|
|
||
| def record_instance_deidentification_failure( | ||
| project_name: str, | ||
| study_uid: str, | ||
| failure_type: str, | ||
| message: str, | ||
| ) -> None: | ||
| """ | ||
| Record an instance de-identification failure metric. | ||
|
|
||
| Args: | ||
| project_name: The name of the project for which the de-identification failure occurred. | ||
| study_uid: The UID of the study for which the de-identification failure occurred. | ||
| failure_type: The type of the failure. | ||
| message (str): The message for the de-identification failure. | ||
|
|
||
| """ | ||
| if pixl_metrics.instance_deidentification_failures is None: | ||
| return | ||
|
|
||
| pixl_metrics.instance_deidentification_failures.add( | ||
| amount=1, | ||
| attributes={ | ||
| "project_name": project_name, | ||
| "study_uid": study_uid, | ||
| "type": failure_type, | ||
| "message": message, | ||
| }, | ||
| ) |
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
Oops, something went wrong.
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.
Uh oh!
There was an error while loading. Please reload this page.