-
Notifications
You must be signed in to change notification settings - Fork 193
Add example of OpenTelemetry in Optuna Dashboard #330
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 1 commit
Commits
Show all changes
2 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 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,24 @@ | ||
| services: | ||
| grafana: | ||
| image: grafana/grafana:latest | ||
| ports: | ||
| - "127.0.0.1:13000:3000" | ||
| prometheus: | ||
| image: prom/prometheus | ||
| volumes: | ||
| - ./prometheus.yml:/etc/prometheus.yml | ||
| command: | ||
| - "--config.file=/etc/prometheus.yml" | ||
| ports: | ||
| - "127.0.0.19090:9090" | ||
| otel-collector: | ||
| image: ghcr.io/open-telemetry/opentelemetry-collector-releases/opentelemetry-collector-contrib:latest | ||
| volumes: | ||
| - ./otel-collector-config.yml:/etc/otel-collector-config.yml | ||
| command: ["--config=/etc/otel-collector-config.yml"] | ||
| ports: | ||
| - "127.0.0.1:8888:8888" # Prometheus metrics exposed by the collector | ||
| - "127.0.0.1:8889:8889" # Prometheus exporter metrics | ||
| - "127.0.0.1:13133:13133" # health_check extension | ||
| - "127.0.0.1:4317:4317" # OTLP gRPC receiver | ||
| - "127.0.0.1:4318:4318" # OTLP HTTP receiver | ||
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,57 @@ | ||
| import wsgiref.simple_server | ||
|
|
||
| from opentelemetry import metrics | ||
| from opentelemetry.exporter.otlp.proto.http.metric_exporter import OTLPMetricExporter | ||
| from opentelemetry.instrumentation.sqlalchemy import SQLAlchemyInstrumentor | ||
| from opentelemetry.instrumentation.wsgi import OpenTelemetryMiddleware | ||
| from opentelemetry.sdk.metrics import MeterProvider | ||
| from opentelemetry.sdk.metrics.export import PeriodicExportingMetricReader | ||
| from opentelemetry.sdk.resources import Resource | ||
| from optuna.storages import RDBStorage | ||
| import optuna_dashboard | ||
|
|
||
|
|
||
| SQLALCHEMY_URL = "sqlite:///db.sqlite3" | ||
| OTEL_COLLECTOR_ENDPOINT = "http://127.0.0.1:4318/v1/metrics" | ||
|
|
||
|
|
||
| def main() -> None: | ||
| resource = Resource.create({"service.name": "optuna-dashboard"}) | ||
| readers = [ | ||
| PeriodicExportingMetricReader( | ||
| OTLPMetricExporter(endpoint=OTEL_COLLECTOR_ENDPOINT), | ||
| export_interval_millis=1000, | ||
| export_timeout_millis=5000, | ||
| ), | ||
| ] | ||
| metrics.set_meter_provider(MeterProvider(resource=resource, metric_readers=readers)) | ||
|
|
||
| # If you want to use PrometheusMetricReader, uncomment the following lines | ||
| # from prometheus_client import start_http_server | ||
| # from opentelemetry.exporter.prometheus import PrometheusMetricReader | ||
| # print("Metrics endpoint: http://127.0.0.1:9464/metrics") | ||
| # start_http_server(port=9464, addr="127.0.0.1") | ||
| # readers.append(PrometheusMetricReader("optuna_dashboard")) | ||
|
|
||
| # If you want to see metrics in the console, uncomment the following line | ||
| # from opentelemetry.sdk.metrics.export import ConsoleMetricExporter | ||
| # readers.append(PeriodicExportingMetricReader(ConsoleMetricExporter())) | ||
|
|
||
| # Enable opentelemetry-instrumentation-sqlalchemy | ||
| storage = RDBStorage(SQLALCHEMY_URL, skip_compatibility_check=True, skip_table_creation=True) | ||
| SQLAlchemyInstrumentor().instrument( | ||
| engine=storage.engine, | ||
| meter_provider=metrics.get_meter_provider(), | ||
| ) | ||
|
|
||
| # Enable opentelemetry-instrumentation-wsgi | ||
| app = optuna_dashboard.wsgi(storage=storage) | ||
| app = OpenTelemetryMiddleware(app, meter_provider=metrics.get_meter_provider()) | ||
|
|
||
| # Start Optuna Dashboard | ||
| with wsgiref.simple_server.make_server("127.0.0.1", 8080, app) as httpd: | ||
| httpd.serve_forever() | ||
|
|
||
|
|
||
| if __name__ == "__main__": | ||
| main() |
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,25 @@ | ||
| receivers: | ||
| otlp: | ||
| protocols: | ||
| http: | ||
| endpoint: otel-collector:4318 | ||
| cors: | ||
| allowed_origins: | ||
| - "http://*" | ||
| - "https://*" | ||
| processors: | ||
| batch: | ||
| exporters: | ||
| prometheus: | ||
| endpoint: "0.0.0.0:8889" | ||
| debug: | ||
| verbosity: detailed | ||
| extensions: | ||
| health_check: | ||
| service: | ||
| extensions: ["health_check"] | ||
| pipelines: | ||
| metrics: | ||
| receivers: [otlp] | ||
| processors: [batch] | ||
| exporters: [prometheus, debug] |
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,7 @@ | ||
| global: | ||
| scrape_interval: 15s | ||
| scrape_configs: | ||
| - job_name: "prometheus" | ||
| scrape_interval: 5s | ||
| static_configs: | ||
| - targets: ["otel-collector:8889"] |
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,7 @@ | ||
| optuna-dashboard | ||
| opentelemetry-api>=1.34.1 | ||
| opentelemetry-exporter-otlp>=1.34.1 | ||
| opentelemetry-exporter-prometheus>=0.55b1 | ||
| opentelemetry-sdk>=1.34.1 | ||
| opentelemetry-instrumentation-wsgi | ||
| opentelemetry-instrumentation-sqlalchemy |
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.