<commit_before>package metrics2

import (
	"sync"
	"time"

	"golang.org/x/net/context"

	"go.skia.org/infra/go/util"
)

const (
	MEASUREMENT_LIVENESS = "liveness"
)

// Liveness keeps a time-since-last-successful-update metric.
//
// The unit of the metrics is in seconds.
//
// It is used to keep track of periodic processes to make sure that they are running
// successfully. Every liveness metric should have a corresponding alert set up that
// will fire of the time-since-last-successful-update metric gets too large.
type Liveness struct {
	cancelFn             func()
	lastSuccessfulUpdate time.Time
	m                    *Int64Metric
	mtx                  sync.Mutex
}

// NewLiveness creates a new Liveness metric helper. The current value is
// reported at the given frequency; if the report frequency is zero, the value
// is only reported when it changes.
func (c *Client) NewLiveness(name string, tagsList ...map[string]string) *Liveness {
	// Make a copy of the tags and add the name.
	tags := util.AddParams(map[string]string{}, tagsList...)
	tags["name"] = name
	ctx, cancelFn := context.WithCancel(context.Background())
	l := &Liveness{
		cancelFn:             cancelFn,
		lastSuccessfulUpdate: time.Now(),
		m:                    c.GetInt64Metric(MEASUREMENT_LIVENESS, tags),
		mtx:                  sync.Mutex{},
	}
	go util.RepeatCtx(c.reportFrequency, ctx, func() {
		l.update()
	})
	return l
}

// NewLiveness creates a new Liveness metric helper using the default client.
// The current value is reported at the given frequency; if the report frequency
// is zero, the value is only reported when it changes.
func NewLiveness(name string, tags ...map[string]string) *Liveness {
	return DefaultClient.NewLiveness(name, tags...)
}

// getLocked returns the current value of the Liveness. Assumes the caller holds a lock.
func (l *Liveness) getLocked() int64 {
	return int64(time.Since(l.lastSuccessfulUpdate).Seconds())
}

// Get returns the current value of the Liveness.
func (l *Liveness) Get() int64 {
	l.mtx.Lock()
	defer l.mtx.Unlock()
	return l.getLocked()
}

// updateLocked sets the value of the Liveness. Assumes the caller holds a lock.<commit_msg>metrics2.Liveness: Don't wrap l.update in another function<commit_after>package metrics2

import (
	"sync"
	"time"

	"golang.org/x/net/context"

	"go.skia.org/infra/go/util"
)

const (
	MEASUREMENT_LIVENESS = "liveness"
)

// Liveness keeps a time-since-last-successful-update metric.
//
// The unit of the metrics is in seconds.
//
// It is used to keep track of periodic processes to make sure that they are running
// successfully. Every liveness metric should have a corresponding alert set up that
// will fire of the time-since-last-successful-update metric gets too large.
type Liveness struct {
	cancelFn             func()
	lastSuccessfulUpdate time.Time
	m                    *Int64Metric
	mtx                  sync.Mutex
}

// NewLiveness creates a new Liveness metric helper. The current value is
// reported at the given frequency; if the report frequency is zero, the value
// is only reported when it changes.
func (c *Client) NewLiveness(name string, tagsList ...map[string]string) *Liveness {
	// Make a copy of the tags and add the name.
	tags := util.AddParams(map[string]string{}, tagsList...)
	tags["name"] = name
	ctx, cancelFn := context.WithCancel(context.Background())
	l := &Liveness{
		cancelFn:             cancelFn,
		lastSuccessfulUpdate: time.Now(),
		m:                    c.GetInt64Metric(MEASUREMENT_LIVENESS, tags),
		mtx:                  sync.Mutex{},
	}
	go util.RepeatCtx(c.reportFrequency, ctx, l.update)
	return l
}

// NewLiveness creates a new Liveness metric helper using the default client.
// The current value is reported at the given frequency; if the report frequency
// is zero, the value is only reported when it changes.
func NewLiveness(name string, tags ...map[string]string) *Liveness {
	return DefaultClient.NewLiveness(name, tags...)
}

// getLocked returns the current value of the Liveness. Assumes the caller holds a lock.
func (l *Liveness) getLocked() int64 {
	return int64(time.Since(l.lastSuccessfulUpdate).Seconds())
}

// Get returns the current value of the Liveness.
func (l *Liveness) Get() int64 {
	l.mtx.Lock()
	defer l.mtx.Unlock()
	return l.getLocked()
}

// updateLocked sets the value of the Liveness. Assumes the caller holds a lock.
{'fraction_non_alphanumeric': 0.10116199589883801, 'fraction_numerical': 0.005240373661426293, 'mean_word_length': 3.2580019398642097, 'pattern_counts': {'":': 0, '<': 3, '<?xml version=': 0, '>': 3, 'https://': 0, 'lorem ipsum': 0, 'www.': 0, 'xml': 0}, 'pii_count': 0, 'substrings_counts': 0, 'word_list_counts': {'cursed_substrings.json': 0, 'profanity_word_list.json': 0, 'sexual_word_list.json': 0, 'zh_pornsignals.json': 0}}
{'dir': 'git-commits-cleaned', 'id': '1645172', 'n_tokens_mistral': 1348, 'n_tokens_neox': 1321, 'n_words': 484}