PrometheusCounters

Performance counters that send their metrics to Prometheus service.

Extends: CachedCounters

Implements: IReferenceable, IOpenable

Description

The PrometheusCounters class allows you to create performance counters that send their metrics to Pormetheus service.

Important points

  • The component is normally used in passive mode conjunction with PrometheusMetricsController. Alternatively, when connection parameters are set, it can push metrics to Prometheus PushGateway.

Configuration parameters

  • connection(s):

    • discovery_key: (optional) key to retrieve the connection from IDiscovery
    • protocol: connection protocol: http or https
    • host: host name or IP address
    • port: port number
    • uri: resource URI or connection string with all parameters in it
  • options:

    • retries: number of retries (default: 3)
    • connect_timeout: connection timeout in milliseconds (default: 10 sec)
    • timeout: invocation timeout in milliseconds (default: 10 sec)

References

  • *:logger:*:*:1.0 - (optional) ILogger components to pass log messages
  • *:discovery:*:*:1.0 - (optional) IDiscovery services
  • *:counters:*:*:1.0 - (optional) ICounters components to pass collected measurements

Constructors

NewPrometheusCounters

Creates a new instance of the performance counters.

NewPrometheusCounters() *PrometheusCounters

Methods

Close

Closes the component and frees used resources.

(c *PrometheusCounters) Close(ctx context.Context, context IContext) error

  • ctx; context.Context - operation context.
  • context: IContext - (optional) a context to trace execution through a call chain.
  • returns: error - error or nil no errors occured.

Configure

Configures the component by passing its configuration parameters.

(c *PrometheusCounters) Configure(ctx context.Context, config *cconf.ConfigParams)

  • ctx: context.Context - operation context.
  • config: *cconf.ConfigParams - configuration parameters to be set.

IsOpen

Checks if the component is opened.

(c *PrometheusCounters) IsOpen() bool

  • returns: bool - True if the component is open and False otherwise.

Open

Opens the component.

(c *PrometheusCounters) Open(ctx context.Context, context IContext) (err error)

  • ctx: context.Context - operation context.
  • context: IContext - (optional) a context to trace execution through a call chain.
  • returns: (err error) - error or nil no errors occured.

Save

Saves the current counters' measurements.

(c *PrometheusCounters) Save(ctx context.Context, counters []*ccount.Counter) (err error)

  • ctx: context.Context - operation context.
  • counters: []*ccount.Counter - current counters measurements to be saved.
  • returns: (err error) - error or nil no errors occured.

SetReferences

Sets references to dependent components.

(c *PrometheusCounters) SetReferences(ctx context.Context, references cref.IReferences)

  • ctx: context.Context - operation context.
  • references: cref.IReferences - references to locate the component dependencies.

Examples

ctx := context.Background()
counters = NewPrometheusCounters();
counters.Configure(ctx, cconf.NewConfigParamsFromTuples(
    "connection.protocol", "http",
    "connection.host", "localhost",
    "connection.port", 8080,
));

counters.Open("123")

counters.Increment(ctx, "mycomponent.mymethod.calls")
timing := counters.BeginTiming(ctx, "mycomponent.mymethod.exec_time")
    ...
timing.EndTiming(ctx)

counters.Dump(ctx)

See also