Skip to content

Commit

Permalink
Metrics: keep route when disabled
Browse files Browse the repository at this point in the history
This is to return a 503 Service Unavailable error instead of a 404 Not
Found, a more helpfull error.
  • Loading branch information
Skantes committed May 29, 2024
1 parent 10b589b commit 2d20184
Show file tree
Hide file tree
Showing 3 changed files with 22 additions and 157 deletions.
9 changes: 5 additions & 4 deletions http/http.go
Original file line number Diff line number Diff line change
Expand Up @@ -100,10 +100,11 @@ func HTTPServer(redis *database.Redis, cache *mirrors.Cache) *HTTP {
if config.GetConfig().MetricsEnabled {
log.Info("Metrics enabled")
h.metrics = NewMetrics(redis)
http.Handle("/metrics", NewGzipHandler(h.metricsHandler))
} else {
h.metrics = nil
log.Info("Metrics disabled")
}
http.Handle("/metrics", NewGzipHandler(h.metricsHandler))

// Load the GeoIP databases
if err := h.geoip.LoadGeoIP(); err != nil {
Expand Down Expand Up @@ -170,9 +171,9 @@ func (h *HTTP) Reload() {
if config.GetConfig().MetricsEnabled && h.metrics == nil {
log.Info("Configuration Reload: Metrics enabled")
h.metrics = NewMetrics(h.redis)
http.Handle("/metrics", NewGzipHandler(h.metricsHandler))
} else {
log.Info("Configuration Reload: Metrics not enabled")
} else if h.metrics != nil {
h.metrics = nil
log.Info("Configuration Reload: Metrics disabled")
}

// Reload the templates
Expand Down
16 changes: 12 additions & 4 deletions http/metrics.go
Original file line number Diff line number Diff line change
Expand Up @@ -38,6 +38,7 @@ func NewMetrics(r *database.Redis) *Metrics {
metrics.getMetrics(r)
trackedFiles, err := r.GetListOfTrackedFiles()
if err != nil {
log.Error("WTF")
log.Error(err.Error)
} else {
metrics.trackedFileList = trackedFiles
Expand Down Expand Up @@ -375,10 +376,17 @@ func (m *Metrics) getMetrics(httpRedis *database.Redis) {
}

func (h *HTTP) metricsHandler(w http.ResponseWriter, r *http.Request) {
h.metrics.lock.Lock()
output := h.metrics.metricsResponse
h.metrics.lock.Unlock()
w.Write([]byte(output))
if config.GetConfig().MetricsEnabled {
h.metrics.lock.Lock()
output := h.metrics.metricsResponse
h.metrics.lock.Unlock()
w.Write([]byte(output))
log.Debug("test")
} else {
log.Errorf("Error: metrics are disabled")
http.Error(w, http.StatusText(http.StatusServiceUnavailable), http.StatusServiceUnavailable)
}

}

func getDurationFromKey(key string) string {
Expand Down
154 changes: 5 additions & 149 deletions mirrorbits.conf
Original file line number Diff line number Diff line change
@@ -1,151 +1,7 @@
# vim: set ft=yaml:

###################
##### GENERAL #####
###################

## Path to the local repository
# Repository: /srv/repo

## Path to the templates (default autodetect)
# Templates: /usr/share/mirrorbits/

## A local path or URL containing the JavaScript used by the templates.
## If this is not set (the default), the JavaScript will just be loaded
## from the usual CDNs. See also `contrib/localjs/fetchfiles.sh`.
# LocalJSPath:

## Path where to store logs (comment to disable)
# LogDir: /var/log/mirrorbits

## Path to the GeoIP2 mmdb databases
# GeoipDatabasePath: /usr/share/GeoIP/

## OutputMode can take on the three values:
## - redirect: HTTP redirect to the destination file on the selected mirror
## - json: return a json document for pre-treatment by an application
## - auto: based on the Accept HTTP header
# OutputMode: auto

## Enable Gzip compression
# Gzip: false

## Interval in seconds between which 2 range downloads of a given file
## from a same origin (hashed (IP, user-agent) couple) are considered
## to be the same download. In particular, download statistics are not
## incremented for this file.
# SameDownloadInterval: 600

## Host and port to listen on
# ListenAddress: :8080

## Host and port to listen for the CLI RPC
# RPCListenAddress: localhost:3390

## Password for restricting access to the CLI (optional)
# RPCPassword:

####################
##### DATABASE #####
####################

## Redis host and port
# RedisAddress: 10.0.0.1:6379

## Redis password (if any)
# RedisPassword: supersecure

## Redis database ID (if any)
# RedisDB: 0

## Redis sentinel name (only if using sentinel)
# RedisSentinelMasterName: mirrorbits

## List of Redis sentinel hosts (only if using sentinel)
# RedisSentinels:
# - Host: 10.0.0.1:26379
# - Host: 10.0.0.2:26379
# - Host: 10.0.0.3:26379

###################
##### MIRRORS #####
###################

## Relative path to the trace file within the repository (optional).
## The file must contain the number of seconds since epoch and should
## be updated every minute (or so) with a cron on the master repository.
# TraceFileLocation: /trace

## Interval between two scans of the local repository.
## The repository scan will index new and removed files and collect file
## sizes and checksums.
## This should, more or less, match the frequency where the local repo
## is updated.
# RepositoryScanInterval: 5

## Enable or disable specific hashing algorithms
# Hashes:
# SHA256: On
# SHA1: Off
# MD5: Off

###################
##### MIRRORS #####
###################

## Maximum number of concurrent mirror synchronization to do (rsync/ftp)
# ConcurrentSync: 5

## Interval in minutes between mirror scan
# ScanInterval: 30

## Interval in minutes between mirrors HTTP health checks
# CheckInterval: 1

## Allow a mirror to issue an HTTP redirect.
## Setting this to true will disable the mirror if a redirect is detected.
# DisallowRedirects: false

## Disable a mirror if an active file is missing (HTTP 404)
# DisableOnMissingFile: false

## Adjust the weight/range of the geographic distribution
# WeightDistributionRange: 1.5

## Maximum number of alternative links to return in the HTTP header
# MaxLinkHeaders: 10

## Automatically fix timezone offsets.
## Enable this if one or more mirrors are always excluded because their
## last-modification-time mismatch. This option will try to guess the
## offset and adjust the mod time accordingly.
## Affected mirrors will need to be rescanned after enabling this feature.
# FixTimezoneOffsets: false

## List of mirrors to use as fallback which will be used in case mirrorbits
## is unable to answer a request because the database is unreachable.
## Note: Mirrorbits will redirect to one of these mirrors based on the user
## location but won't be able to know if the mirror has the requested file.
## Therefore only put your most reliable and up-to-date mirrors here.
# Fallbacks:
# - URL: http://fallback1.mirror/repo/
# CountryCode: fr
# ContinentCode: eu
# - URL: http://fallback2.mirror/repo/
# CountryCode: us
# ContinentCode: na

###################
##### METRICS #####
###################

## Enable the metrics route, default to false
## MetricsEnable: false

## Number of days to keep the daily top 10 metrics in database
## Keep in mind that this will have an impact on the database's RAM usage
## It is not recommended to keep more than a few days of retention.
## MetricsTopFilesRetention: 0

## Enable / Disable automatically adding a new file to tracked files
## MetricsAutoTrackedFiles: false
Repository: /srv/repo
ListenAddress: :80
RedisAddress: 172.18.0.1:6379
LogDir: /var/log/mirrorbits
MetricsEnabled: true

0 comments on commit 2d20184

Please sign in to comment.