<commit_before>package metrics

import (
	"errors"
	"fmt"
	"net/http"
	"time"
)

// Backends holds a map of registered metrics backends.
var Backends = map[string]Interface{}

// current is the current selected backend.
var current Interface = nil

// servicePrefix is prefixed to all event service names.
var servicePrefix = ""

// Event represents an event which can be published to
// a backend.
type Event struct {
	State      string
	Host       string
	Service    string
	HttpStatus int
	Metric     int64
	Ttl        float32
	Tags       []string
	Transient  bool
	Attributes map[string]interface{}
}

// SetAttr sets an attribute with the given key and value.
// The value must be a string or a boolean.
func (e *Event) SetAttr(k string, v interface{}) {
	if e.Attributes == nil {
		e.Attributes = map[string]interface{}{}
	}
	e.Attributes[k] = v
}

// Interface implements the Publish method.
// It is the type of all metrics backends.
type Interface interface {
	Publish(e ...*Event) error
}

// Register registers a backend with the metrics library.
func Register(name string, m Interface) {
	Backends[name] = m
}

// Use selects the metrics backend to use for Publishing.
func Use(name string) error {
	if _, ok := Backends[name]; !ok {
		return errors.New("backend not found")
	}
	current = Backends[name]
	return nil
}

// Publish publishes one or more events to the current backend.
func Publish(evs ...*Event) error {
	if current != nil {
		for _, e := range evs {
			// TODO Make copy of event
			e.Service = servicePrefix + e.Service

			err := current.Publish(e)
			if err != nil {
				return fmt.Errorf("error publishing metric '%s': %s", e.Service, err)
			}
		}
	}
	return nil
}

// PublishHttpAccess publishes an HTTP access to the current backend.
func PublishHttpAccess(r *http.Request, d time.Duration, status int) error {
	return Publish(
		&Event{
			Service:   "inbound.timings",
			Tags:      []string{"http", "inbound", "percentiles"},
			Metric:    int64(d / time.Millisecond),
			Transient: true,
		},
		&Event{
			Service:    "outbound",
			HttpStatus: status,
			Tags:       []string{"http", "outbound", "rate"},
			Metric:     1,
			Transient:  true,
		},
	)
}

// SetPrefix sets the event service prefix.
// Use this to prefix all events with the name of the service
// generating the events.
func SetPrefix(pre string) {
	servicePrefix = pre
}<commit_msg>State & Host<commit_after>package metrics

import (
	"errors"
	"flag"
	"fmt"
	"net/http"
	"os"
	"time"
)

// Backends holds a map of registered metrics backends.
var Backends = map[string]Interface{}

// current is the current selected backend.
var current Interface = nil

// servicePrefix is prefixed to all event service names.
var servicePrefix = ""

// defaultHost is the value for "host" property of events.
var defaultHost *string

// Event represents an event which can be published to
// a backend.
type Event struct {
	State      string
	Host       string
	Service    string
	HttpStatus int
	Metric     int64
	Ttl        float32
	Tags       []string
	Transient  bool
	Attributes map[string]interface{}
}

// SetAttr sets an attribute with the given key and value.
// The value must be a string or a boolean.
func (e *Event) SetAttr(k string, v interface{}) {
	if e.Attributes == nil {
		e.Attributes = map[string]interface{}{}
	}
	e.Attributes[k] = v
}

// Interface implements the Publish method.
// It is the type of all metrics backends.
type Interface interface {
	Publish(e ...*Event) error
}

// Register registers a backend with the metrics library.
func Register(name string, m Interface) {
	Backends[name] = m
}

// Use selects the metrics backend to use for Publishing.
func Use(name string) error {
	if _, ok := Backends[name]; !ok {
		return errors.New("backend not found")
	}
	current = Backends[name]
	return nil
}

// Publish publishes one or more events to the current backend.
func Publish(evs ...*Event) error {
	if current != nil {
		for _, e := range evs {
			// TODO Make copy of event
			e.Service = servicePrefix + e.Service

			if e.Host == "" {
				e.Host = *defaultHost
			}

			err := current.Publish(e)
			if err != nil {
				return fmt.Errorf("error publishing metric '%s': %s", e.Service, err)
			}
		}
	}
	return nil
}

// PublishHttpAccess publishes an HTTP access to the current backend.
func PublishHttpAccess(r *http.Request, d time.Duration, status int) error {
	state := "ok"

	if status == http.StatusInternalServerError {
		state = "critical"
	}

	return Publish(
		&Event{
			Service:   "inbound.timings",
			State:     state,
			Tags:      []string{"http", "inbound", "percentiles"},
			Metric:    int64(d / time.Millisecond),
			Transient: true,
		},
		&Event{
			Service:    "outbound",
			State:      state,
			HttpStatus: status,
			Tags:       []string{"http", "outbound", "rate"},
			Metric:     1,
			Transient:  true,
		},
	)
}

// SetPrefix sets the event service prefix.
// Use this to prefix all events with the name of the service
// generating the events.
func SetPrefix(pre string) {
	servicePrefix = pre
}

// SetDefaultHost sets the default value of the host property for all events.
func SetDefaultHost(h string) {
	defaultHost = &h
}

func init() {
	host, err := os.Hostname()
	if err != nil {
		panic(err)
	}
	defaultHost = flag.String("metrics.host", host, "hostname to use in events")
}
{'fraction_non_alphanumeric': 0.11125654450261781, 'fraction_numerical': 0.002617801047120419, 'mean_word_length': 3.156177156177156, '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': '163651', 'n_tokens_mistral': 1764, 'n_tokens_neox': 1658, 'n_words': 616}