Naar hoofdinhoud gaan
Configuratie. Belangrijk, want zonder weet de AppSignal Ruby gem niet welke applicatie hij instrumenteert of in welke omgeving. In dit onderwerp leggen we uit hoe u AppSignal configureert, wat er in de Ruby gem geconfigureerd kan worden, wat de minimaal benodigde configuratie is en hoe de configuratie wordt geladen.

Minimaal benodigde configuratie

Om applicaties data naar AppSignal te laten rapporteren is de volgende configuratie vereist. Alle andere configuratie is optioneel. Voor Rails-apps hoeft u uw applicatienaam niet te configureren; AppSignal gebruikt standaard de naam van uw applicatie. Als u een framework gebruikt dat omgevingen kent en door de AppSignal gem ondersteund wordt, wordt de omgeving automatisch gedetecteerd.
# config/appsignal.rb
Appsignal.configure do |config|
  config.activate_if_environment(:development, :staging, :production)
  config.name = "My app"
  config.push_api_key = "1234-1234-1234"
end

Configuratiemethoden

Er zijn verschillende configuratiemethoden beschikbaar in onze Ruby gem. Om te zien in welke volgorde ze worden geladen en welke configuratiemethode waarden uit andere bronnen overschrijft, raadpleegt u onze pagina over de laadvolgorde van configuratie.

Ruby configuratiebestand

De AppSignal Ruby gem kan worden geconfigureerd met behulp van een Ruby configuratiebestand. Als hiervoor gekozen wordt, maakt de installer automatisch een config/appsignal.rb-bestand aan. Dit bestand bevat standaardconfiguratie-instellingen die kunnen worden aangepast om aan de specifieke behoeften van uw applicatie te voldoen. Dit configuratiebestand wordt gelezen wanneer Appsignal.start wordt aangeroepen, wat automatisch gebeurt voor de meeste van onze integraties. Lees de instructies voor het integreren van AppSignal als Appsignal.start niet automatisch wordt aangeroepen. Het configuratiebestand configureert onze Ruby gem met behulp van de Appsignal.configure helper. Lees de sectie de Appsignal.configure helper voor meer details over hoe u deze gebruikt. Het YAML-configuratiebestand wordt niet gelezen wanneer dit bestand aanwezig is.
Appsignal.configure do |config|
  # Enable AppSignal for the following environments
  config.activate_if_environment(:development, :staging, :production)

  # Ignore this action for all environments
  config.ignore_actions << "My global action"

  # Ignore this action for only the production environment
  if config.env?(:production)
    config.ignore_actions << "My production action"
  end

  # Ignore this action for only the staging environment
  if config.env?(:staging)
    config.ignore_actions << "My staging action"
  end
end

Appsignal.configure helper

De Appsignal.configure helper kan worden gebruikt om AppSignal met Ruby-code te configureren. Het Ruby configuratiebestand gebruikt deze helper en deze kan ook direct in uw applicatie worden gebruikt. Als uw app Appsignal.configure gebruikt buiten het config/appsignal.rb-bestand, zoals in een Rails-initializer, lees dan deze sectie over verschillen in gedrag. Stel configuratieopties in binnen het Appsignal.configure-blok door de writer-methoden op het config-object aan te roepen. Configuratieopties kunnen worden ingesteld met de “Config file key” die wordt vermeld bij de configuratieopties. Bijvoorbeeld de send_params-optie kan worden ingesteld met: config.send_params = false.
# Some examples on how to configure config options
Appsignal.configure do |config|
  # Enable AppSignal for the following environments
  config.activate_if_environment(:development, :staging, :production)

  # Configure String config options
  config.name = "My app name"
  # Read a value from an environment variable
  config.hostname = ENV.fetch("HOSTNAME", "default hostname value")

  # Append values to Array config options
  config.ignore_actions += ["My ignored action", "My other ignored action"]
  config.request_headers << "MY_HTTP_HEADER"

  # Configure Boolean config options
  config.send_params = true
  config.enable_host_metrics = false
end
Lees de rest van deze sectie voor meer details over hoe de Appsignal.configure helper werkt en welke andere helpers deze heeft.

String-configuratieopties

Configuratieopties van het type String kunnen zowel met Strings als met Symbols worden ingesteld. Wanneer ingesteld, converteren we Symbols naar Strings.
Appsignal.configure do |config|
  config.activejob_report_errors = :discard

  config.activejob_report_errors
  # => "discard"
end

Array-configuratieopties

