artprima / prometheus-metrics-bundle

Symfony 5/6 Prometheus Metrics Bundle
MIT License
127 stars 29 forks source link

Add storage adapter factory (#51) (#55) (#61) (#64) #68

Closed Johnmeurt closed 2 years ago

Johnmeurt commented 2 years ago

This PR provides factories for the storage adapter. This allows evaluation of the adapter in runtime. With this internal update you can now:

To declare a custom adapter you should add a custom facotry and add the tag prometheus_metrics_bundle.adapter_factory. I also register the factory interface for auto configuration.

The storage factory is designed to manage their own options. They should extract from array and cast if necessary their options and also manage default values before injection. I could change this parameter to a class to simplify the usage options (I don't know if this is relevant).

I change the configuration to move type and redis under a new parameter storage and I deprecated those parameters. I add a variable parameter storage.options to allows additionnal parameters.

The container will have a new parameter for debug prometheus_metrics_bundle.storage. The container parameters prometheus_metrics_bundle.type and prometheus_metrics_bundle.redis are still set for debug purpose but are not use anymore.

I am not sure about the storage. It provides all known parameters for redis and apcu but I feel documentation could be enough. This means those parameters will move under storage.options as variable parameters.

I also add support of DSN. The value extract from the dsn will override the defined parameters. Here is a example:

artprima_prometheus_metrics:
    # namespace is used to prefix the prometheus metrics
    namespace: myapp

    # type and redis are still available.

    # metrics backend
    storage:
        # DSN of the storage. All parsed values will override explicitly set parameters. Ex: redis://127.0.0.1?timeout=0.1
        url: ~

        # Known values: in_memory, apcu, redis
        type: in_memory

        # Available parameters used by redis
        host: 127.0.0.1
        port: 6379
        timeout: 0.1
        read_timeout: 10
        persistent_connections: false
        password: ~
        database: ~ # Int value used by redis adapter
        prefix: ~   # String value used by redis and apcu

        # A variable parameter to define additionnal options as key / value.
        options:
            foo: bar
codecov-commenter commented 2 years ago

Codecov Report

Merging #68 (0a9d527) into master (9966e12) will increase coverage by 3.38658%. The diff coverage is 94.02985%.

@@                  Coverage Diff                  @@
##                master         #68         +/-   ##
=====================================================
+ Coverage     82.69231%   86.07889%   +3.38658%     
- Complexity         100         134         +34     
=====================================================
  Files               13          17          +4     
  Lines              312         431        +119     
=====================================================
+ Hits               258         371        +113     
- Misses              54          60          +6     
Impacted Files Coverage Δ
StorageFactory/ApcFactory.php 50.00000% <50.00000%> (ø)
...njection/Compiler/ResolveAdapterDefinitionPass.php 71.42857% <80.00000%> (-7.14286%) :arrow_down:
StorageFactory/FactoryRegistry.php 93.93939% <93.93939%> (ø)
StorageFactory/RedisFactory.php 94.73684% <94.73684%> (ø)
DependencyInjection/Configuration.php 97.47899% <96.36364%> (-0.95851%) :arrow_down:
...cyInjection/ArtprimaPrometheusMetricsExtension.php 100.00000% <100.00000%> (ø)
StorageFactory/InMemoryFactory.php 100.00000% <100.00000%> (ø)

Continue to review full report at Codecov.

Legend - Click here to learn more Δ = absolute <relative> (impact), ø = not affected, ? = missing data Powered by Codecov. Last update 9966e12...0a9d527. Read the comment docs.