Skip to content

Commit

Permalink
Rename indexer metrics related to get operations (#853)
Browse files Browse the repository at this point in the history
Rename indexer get metrics (histogram) to be specific to each
type of indexer. Currently, file system, zip and storage indexers.
Each indexer type is going to be included as a label in the metric.

Fixed options filter to check whether or not it is nil.
  • Loading branch information
mrodm authored Jul 27, 2022
1 parent 1faeffe commit a69ff8a
Show file tree
Hide file tree
Showing 8 changed files with 48 additions and 18 deletions.
1 change: 1 addition & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -12,6 +12,7 @@ and this project adheres to [Semantic Versioning](https://semver.org/spec/v2.0.0

* Return only the latest version of each package when a combined index is used. [#849](https://github.com/elastic/package-registry/pull/849)
* Return only first appearance of the same version of a package when it is available in multiple indexes. [#849](https://github.com/elastic/package-registry/pull/849)
* Rename indexer metrics related to get operations and add the indexer name label to it. [#853](https://github.com/elastic/package-registry/pull/853)

### Added

Expand Down
6 changes: 1 addition & 5 deletions indexer.go
Original file line number Diff line number Diff line change
Expand Up @@ -7,11 +7,9 @@ package main
import (
"context"
"sort"
"time"

"github.com/Masterminds/semver/v3"

"github.com/elastic/package-registry/metrics"
"github.com/elastic/package-registry/packages"
)

Expand All @@ -37,8 +35,6 @@ func (c CombinedIndexer) Init(ctx context.Context) error {
}

func (c CombinedIndexer) Get(ctx context.Context, opts *packages.GetOptions) (packages.Packages, error) {
start := time.Now()
defer metrics.StorageIndexerGetDurationSeconds.Observe(time.Since(start).Seconds())
var packages packages.Packages
for _, indexer := range c {
p, err := indexer.Get(ctx, opts)
Expand All @@ -48,7 +44,7 @@ func (c CombinedIndexer) Get(ctx context.Context, opts *packages.GetOptions) (pa
packages = packages.Join(p)
}

if !opts.Filter.AllVersions {
if opts != nil && opts.Filter != nil && !opts.Filter.AllVersions {
return latestPackagesVersion(packages), nil
}

Expand Down
2 changes: 1 addition & 1 deletion main.go
Original file line number Diff line number Diff line change
Expand Up @@ -68,7 +68,7 @@ var (
func init() {
flag.BoolVar(&printVersionInfo, "version", false, "Print Elastic Package Registry version")
flag.StringVar(&address, "address", "localhost:8080", "Address of the package-registry service.")
flag.StringVar(&metricsAddress, "metrics-address", "", "Address to expose the Prometheus metrics.")
flag.StringVar(&metricsAddress, "metrics-address", "", "Address to expose the Prometheus metrics (experimental). ")
flag.StringVar(&tlsCertFile, "tls-cert", "", "Path of the TLS certificate.")
flag.StringVar(&tlsKeyFile, "tls-key", "", "Path of the TLS key.")
flag.StringVar(&configPath, "config", "config.yml", "Path to the configuration file.")
Expand Down
11 changes: 6 additions & 5 deletions metrics/metrics.go
Original file line number Diff line number Diff line change
Expand Up @@ -39,32 +39,33 @@ var (
prometheus.CounterOpts{
Namespace: metricsNamespace,
Name: "storage_indexer_update_index_success_total",
Help: "A counter for updates of the cursor.",
Help: "A counter for all the updates index of the cursor that finished successfully in the storage indexer.",
},
)

StorageIndexerUpdateIndexErrorsTotal = prometheus.NewCounter(
prometheus.CounterOpts{
Namespace: metricsNamespace,
Name: "storage_indexer_update_index_error_total",
Help: "A counter for all the update index processes that finished with error.",
Help: "A counter for all the update index of the cursor that finished with error in the storage indexer.",
},
)

StorageIndexerUpdateIndexDurationSeconds = prometheus.NewHistogram(
prometheus.HistogramOpts{
Namespace: metricsNamespace,
Name: "storage_indexer_update_index_duration_seconds",
Help: "A histogram of latencies for update index processes run by the indexer.",
Help: "A histogram of latencies for update index processes run by the storage indexer.",
},
)

StorageIndexerGetDurationSeconds = prometheus.NewHistogram(
IndexerGetDurationSeconds = prometheus.NewHistogramVec(
prometheus.HistogramOpts{
Namespace: metricsNamespace,
Name: "storage_indexer_get_duration_seconds",
Name: "indexer_get_duration_seconds",
Help: "A histogram of latencies for get processes run by the indexer.",
},
[]string{"indexer"},
)
)

Expand Down
2 changes: 1 addition & 1 deletion metrics/middleware.go
Original file line number Diff line number Diff line change
Expand Up @@ -25,7 +25,7 @@ func MetricsMiddleware() mux.MiddlewareFunc {

prometheus.MustRegister(NumberIndexedPackages)
prometheus.MustRegister(StorageRequestsTotal)
prometheus.MustRegister(StorageIndexerGetDurationSeconds)
prometheus.MustRegister(IndexerGetDurationSeconds)
prometheus.MustRegister(StorageIndexerUpdateIndexDurationSeconds)
prometheus.MustRegister(StorageIndexerUpdateIndexSuccessTotal)
prometheus.MustRegister(StorageIndexerUpdateIndexErrorsTotal)
Expand Down
33 changes: 27 additions & 6 deletions packages/http.go
Original file line number Diff line number Diff line change
Expand Up @@ -18,18 +18,31 @@ import (
"github.com/elastic/package-registry/util"
)

const (
localLocationPrometheusLabel = "local"
remoteLocationPrometheusLabel = "remote"

artifactComponentPrometheusLabel = "artifacts"
staticComponentPrometheusLabel = "statics"
signatureComponentPrometheusLabel = "signatures"
)

// ServePackage is used by artifactsHandler to serve packages and signatures.
func ServePackage(w http.ResponseWriter, r *http.Request, p *Package) {
span, _ := apm.StartSpan(r.Context(), "ServePackage", "app")
defer span.End()

if p.RemoteResolver() != nil {
p.RemoteResolver().RedirectArtifactsHandler(w, r, p)
metrics.StorageRequestsTotal.With(prometheus.Labels{"location": "remote", "component": "artifacts"}).Inc()
metrics.StorageRequestsTotal.With(
prometheus.Labels{"location": remoteLocationPrometheusLabel, "component": artifactComponentPrometheusLabel},
).Inc()
return
}
serveLocalPackage(w, r, p, p.BasePath)
metrics.StorageRequestsTotal.With(prometheus.Labels{"location": "local", "component": "artifacts"}).Inc()
metrics.StorageRequestsTotal.With(
prometheus.Labels{"location": localLocationPrometheusLabel, "component": artifactComponentPrometheusLabel},
).Inc()
}

// ServePackageSignature is used by signaturesHandler to serve signatures.
Expand All @@ -39,11 +52,15 @@ func ServePackageSignature(w http.ResponseWriter, r *http.Request, p *Package) {

if p.RemoteResolver() != nil {
p.RemoteResolver().RedirectSignaturesHandler(w, r, p)
metrics.StorageRequestsTotal.With(prometheus.Labels{"location": "remote", "component": "signatures"}).Inc()
metrics.StorageRequestsTotal.With(
prometheus.Labels{"location": remoteLocationPrometheusLabel, "component": signatureComponentPrometheusLabel},
).Inc()
return
}
serveLocalPackage(w, r, p, p.BasePath+".sig")
metrics.StorageRequestsTotal.With(prometheus.Labels{"location": "local", "component": "signatures"}).Inc()
metrics.StorageRequestsTotal.With(
prometheus.Labels{"location": localLocationPrometheusLabel, "component": signatureComponentPrometheusLabel},
).Inc()
}

func serveLocalPackage(w http.ResponseWriter, r *http.Request, p *Package, packagePath string) {
Expand Down Expand Up @@ -82,7 +99,9 @@ func ServePackageResource(w http.ResponseWriter, r *http.Request, p *Package, pa

if p.RemoteResolver() != nil {
p.RemoteResolver().RedirectStaticHandler(w, r, p, packageFilePath)
metrics.StorageRequestsTotal.With(prometheus.Labels{"location": "remote", "component": "static"}).Inc()
metrics.StorageRequestsTotal.With(
prometheus.Labels{"location": remoteLocationPrometheusLabel, "component": staticComponentPrometheusLabel},
).Inc()
return
}

Expand Down Expand Up @@ -119,5 +138,7 @@ func ServePackageResource(w http.ResponseWriter, r *http.Request, p *Package, pa
defer f.Close()

http.ServeContent(w, r, packageFilePath, stat.ModTime(), f)
metrics.StorageRequestsTotal.With(prometheus.Labels{"location": "local", "component": "static"}).Inc()
metrics.StorageRequestsTotal.With(
prometheus.Labels{"location": localLocationPrometheusLabel, "component": staticComponentPrometheusLabel},
).Inc()
}
5 changes: 5 additions & 0 deletions packages/packages.go
Original file line number Diff line number Diff line change
Expand Up @@ -10,12 +10,15 @@ import (
"os"
"path/filepath"
"strings"
"time"

"github.com/Masterminds/semver/v3"
"github.com/pkg/errors"
"github.com/prometheus/client_golang/prometheus"
"go.elastic.co/apm"
"go.uber.org/zap"

"github.com/elastic/package-registry/metrics"
"github.com/elastic/package-registry/util"
)

Expand Down Expand Up @@ -192,6 +195,8 @@ func (i *FileSystemIndexer) Init(ctx context.Context) (err error) {
// This assumes changes to packages only happen on restart (unless development mode is enabled).
// Caching the packages request many file reads every time this method is called.
func (i *FileSystemIndexer) Get(ctx context.Context, opts *GetOptions) (Packages, error) {
start := time.Now()
defer metrics.IndexerGetDurationSeconds.With(prometheus.Labels{"indexer": i.label}).Observe(time.Since(start).Seconds())
if opts == nil {
return i.packageList, nil
}
Expand Down
6 changes: 6 additions & 0 deletions storage/indexer.go
Original file line number Diff line number Diff line change
Expand Up @@ -14,13 +14,16 @@ import (

"cloud.google.com/go/storage"
"github.com/pkg/errors"
"github.com/prometheus/client_golang/prometheus"
"go.uber.org/zap"

"github.com/elastic/package-registry/metrics"
"github.com/elastic/package-registry/packages"
"github.com/elastic/package-registry/util"
)

const indexerGetDurationPrometheusLabel = "StorageIndexer"

type Indexer struct {
options IndexerOptions
storageClient *storage.Client
Expand Down Expand Up @@ -173,6 +176,9 @@ func (i *Indexer) updateIndex(ctx context.Context) error {
}

func (i *Indexer) Get(ctx context.Context, opts *packages.GetOptions) (packages.Packages, error) {
start := time.Now()
defer metrics.IndexerGetDurationSeconds.With(prometheus.Labels{"indexer": indexerGetDurationPrometheusLabel}).Observe(time.Since(start).Seconds())

i.m.RLock()
defer i.m.RUnlock()

Expand Down

0 comments on commit a69ff8a

Please sign in to comment.