<commit_before>/*
Package export provides an interface to instrument Go code.

Instrumenting Go code with this package is very similar to the "expvar" package
in the vanilla Go distribution. In fact, the variables exported with this
package are also registered with the "expvar" package, so that you can also use
other existing metric collection frameworks with it. This package differs in
that it has an explicitly cumulative type, Derive.

The intended usage pattern of this package is as follows: First, global
variables are initialized with NewDerive(), NewGauge(), NewDeriveString() or
NewGaugeString(). The Run() function is called as a separate goroutine as part
of your program's initialization and, last but not least, the variables are
updated with their respective update functions, Add() for Derive and Set() for
Gauge.

  // Initialize global variable.
  var requestCounter = export.NewDeriveString("example.com/golang/total_requests")

  // Call Run() in its own goroutine.
  func main() {
          client, err := network.Dial(
                  net.JoinHostPort(network.DefaultIPv6Address, network.DefaultService),
                  network.ClientOptions{})
          if err !=  nil {
                  log.Fatal(err)
                  }
          go export.Run(client, export.Options{
                  Interval: 10 * time.Second,
          })
          // …
  }

  // Update variable.
  func requestHandler(w http.ResponseWriter, req *http.Request) {
          defer requestCounter.Add(1)
          // …
  }
*/
package export // import "collectd.org/export"

import (
	"context"
	"expvar"
	"log"
	"math"
	"strconv"
	"sync"
	"time"

	"collectd.org/api"
)

var (
	mutex sync.RWMutex
	vars  []Var
)

// Var is an abstract type for metrics exported by this package.
type Var interface {
	ValueList() *api.ValueList
}

// Publish adds v to the internal list of exported metrics.
func Publish(v Var) {
	mutex.Lock()
	defer mutex.Unlock()

	vars = append(vars, v)
}

// Options holds options for the Run() function.
type Options struct {
	Interval time.Duration
}

// Run periodically calls the ValueList function of each Var, sets the Time and
// Interval fields and passes it w.Write(). This function blocks until the
// context is cancelled.
func Run(ctx context.Context, w api.Writer, opts Options) error {
	ticker := time.NewTicker(opts.Interval)

	for {
		select {
		case <-ticker.C:
			mutex.RLock()
			for _, v := range vars {
				vl := v.ValueList()
				vl.Time = time.Now()
				vl.Interval = opts.Interval
				if err := w.Write(ctx, vl); err != nil {
					mutex.RUnlock()
					return err
				}
			}
			mutex.RUnlock()
		case <-ctx.Done():<commit_msg>Package export: Don't return from Run() when Write() fails.<commit_after>/*
Package export provides an interface to instrument Go code.

Instrumenting Go code with this package is very similar to the "expvar" package
in the vanilla Go distribution. In fact, the variables exported with this
package are also registered with the "expvar" package, so that you can also use
other existing metric collection frameworks with it. This package differs in
that it has an explicitly cumulative type, Derive.

The intended usage pattern of this package is as follows: First, global
variables are initialized with NewDerive(), NewGauge(), NewDeriveString() or
NewGaugeString(). The Run() function is called as a separate goroutine as part
of your program's initialization and, last but not least, the variables are
updated with their respective update functions, Add() for Derive and Set() for
Gauge.

  // Initialize global variable.
  var requestCounter = export.NewDeriveString("example.com/golang/total_requests")

  // Call Run() in its own goroutine.
  func main() {
          client, err := network.Dial(
                  net.JoinHostPort(network.DefaultIPv6Address, network.DefaultService),
                  network.ClientOptions{})
          if err !=  nil {
                  log.Fatal(err)
                  }
          go export.Run(client, export.Options{
                  Interval: 10 * time.Second,
          })
          // …
  }

  // Update variable.
  func requestHandler(w http.ResponseWriter, req *http.Request) {
          defer requestCounter.Add(1)
          // …
  }
*/
package export // import "collectd.org/export"

import (
	"context"
	"expvar"
	"log"
	"math"
	"strconv"
	"sync"
	"time"

	"collectd.org/api"
)

var (
	mutex sync.RWMutex
	vars  []Var
)

// Var is an abstract type for metrics exported by this package.
type Var interface {
	ValueList() *api.ValueList
}

// Publish adds v to the internal list of exported metrics.
func Publish(v Var) {
	mutex.Lock()
	defer mutex.Unlock()

	vars = append(vars, v)
}

// Options holds options for the Run() function.
type Options struct {
	Interval time.Duration
}

// Run periodically calls the ValueList function of each Var, sets the Time and
// Interval fields and passes it w.Write(). This function blocks until the
// context is cancelled.
func Run(ctx context.Context, w api.Writer, opts Options) error {
	ticker := time.NewTicker(opts.Interval)

	for {
		select {
		case <-ticker.C:
			mutex.RLock()
			for _, v := range vars {
				vl := v.ValueList()
				vl.Time = time.Now()
				vl.Interval = opts.Interval
				if err := w.Write(ctx, vl); err != nil {
					log.Printf("%T.Write(): %v", w, err)
				}
			}
			mutex.RUnlock()
		case <-ctx.Done():
{'fraction_non_alphanumeric': 0.09846096792137957, 'fraction_numerical': 0.0014834044131281292, 'mean_word_length': 2.922909090909091, 'pattern_counts': {'":': 0, '<': 7, '<?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': '2831736', 'n_tokens_mistral': 1642, 'n_tokens_neox': 1529, 'n_words': 598}