Skip to content

Commit

Permalink
refactor metrics naming for wasm
Browse files Browse the repository at this point in the history
Signed-off-by: shawnh2 <shawnhxh@outlook.com>
  • Loading branch information
shawnh2 committed Jul 2, 2024
1 parent e9f7266 commit f4ee318
Show file tree
Hide file tree
Showing 4 changed files with 39 additions and 27 deletions.
8 changes: 4 additions & 4 deletions internal/metrics/otel_label.go
Original file line number Diff line number Diff line change
Expand Up @@ -18,11 +18,11 @@ var (
)

const (
StatusSuccess = "Success"
StatusFailure = "Failure"
StatusSuccess = "success"
StatusFailure = "failure"

ReasonError = "Error"
ReasonConflict = "Conflict"
ReasonError = "error"
ReasonConflict = "conflict"
)

// A Label provides a named dimension for a Metric.
Expand Down
17 changes: 10 additions & 7 deletions internal/wasm/cache.go
Original file line number Diff line number Diff line change
Expand Up @@ -239,7 +239,7 @@ func (c *localFileCache) getOrFetch(key cacheKey, opts GetOptions) (*cacheEntry,
// Download the Wasm module with http fetcher.
b, err = c.httpFetcher.Fetch(ctx, key.downloadURL, insecure)
if err != nil {
wasmRemoteFetchCount.With(resultTag.Value(downloadFailure)).Increment()
wasmRemoteFetchTotal.WithFailure(reasonDownloadError).Increment()
return nil, err
}

Expand All @@ -251,7 +251,7 @@ func (c *localFileCache) getOrFetch(key cacheKey, opts GetOptions) (*cacheEntry,
isPrivate = true
}
if imageBinaryFetcher, dChecksum, err = c.prepareFetch(ctx, u, insecure, opts); err != nil {
wasmRemoteFetchCount.With(resultTag.Value(manifestFailure)).Increment()
wasmRemoteFetchTotal.WithFailure(reasonManifestError).Increment()
return nil, fmt.Errorf("could not fetch Wasm OCI image: %w", err)
}
default:
Expand All @@ -261,7 +261,7 @@ func (c *localFileCache) getOrFetch(key cacheKey, opts GetOptions) (*cacheEntry,
// If the checksum is provided, check if it matches the downloaded binary.
if key.checksum != "" {
if dChecksum != key.checksum {
wasmRemoteFetchCount.With(resultTag.Value(checksumMismatch)).Increment()
wasmRemoteFetchTotal.WithFailure(reasonChecksumMismatch).Increment()
return nil, fmt.Errorf("module downloaded from %v has checksum %v, which does not match: %v", key.downloadURL, dChecksum, key.checksum)
}
} else {
Expand All @@ -272,17 +272,18 @@ func (c *localFileCache) getOrFetch(key cacheKey, opts GetOptions) (*cacheEntry,
if imageBinaryFetcher != nil {
b, err = imageBinaryFetcher()
if err != nil {
wasmRemoteFetchCount.With(resultTag.Value(downloadFailure)).Increment()
wasmRemoteFetchTotal.WithFailure(reasonDownloadError).Increment()
return nil, fmt.Errorf("could not fetch Wasm binary: %w", err)
}
}

if !isValidWasmBinary(b) {
wasmRemoteFetchCount.With(resultTag.Value(fetchFailure)).Increment()
wasmRemoteFetchTotal.WithFailure(reasonFetchError).Increment()
return nil, fmt.Errorf("fetched Wasm binary from %s is invalid", key.downloadURL)
}

wasmRemoteFetchCount.With(resultTag.Value(fetchSuccess)).Increment()
wasmRemoteFetchTotal.WithSuccess().Increment()

return c.addEntry(key, b, isPrivate)
}

Expand Down Expand Up @@ -365,7 +366,9 @@ func (c *localFileCache) addEntry(key cacheKey, wasmModule []byte, isPrivate boo
}
ce.referencingURLs.Insert(key.downloadURL)
c.modules[key.moduleKey] = &ce

wasmCacheEntries.Record(float64(len(c.modules)))

return &ce, nil
}

Expand All @@ -378,7 +381,7 @@ func (c *localFileCache) getEntry(key cacheKey, pullPolicy PullPolicy, u *url.UR
c.mux.Lock()
defer func() {
c.mux.Unlock()
wasmCacheLookupCount.With(hitTag.Value(strconv.FormatBool(cacheHit))).Increment()
wasmCacheLookupTotal.With(hitTag.Value(strconv.FormatBool(cacheHit))).Increment()
}()

// If no checksum is provided, check if a wasm module with the same downloading URL has been pulled before.
Expand Down
28 changes: 12 additions & 16 deletions internal/wasm/metrics.go
Original file line number Diff line number Diff line change
Expand Up @@ -21,33 +21,29 @@ package wasm

import "github.com/envoyproxy/gateway/internal/metrics"

// Const strings for label value.
const (
// For remote fetch metric.
fetchSuccess = "success"
fetchFailure = "fetch_failure"
downloadFailure = "download_failure"
manifestFailure = "manifest_failure"
checksumMismatch = "checksum_mismatched"
reasonFetchError = "fetch_error"
reasonDownloadError = "download_error"
reasonManifestError = "manifest_error"
reasonChecksumMismatch = "checksum_mismatched"
)

var (
hitTag = metrics.NewLabel("hit")
resultTag = metrics.NewLabel("result")
hitTag = metrics.NewLabel("hit")

wasmCacheEntries = metrics.NewGauge(
"wasm_cache_entries",
"number of Wasm remote fetch cache entries.",
"Number of Wasm remote fetch cache entries.",
)

wasmCacheLookupCount = metrics.NewCounter(
"wasm_cache_lookup_count",
"number of Wasm remote fetch cache lookups.",
wasmCacheLookupTotal = metrics.NewCounter(
"wasm_cache_lookup_total",
"Total number of Wasm remote fetch cache lookups.",
)

wasmRemoteFetchCount = metrics.NewCounter(
"wasm_remote_fetch_count",
"number of Wasm remote fetches and results, including success, download failure, and checksum mismatch.",
wasmRemoteFetchTotal = metrics.NewCounter(
"wasm_remote_fetch_total",
"Total number of Wasm remote fetches and results.",
)
)

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -81,4 +81,17 @@ Each metric includes the `kind` label to identify the corresponding resources be

Metrics may also include `name` and `namespace` label to identify the name and namespace of corresponding Infrastructure Manager.

## Wasm

Envoy Gateway monitors the status of Wasm remote fetch cache.

| Name | Description |
|---------------------------|--------------------------------------------------|
| `wasm_cache_entries` | Number of Wasm remote fetch cache entries. |
| `wasm_cache_lookup_total` | Total number of Wasm remote fetch cache lookups. |
| `wasm_remote_fetch_total` | Total number of Wasm remote fetches and results. |

For metric `wasm_cache_lookup_total`, we are using `hit` label (boolean) to indicate whether the Wasm cache has been hit.


[prom-format]: https://prometheus.io/docs/instrumenting/exposition_formats/#text-based-format

0 comments on commit f4ee318

Please sign in to comment.