connstate

package module
v0.2.5 Latest Latest
Warning

This package is not in the latest version of its module.

Go to latest
Published: Dec 17, 2025 License: Apache-2.0 Imports: 7 Imported by: 1

README

component-connstate - Settings component for collecting HTTP server metrics

GoDoc

Bugs Code Smells Coverage Lines of Code Maintainability Rating Quality Gate Status Reliability Rating Security Rating Technical Debt Vulnerabilities

autoauto- [component-connstate - Settings component for collecting HTTP server metrics](#component-connstate---settings-component-for-collecting-http-server-metrics)auto - [Overview](#overview)auto - [Quick Start](#quick-start)auto - [Status](#status)auto - [Contributing](#contributing)auto - [Building And Testing](#building-and-testing)auto - [License](#license)auto - [Contributing Agreement](#contributing-agreement)autoauto

Overview

This is a settings that enables constructing a metrics gathering component that integrates with the http.Server.ConnState hook. It works by tracking the state of all active connections and any state changes of a connection while the server runs.

Quick Start

package main

import (
    "context"
    "net/http"

    stat "github.com/asecurityteam/component-stat"
    stat "github.com/asecurityteam/component-connstate"
    "github.com/asecurityteam/settings/v2"
)

func main() {
    ctx := context.Background()
    envSource := settings.NewEnvSource(os.Environ())

    s, _ := stat.New(ctx, envSource)
    cs, _ := connstate.Load(ctx, envSource, connstate.NewComponent().WithStat(s))
    defer cs.Close()

    srv := &http.Server{
        ConnState: cs.HandleEvent,
    }
    go cs.Report()

    _ = srv.ListenAndServe()
}

Status

This project is in incubation which means we are not yet operating this tool in production and the interfaces are subject to change.

Contributing

Building And Testing

We publish a docker image called SDCLI that bundles all of our build dependencies. It is used by the included Makefile to help make building and testing a bit easier. The following actions are available through the Makefile:

  • make dep

    Install the project dependencies into a vendor directory

  • make lint

    Run our static analysis suite

  • make test

    Run unit tests and generate a coverage artifact

  • make integration

    Run integration tests and generate a coverage artifact

  • make coverage

    Report the combined coverage for unit and integration tests

License

This project is licensed under Apache 2.0. See LICENSE.txt for details.

Contributing Agreement

Atlassian requires signing a contributor's agreement before we can accept a patch. If you are an individual you can fill out the individual CLA. If you are contributing on behalf of your company then please fill out the corporate CLA.

Documentation

Index

Constants

This section is empty.

Variables

This section is empty.

Functions

This section is empty.

Types

type Component

type Component struct {
	Stat stat.Stat
}

Component implements the settings.Component interface for connection state monitoring.

func NewComponent

func NewComponent() *Component

NewComponent populates the default values.

func (*Component) New

func (c *Component) New(_ context.Context, conf *Config) (*ConnState, error)

New produces a ServerFn bound to the given configuration.

func (*Component) Settings

func (*Component) Settings() *Config

Settings returns a configuration with all defaults set.

func (*Component) WithStat

func (*Component) WithStat(s stat.Stat) *Component

WithStat returns a copy of the Component bound to a given Stat instance.

type Config

type Config struct {
	NewCounter      string        `description:"Name of the counter metric tracking new clients."`
	NewGauge        string        `description:"Name of the gauge metric tracking new clients."`
	ActiveCounter   string        `description:"Name of the counter metric tracking active clients."`
	ActiveGauge     string        `description:"Name of the gauge metric tracking active clients."`
	IdleCounter     string        `description:"Name of the counter metric tracking idle clients."`
	IdleGauge       string        `description:"Name of the gauge metric tracking idle clients."`
	ClosedCounter   string        `description:"Name of the counter metric tracking closed clients."`
	HijackedCounter string        `description:"Name of the counter metric tracking hijacked clients."`
	ReportInterval  time.Duration `description:"Interval on which gauges are reported."`
}

Config is a container for internal metrics settings.

func (*Config) Description

func (*Config) Description() string

Description returns the help information for the configuration root.

func (*Config) Name

func (*Config) Name() string

Name of the configuration root.

type ConnState

type ConnState struct {
	Stat                      stat.Stat
	Tracking                  *sync.Map
	NewClientCounterName      string
	NewClientGaugeName        string
	ActiveClientCounterName   string
	ActiveClientGaugeName     string
	IdleClientCounterName     string
	IdleClientGaugeName       string
	ClosedClientCounterName   string
	HijackedClientCounterName string
	Interval                  time.Duration
	// contains filtered or unexported fields
}

ConnState plugs into the http.Server.ConnState attribute to track the number of client connections to the server.

func Load

func Load(ctx context.Context, source settings.Source, c *Component) (*ConnState, error)

Load is a convenience method for binding the source to the component.

func (*ConnState) Close

func (c *ConnState) Close() error

Close the reporting loop.

func (*ConnState) HandleEvent

func (c *ConnState) HandleEvent(conn net.Conn, state http.ConnState)

HandleEvent tracks state changes of a connection.

func (*ConnState) Report

func (c *ConnState) Report()

Report loops on a time interval and pushes a set of gauge metrics.

Jump to

Keyboard shortcuts

? : This menu
/ : Search site
f or F : Jump to
y or Y : Canonical URL