1
#pragma once
2

            
3
#include "envoy/common/pure.h"
4

            
5
#include "absl/strings/string_view.h"
6
#include "absl/types/optional.h"
7

            
8
namespace Envoy {
9
namespace Stats {
10

            
11
/**
12
 * CustomStatNamespaces manages custom stat namespaces. Custom stat namespaces are registered
13
 * by extensions that create user-defined metrics, and these metrics are all prefixed
14
 * by the namespace. For example, Wasm extension registers "wasmcustom" as a custom stat namespace,
15
 * and all the metrics created by user Wasm programs are prefixed by "wasmcustom." internally.
16
 * This is mainly for distinguishing these "custom metrics" defined outside Envoy codebase from
17
 * the native metrics defined by Envoy codebase, and this way stat sinks are able to determine
18
 * how to expose these two kinds of metrics.
19
 * Note that the implementation will not be thread-safe so users of this class must be in the main
20
 * thread.
21
 */
22
class CustomStatNamespaces {
23
public:
24
220304
  virtual ~CustomStatNamespaces() = default;
25

            
26
  /**
27
   * @param name is the name to check.
28
   * @return true if the given name is registered as a custom stat namespace, false otherwise.
29
   */
30
  virtual bool registered(const absl::string_view name) const PURE;
31

            
32
  /**
33
   * Used to register a custom namespace by extensions.
34
   * @param name is the name to register.
35
   */
36
  virtual void registerStatNamespace(const absl::string_view name) PURE;
37

            
38
  /**
39
   * Strips the registered custom stat namespace from the given stat name's prefix if it lives in a
40
   * registered custom stat namespace, and the stripped string is returned. Otherwise return
41
   * nullopt.
42
   * @param stat_name is the view to modify. If it is not in any custom registered namespaces, it
43
   * will never be modified.
44
   * @return the stripped string if stat_name has a registered custom stat namespace. Otherwise,
45
   * return nullopt.
46
   */
47
  virtual absl::optional<absl::string_view>
48
  stripRegisteredPrefix(const absl::string_view stat_name) const PURE;
49
};
50

            
51
} // namespace Stats
52
} // namespace Envoy