From b3b47d40d68f11ac9fac64e75ec44b43df0d55ac Mon Sep 17 00:00:00 2001 From: Nikolas Giannikopoulos Date: Fri, 18 Aug 2023 04:33:51 +0100 Subject: [PATCH 1/9] Telemetry - WIP --- docker-compose.yml | 49 + .../README.md | 5 +- .../project/Build.scala | 15 +- .../project/plugins.sbt | 3 +- .../src/main/resources/application.conf | 85 + .../lunatechlabs/dotty/SudokuSolverMain.scala | 2 + infrastructure/grafana/etc/grafana.ini | 1378 +++++++++++++++++ .../provisioning/datasources/datasource.yaml | 25 + infrastructure/prometheus/prometheus.yml | 10 + 9 files changed, 1564 insertions(+), 8 deletions(-) create mode 100644 docker-compose.yml create mode 100644 infrastructure/grafana/etc/grafana.ini create mode 100644 infrastructure/grafana/etc/provisioning/datasources/datasource.yaml create mode 100644 infrastructure/prometheus/prometheus.yml diff --git a/docker-compose.yml b/docker-compose.yml new file mode 100644 index 000000000..cad9d2181 --- /dev/null +++ b/docker-compose.yml @@ -0,0 +1,49 @@ +version: '3.9' + +networks: + monitoring: + driver: bridge + +volumes: + prometheus_data: {} + grafana_data: {} + +services: + grafana: + container_name: grafana + image: grafana/grafana-oss:9.5.7 + volumes: + - ./infrastructure/grafana/etc:/etc/grafana:ro + #- grafana-log:/var/log/grafana + - grafana_data:/var/lib/grafana + ports: + - "3000:3000" + restart: always + depends_on: + - prometheus + privileged: true + networks: + - monitoring + + prometheus: + container_name: prometheus + image: prom/prometheus:v2.46.0 + volumes: + - ./infrastructure/prometheus/prometheus.yml:/etc/prometheus/prometheus.yml + - prometheus_data:/prometheus + ports: + - "9090:9090" + restart: always + privileged: true + networks: + - monitoring + extra_hosts: + - "host.docker.internal:host-gateway" + + zipkin: + container_name: zipkin + image: openzipkin/zipkin:2.24 + ports: + - "9411:9411" + networks: + - monitoring diff --git a/exercises/exercise_000_sudoku_solver_initial_state/README.md b/exercises/exercise_000_sudoku_solver_initial_state/README.md index aa201d0fc..5412cf285 100644 --- a/exercises/exercise_000_sudoku_solver_initial_state/README.md +++ b/exercises/exercise_000_sudoku_solver_initial_state/README.md @@ -20,7 +20,7 @@ This application, written in Scala 2, implements an Akka Typed/Scala based Sudoku Solver that is composed of 29 actors. The application will also start up an instance of a Sudoku problem generator -that continuously cycles through a series of Sudoko problems at a configurable +that continuously cycles through a series of Sudoku problems at a configurable rate, tunable via configuration. The course consists of a series of exercises in which you change the source code to leverage Scala 3 specific features. The application comes with tests that are basically the same across all exercises. The tests should pass at every stage: if they don't, you've broken something and you will need to fix it. @@ -83,7 +83,7 @@ SLF4J: See also http://www.slf4j.org/codes.html#replay man [e] > Scala 2 to Scala 3 > sudoku solver initial state > ``` -- Run the Sodukosolver by executing the `runSolver` command from the `sbt` prompt. +- Run the Sudokusolver by executing the `runSolver` command from the `sbt` prompt. - Note that you can stop the application by hitting `Return` in the sbt session. @@ -161,4 +161,3 @@ body { ``` ![IntelliJ Markdown viewer settings](images/Markdown-viewer-IntelliJ.png) - diff --git a/exercises/exercise_000_sudoku_solver_initial_state/project/Build.scala b/exercises/exercise_000_sudoku_solver_initial_state/project/Build.scala index ee2789094..d3f0a7296 100644 --- a/exercises/exercise_000_sudoku_solver_initial_state/project/Build.scala +++ b/exercises/exercise_000_sudoku_solver_initial_state/project/Build.scala @@ -12,6 +12,7 @@ object CompileOptions { object Versions { lazy val akkaVer = "2.6.20" + lazy val kamonVer = "2.6.3" lazy val logbackVer = "1.2.3" lazy val mUnitVer = "0.7.26" } @@ -29,9 +30,15 @@ object Dependencies { private lazy val munitDeps = Seq("org.scalameta" %% "munit" % Versions.mUnitVer % Test) + private lazy val kamonDeps = Seq( + "io.kamon" %% "kamon-bundle" % Versions.kamonVer, + "io.kamon" %% "kamon-prometheus" % Versions.kamonVer + ) + lazy val dependencies: Seq[ModuleID] = - logbackDeps ++ - munitDeps ++ - akkaDeps ++ - akkaTestkitDeps + akkaDeps ++ + akkaTestkitDeps ++ + kamonDeps ++ + logbackDeps ++ + munitDeps } diff --git a/exercises/exercise_000_sudoku_solver_initial_state/project/plugins.sbt b/exercises/exercise_000_sudoku_solver_initial_state/project/plugins.sbt index 4cdd8f2a2..305da2312 100644 --- a/exercises/exercise_000_sudoku_solver_initial_state/project/plugins.sbt +++ b/exercises/exercise_000_sudoku_solver_initial_state/project/plugins.sbt @@ -1,4 +1,5 @@ addSbtPlugin("com.eed3si9n" % "sbt-buildinfo" % "0.11.0") +addSbtPlugin("io.kamon" % "sbt-kanela-runner" % "2.0.14") addSbtPlugin("org.scalameta" % "sbt-mdoc" % "2.3.7") -addSbtPlugin("org.scalameta" % "sbt-scalafmt" % "2.5.0") addSbtPlugin("org.scalameta" % "sbt-native-image" % "0.3.1") +addSbtPlugin("org.scalameta" % "sbt-scalafmt" % "2.5.0") diff --git a/exercises/exercise_000_sudoku_solver_initial_state/src/main/resources/application.conf b/exercises/exercise_000_sudoku_solver_initial_state/src/main/resources/application.conf index 0b5d34e99..58ad077b0 100644 --- a/exercises/exercise_000_sudoku_solver_initial_state/src/main/resources/application.conf +++ b/exercises/exercise_000_sudoku_solver_initial_state/src/main/resources/application.conf @@ -23,3 +23,88 @@ akka { } } } + +kamon { + environment { + service = "scala2-to-scala3-course-service" + } + + trace { + tick-interval = 5 seconds + sampler = always + } + + instrumentation.akka.filters { + actors.track { + includes = [ "sudoku-solver-system/user/**" ] + } + + actors.trace { + includes = [ "sudoku-solver-system/user/**" ] + } + + actors.start-trace { + includes = [ "sudoku-solver-system/user/**" ] + } + + dispatchers { + includes = [ "sudoku-solver-system/akka.actor.default-dispatcher" ] + } + } + + # Source: https://kamon.io/docs/latest/reporters/prometheus/ + prometheus { + include-environment-tags = true + embedded-server { + hostname = 0.0.0.0 + port = 9095 + } + + # Source: https://kamon.io/docs/latest/reporters/prometheus/ + buckets { + default-buckets = [ + 10, + 30, + 100, + 300, + 1000, + 3000, + 10000, + 30000, + 100000 + ] + + time-buckets = [ + 0.005, + 0.01, + 0.025, + 0.05, + 0.075, + 0.1, + 0.25, + 0.5, + 0.75, + 1, + 2.5, + 5, + 7.5, + 10 + ] + + information-buckets = [ + 512, + 1024, + 2048, + 4096, + 16384, + 65536, + 524288, + 1048576 + ] + } + } +} + +kanela { + log-level = "DEBUG" +} diff --git a/exercises/exercise_000_sudoku_solver_initial_state/src/main/scala/org/lunatechlabs/dotty/SudokuSolverMain.scala b/exercises/exercise_000_sudoku_solver_initial_state/src/main/scala/org/lunatechlabs/dotty/SudokuSolverMain.scala index 67dc0f85e..cc30f8571 100644 --- a/exercises/exercise_000_sudoku_solver_initial_state/src/main/scala/org/lunatechlabs/dotty/SudokuSolverMain.scala +++ b/exercises/exercise_000_sudoku_solver_initial_state/src/main/scala/org/lunatechlabs/dotty/SudokuSolverMain.scala @@ -20,6 +20,7 @@ import akka.actor.typed.scaladsl.adapter.TypedActorSystemOps import akka.actor.typed.scaladsl.{Behaviors, Routers} import akka.actor.typed.{ActorSystem, Behavior, Terminated} import org.lunatechlabs.dotty.sudoku.{SudokuProblemSender, SudokuSolver, SudokuSolverSettings} +import kamon.Kamon import scala.Console.{GREEN, RESET} import scala.io.StdIn @@ -42,6 +43,7 @@ object Main { object SudokuSolverMain { def main(args: Array[String]): Unit = { + Kamon.init() val system = ActorSystem[NotUsed](Main(), "sudoku-solver-system") diff --git a/infrastructure/grafana/etc/grafana.ini b/infrastructure/grafana/etc/grafana.ini new file mode 100644 index 000000000..74a7b939a --- /dev/null +++ b/infrastructure/grafana/etc/grafana.ini @@ -0,0 +1,1378 @@ +# Import from upstream grafana sample config with minimum changes: +# - https://github.com/grafana/grafana/blob/v9.5.7/conf/sample.ini + +##################### Grafana Configuration Example ##################### +# +# Everything has defaults so you only need to uncomment things you want to +# change + +# possible values : production, development +;app_mode = production + +# instance name, defaults to HOSTNAME environment variable value or hostname if HOSTNAME var is empty +;instance_name = ${HOSTNAME} + +# force migration will run migrations that might cause dataloss +;force_migration = false + +#################################### Paths #################################### +[paths] +# Path to where grafana can store temp files, sessions, and the sqlite3 db (if that is used) +;data = /var/lib/grafana + +# Temporary files in `data` directory older than given duration will be removed +;temp_data_lifetime = 24h + +# Directory where grafana can store logs +;logs = /var/log/grafana + +# Directory where grafana will automatically scan and look for plugins +;plugins = /var/lib/grafana/plugins + +# folder that contains provisioning config files that grafana will apply on startup and while running. +;provisioning = conf/provisioning + +#################################### Server #################################### +[server] +# Protocol (http, https, h2, socket) +;protocol = http + +# The ip address to bind to, empty will bind to all interfaces +;http_addr = + +# The http port to use +;http_port = 3000 + +# The public facing domain name used to access grafana from a browser +;domain = localhost + +# Redirect to correct domain if host header does not match domain +# Prevents DNS rebinding attacks +;enforce_domain = false + +# The full public facing url you use in browser, used for redirects and emails +# If you use reverse proxy and sub path specify full url (with sub path) +;root_url = %(protocol)s://%(domain)s:%(http_port)s/ + +# Serve Grafana from subpath specified in `root_url` setting. By default it is set to `false` for compatibility reasons. +;serve_from_sub_path = false + +# Log web requests +;router_logging = false + +# the path relative working path +;static_root_path = public + +# enable gzip +;enable_gzip = false + +# https certs & key file +;cert_file = +;cert_key = + +# Unix socket gid +# Changing the gid of a file without privileges requires that the target group is in the group of the process and that the process is the file owner +# It is recommended to set the gid as http server user gid +# Not set when the value is -1 +;socket_gid = + +# Unix socket mode +;socket_mode = + +# Unix socket path +;socket = + +# CDN Url +;cdn_url = + +# Sets the maximum time using a duration format (5s/5m/5ms) before timing out read of an incoming request and closing idle connections. +# `0` means there is no timeout for reading the request. +;read_timeout = 0 + +# This setting enables you to specify additional headers that the server adds to HTTP(S) responses. +[server.custom_response_headers] +#exampleHeader1 = exampleValue1 +#exampleHeader2 = exampleValue2 + +#################################### GRPC Server ######################### +;[grpc_server] +;network = "tcp" +;address = "127.0.0.1:10000" +;use_tls = false +;cert_file = +;key_file = + +#################################### Database #################################### +[database] +# You can configure the database connection by specifying type, host, name, user and password +# as separate properties or as on string using the url properties. + +# Either "mysql", "postgres" or "sqlite3", it's your choice +;type = sqlite3 +;host = 127.0.0.1:3306 +;name = grafana +;user = root +# If the password contains # or ; you have to wrap it with triple quotes. Ex """#password;""" +;password = + +# Use either URL or the previous fields to configure the database +# Example: mysql://user:secret@host:port/database +;url = + +# For "postgres", use either "disable", "require" or "verify-full" +# For "mysql", use either "true", "false", or "skip-verify". +;ssl_mode = disable + +# Database drivers may support different transaction isolation levels. +# Currently, only "mysql" driver supports isolation levels. +# If the value is empty - driver's default isolation level is applied. +# For "mysql" use "READ-UNCOMMITTED", "READ-COMMITTED", "REPEATABLE-READ" or "SERIALIZABLE". +;isolation_level = + +;ca_cert_path = +;client_key_path = +;client_cert_path = +;server_cert_name = + +# For "sqlite3" only, path relative to data_path setting +;path = grafana.db + +# Max idle conn setting default is 2 +;max_idle_conn = 2 + +# Max conn setting default is 0 (mean not set) +;max_open_conn = + +# Connection Max Lifetime default is 14400 (means 14400 seconds or 4 hours) +;conn_max_lifetime = 14400 + +# Set to true to log the sql calls and execution times. +;log_queries = + +# For "sqlite3" only. cache mode setting used for connecting to the database. (private, shared) +;cache_mode = private + +# For "sqlite3" only. Enable/disable Write-Ahead Logging, https://sqlite.org/wal.html. Default is false. +;wal = false + +# For "mysql" only if migrationLocking feature toggle is set. How many seconds to wait before failing to lock the database for the migrations, default is 0. +;locking_attempt_timeout_sec = 0 + +# For "sqlite" only. How many times to retry query in case of database is locked failures. Default is 0 (disabled). +;query_retries = 0 + +# For "sqlite" only. How many times to retry transaction in case of database is locked failures. Default is 5. +;transaction_retries = 5 + +################################### Data sources ######################### +[datasources] +# Upper limit of data sources that Grafana will return. This limit is a temporary configuration and it will be deprecated when pagination will be introduced on the list data sources API. +;datasource_limit = 5000 + +#################################### Cache server ############################# +[remote_cache] +# Either "redis", "memcached" or "database" default is "database" +;type = database + +# cache connectionstring options +# database: will use Grafana primary database. +# redis: config like redis server e.g. `addr=127.0.0.1:6379,pool_size=100,db=0,ssl=false`. Only addr is required. ssl may be 'true', 'false', or 'insecure'. +# memcache: 127.0.0.1:11211 +;connstr = + +# prefix prepended to all the keys in the remote cache +; prefix = + +# This enables encryption of values stored in the remote cache +;encryption = + +#################################### Data proxy ########################### +[dataproxy] + +# This enables data proxy logging, default is false +;logging = false + +# How long the data proxy waits to read the headers of the response before timing out, default is 30 seconds. +# This setting also applies to core backend HTTP data sources where query requests use an HTTP client with timeout set. +;timeout = 30 + +# How long the data proxy waits to establish a TCP connection before timing out, default is 10 seconds. +;dialTimeout = 10 + +# How many seconds the data proxy waits before sending a keepalive probe request. +;keep_alive_seconds = 30 + +# How many seconds the data proxy waits for a successful TLS Handshake before timing out. +;tls_handshake_timeout_seconds = 10 + +# How many seconds the data proxy will wait for a server's first response headers after +# fully writing the request headers if the request has an "Expect: 100-continue" +# header. A value of 0 will result in the body being sent immediately, without +# waiting for the server to approve. +;expect_continue_timeout_seconds = 1 + +# Optionally limits the total number of connections per host, including connections in the dialing, +# active, and idle states. On limit violation, dials will block. +# A value of zero (0) means no limit. +;max_conns_per_host = 0 + +# The maximum number of idle connections that Grafana will keep alive. +;max_idle_connections = 100 + +# How many seconds the data proxy keeps an idle connection open before timing out. +;idle_conn_timeout_seconds = 90 + +# If enabled and user is not anonymous, data proxy will add X-Grafana-User header with username into the request, default is false. +;send_user_header = false + +# Limit the amount of bytes that will be read/accepted from responses of outgoing HTTP requests. +;response_limit = 0 + +# Limits the number of rows that Grafana will process from SQL data sources. +;row_limit = 1000000 + +# Sets a custom value for the `User-Agent` header for outgoing data proxy requests. If empty, the default value is `Grafana/` (for example `Grafana/9.0.0`). +;user_agent = + +#################################### Analytics #################################### +[analytics] +# Server reporting, sends usage counters to stats.grafana.org every 24 hours. +# No ip addresses are being tracked, only simple counters to track +# running instances, dashboard and error counts. It is very helpful to us. +# Change this option to false to disable reporting. +;reporting_enabled = true + +# The name of the distributor of the Grafana instance. Ex hosted-grafana, grafana-labs +;reporting_distributor = grafana-labs + +# Set to false to disable all checks to https://grafana.com +# for new versions of grafana. The check is used +# in some UI views to notify that a grafana update exists. +# This option does not cause any auto updates, nor send any information +# only a GET request to https://raw.githubusercontent.com/grafana/grafana/main/latest.json to get the latest version. +;check_for_updates = true + +# Set to false to disable all checks to https://grafana.com +# for new versions of plugins. The check is used +# in some UI views to notify that a plugin update exists. +# This option does not cause any auto updates, nor send any information +# only a GET request to https://grafana.com to get the latest versions. +;check_for_plugin_updates = true + +# Google Analytics universal tracking code, only enabled if you specify an id here +;google_analytics_ua_id = + +# Google Analytics 4 tracking code, only enabled if you specify an id here +;google_analytics_4_id = + +# When Google Analytics 4 Enhanced event measurement is enabled, we will try to avoid sending duplicate events and let Google Analytics 4 detect navigation changes, etc. +;google_analytics_4_send_manual_page_views = false + +# Google Tag Manager ID, only enabled if you specify an id here +;google_tag_manager_id = + +# Rudderstack write key, enabled only if rudderstack_data_plane_url is also set +;rudderstack_write_key = + +# Rudderstack data plane url, enabled only if rudderstack_write_key is also set +;rudderstack_data_plane_url = + +# Rudderstack SDK url, optional, only valid if rudderstack_write_key and rudderstack_data_plane_url is also set +;rudderstack_sdk_url = + +# Rudderstack Config url, optional, used by Rudderstack SDK to fetch source config +;rudderstack_config_url = + +# Intercom secret, optional, used to hash user_id before passing to Intercom via Rudderstack +;intercom_secret = + +# Controls if the UI contains any links to user feedback forms +;feedback_links_enabled = true + +#################################### Security #################################### +[security] +# disable creation of admin user on first start of grafana +;disable_initial_admin_creation = false + +# default admin user, created on startup +;admin_user = admin + +# default admin password, can be changed before first start of grafana, or in profile settings +;admin_password = admin + +# default admin email, created on startup +;admin_email = admin@localhost + +# used for signing +;secret_key = SW2YcwTIb9zpOOhoPsMm + +# current key provider used for envelope encryption, default to static value specified by secret_key +;encryption_provider = secretKey.v1 + +# list of configured key providers, space separated (Enterprise only): e.g., awskms.v1 azurekv.v1 +;available_encryption_providers = + +# disable gravatar profile images +;disable_gravatar = false + +# data source proxy whitelist (ip_or_domain:port separated by spaces) +;data_source_proxy_whitelist = + +# disable protection against brute force login attempts +;disable_brute_force_login_protection = false + +# set to true if you host Grafana behind HTTPS. default is false. +;cookie_secure = false + +# set cookie SameSite attribute. defaults to `lax`. can be set to "lax", "strict", "none" and "disabled" +;cookie_samesite = lax + +# set to true if you want to allow browsers to render Grafana in a ,