Configuratieopties van het type Array kunnen worden gewijzigd als een normale array:
  • Gebruik een toewijzing om de standaardwaarden en eerder ingelezen waarden uit omgevingsvariabelen te overschrijven.
    • Bijvoorbeeld: config.array_option = ["value 1", "value 2"]
    • Optiewaarde: ["value 1", "value 2"]
  • Gebruik de additieve toewijzingsoperator += om een nieuwe array van waarden samen te voegen met de standaardwaarden en eerder ingelezen waarden uit omgevingsvariabelen.
    • Bijvoorbeeld: config.array_option += ["value 1", "value 2"]
    • Optiewaarde: ["DUMMY default value", "value 1", "value 2"]
  • Gebruik de append-operator << om een enkele waarde toe te voegen aan de lijst van standaardwaarden en eerder ingelezen waarden uit omgevingsvariabelen.
    • Bijvoorbeeld: config.array_option << "value 1"
    • Optiewaarde: ["DUMMY default value", "value 1"]

Automatische omgevingsdetectie

De AppSignal Ruby gem detecteert automatisch de omgeving van de applicatie. We integreren met gems zoals Rails, Sinatra, Rack en anderen om te detecteren in welke omgeving de applicatie is gestart. Daarom zou het niet nodig moeten zijn om de omgeving handmatig te configureren. Als u de applicatieomgeving toch handmatig wilt configureren, geef dan de naam van de omgeving als eerste argument aan de Appsignal.configure helper. Dit argument overschrijft de waarde van de APPSIGNAL_APP_ENV omgevingsvariabele en de automatische omgevingsdetectie.
# Configure the application environment explicitly to be "production"
Appsignal.configure(:production) do |config|
  # Some config
end

activate_if_environment helper

De Appsignal.configure helper heeft een helper om te configureren welke omgevingen actief moeten zijn en data naar AppSignal.com moeten rapporteren. Roep activate_if_environment aan met een lijst van omgevingen (Strings en/of Symbols). AppSignal detecteert de omgeving wanneer de Appsignal.configure helper wordt aangeroepen zonder omgevingsargument. Vervolgens controleert het of de omgeving overeenkomt met een van de gegeven waarden, en zo ja, dan wordt de active-configuratieoptie op true gezet.
Appsignal.configure do |config|
  # Enable AppSignal for the following environments
  config.activate_if_environment(:development, :staging, :production)
end
De activate_if_environment helper is een handige helper om te voorkomen dat applicaties hun eigen controles moeten toevoegen wanneer AppSignal actief moet zijn, zoals:
Appsignal.configure do |config|
  # Example of a manual check for which environments AppSignal should be active
  config.active = Rails.env.development? || Rails.env.staging? || Rails.env.production?
end

env? helper

De Appsignal.configure helper heeft een helper om te controleren welke omgeving AppSignal heeft gedetecteerd. Gebruik deze helper om te controleren welke omgeving actief is en zo de configuratieopties in te stellen die alleen op deze omgeving van toepassing zijn. Roep de env? helper aan met een omgevingsnaam (String of Symbol), en deze retourneert true als de naam van de omgeving overeenkomt met de momenteel actieve omgeving.
Appsignal.configure do |config|
  # Add this configuration only for the staging environment
  if config.env?(:staging)
    config.ignore_actions << "My staging action"
  end
end

De Appsignal.configure helper in uw applicatie gebruiken

We raden aan om de AppSignal Ruby gem te configureren met het Ruby configuratiebestand op config/appsignal.rb. Als een Rails-initializer of inline-configuratie de voorkeur heeft, lees dan deze sectie voor de veranderingen in gedrag die dit met zich meebrengt. Als de Appsignal.configure helper wordt aangeroepen voordat het config/appsignal.rb Ruby-configuratiebestand wordt gelezen wanneer Appsignal.start wordt aangeroepen, zal het Ruby-configuratiebestand niet worden gelezen. In Rails-apps, zorg ervoor dat u de AppSignal gem zo configureert dat hij start nadat Rails is geïnitialiseerd, anders wordt de configuratie ingesteld met Appsignal.configure genegeerd wanneer deze in een Rails-initializer zoals config/initializers/appsignal.rb wordt aangeroepen.
In Ruby gem versie 3.12 en nieuwer wordt, als een YAML-configuratiebestand aanwezig is, dit gelezen wanneer Appsignal.configure wordt aangeroepen in een Rails-initializer of inline in een applicatie. Dit gedrag is verouderd. Verplaats alle configuratie naar de Appsignal.configure helper.We zullen dit gedrag in de volgende major-versie van de Ruby gem verwijderen.
Voorbeeld van een Rails-initializer:
# Example file: config/initializers/appsignal.rb

# WARNING: In this example the `config/appsignal.rb` config file is not read!
Appsignal.configure do |config|
  # Set some config
end
Voorbeeld van inline-configuratie:
# Example file: app.rb
require "appsignal"
require "web_framework"

# WARNING: In this example the `config/appsignal.rb` config file is not read!
Appsignal.configure do |config|
  # Set some config
end
Appsignal.start

run WebFramework.app

Systeem-omgevingsvariabelen

AppSignal kan ook worden geconfigureerd met behulp van systeem-omgevingsvariabelen op de host waarop de applicatie die AppSignal monitort, draait. Dit is gebruikelijk op platforms zoals Heroku. Zorg ervoor dat deze omgevingsvariabelen geconfigureerd zijn op een manier die compatibel is met uw besturingssysteem en dat de waarden worden geladen voordat uw app met AppSignal wordt gestart.
export APPSIGNAL_APP_NAME="My app"

YAML-configuratiebestand

Deze configuratiemethode is een verouderde methode en wordt verwijderd in de volgende major-versie van de Ruby gem.Gebruik in plaats daarvan het Ruby configuratiebestand.
De AppSignal Ruby gem kan worden geconfigureerd met een YAML-configuratiebestand. Tijdens de installatie maakt de Ruby gem, indien geselecteerd, een config/appsignal.yml-bestand aan. In dit bestand wordt enige standaardconfiguratie aangeleverd die kan worden aangepast aan de behoeften van uw applicatie. Dit config/appsignal.yml-bestand ondersteunt ERB-tags zodat systeem-omgevingsvariabelen ook in dit bestand geladen kunnen worden. De config/appsignal.yml-configuratievoorbeelden die voor configuratieopties getoond worden, gebruiken het default YAML-anker. De AppSignal-installer maakt standaard een config/appsignal.yml-bestand met dit anker aan. Als dit niet aanwezig is, zorg er dan voor dat u de configuratieoptie aan de juiste omgeving toevoegt.
# config/appsignal.yml
# Define the "defaults" anchor
default: &defaults
  name: "My app"
  # Supports ERB
  push_api_key: "<%= ENV['APPSIGNAL_PUSH_API_KEY'] %>"

production:
  # Loads the defaults in the production environment by referencing the anchor
  <<: *defaults
  # production environment specific configuration
  active: true

Meerdere app-omgevingen

Meerdere app-omgevingen kunnen in dit bestand worden geconfigureerd via root-level sleutels.
# Example: config/appsignal.yml
development: # Development app environment
  active: true

production: # Production app environment
  active: true

test: # Testing app environment
  active: false # Disabled for test environment
Om te voorkomen dat we de configuratie voor elke app-omgeving moeten herhalen, kunnen we YAML-ankers gebruiken om YAML-objecten uit te breiden.
# Example: config/appsignal.yml
default: &defaults
  active: true

development: # Development app environment
  <<: *defaults

production: # Production app environment
  <<: *defaults

test: # Testing app environment
  <<: *defaults
  active: false # Overwrites the inherited active config option from "defaults"
Het is niet mogelijk om alleen een defaults-anker te configureren en dat automatisch op alle omgevingen toe te passen. Elke omgeving moet in het YAML-bestand worden geconfigureerd met een root-level sleutel en dit defaults-anker uitbreiden.

Voorbeeld van YAML-configuratiebestand

Hier is een voorbeeld van een appsignal.yml-configuratiebestand. Het wordt aanbevolen dat u alleen de configuratie die u nodig heeft aan uw configuratiebestand toevoegt. Voor de volledige lijst met opties, zie de pagina met configuratie- opties.
# config/appsignal.yml
default: &defaults
  # Your Push API Key, it is possible to set this dynamically using ERB. Required
  push_api_key: "<%= ENV['APPSIGNAL_PUSH_API_KEY'] %>"

  # Your app's name as reported on AppSignal.com. Required
  name: "My App"

  # Your server's hostname. Optional, auto detected
  hostname: "frontend1.myapp.com"

  # Add default instrumentation of net/http. Default: true
  instrument_net_http: true

  # Skip session data, if it contains private information. Default: false
  skip_session_data: true

  # Ignore these errors (Optional)
  ignore_errors:
    - SystemExit

  # Ignore these actions, used by our Loadbalancer. Optional
  ignore_actions:
    - IsUpController#index

  # Enable allocation tracking for memory metrics. Default: true
  enable_allocation_tracking: true

# Configuration per environment, leave out an environment or set active
# to false to not push metrics for that environment.
development:
  <<: *defaults
  active: true
  # Set the severity level of AppSignal's internal logger. Optional
  # Supported values are error, warning, info, debug, trace
  log_level: debug

staging:
  <<: *defaults
  # Configure AppSignal to be active for this environment. Required
  active: true

production:
  <<: *defaults
  # Configure AppSignal to be active for this environment. Required
  active: true

  # Set different path for the log file. Optional, auto detected
  log_path: "/home/my_app/app/shared/log"

  # Set AppSignal working dir. Optional, auto detected
  working_directory_path: "/tmp/appsignal"

  # When it's not possible to connect to the outside world without a proxy.
  # Optional
  http_proxy: "proxy.mydomain.com:8080"