<commit_before>package logtap

import (
	"encoding/json"
	"fmt"
	"log"
)

// Telemetry is the interface that represents a telemetry receiver.
type Telemetry interface {
	Value(value interface{}, name string)
	Count(value int, name string)
}

type discardTelemetry struct{}

// DiscardTelemetry discards telemetry data.
var DiscardTelemetry = &discardTelemetry{}

func (*discardTelemetry) Value(value interface{}, name string) {}
func (*discardTelemetry) Count(value int, name string)         {}

type logTelemetry struct{}

// LogTelemetry sends telemetry to the standard logger.
//
// Examples:
//
//     LogTelemetry.Count(33, "beans")  // prints `APP_METRIC {"stat": "beans", "count": 33}`
//     LogTelemetry.Value(36.6, "temp") // prints `APP_METRIC {"stat": "temp", "value": 36.6}`
var LogTelemetry = &logTelemetry{}

func printAppMetric(key string, value interface{}, name string) {
	var v, n []byte
	switch value.(type) {
	case float32, float64,
		int, int8, int16, int32, int64,
		uint, uint8, uint16, uint32, uint64:
		v, _ = json.Marshal(value)
	default:
		return
	}
	n, _ = json.Marshal(name)
	log.Print(fmt.Sprintf(`APP_METRIC {"stat": %s, "%s": %s}`, n, key, v))
}

func (*logTelemetry) Value(value interface{}, name string) {
	printAppMetric("value", value, name)
}

func (*logTelemetry) Count(value int, name string) {
	printAppMetric("count", value, name)
}<commit_msg>Telemetry: Additional property pairs may be passed to Count/Value methods.<commit_after>package logtap

import (
	"encoding/json"
	"fmt"
	"log"
)

// Telemetry is the interface that represents a telemetry receiver.
type Telemetry interface {
	Value(value interface{}, name string, props ...interface{})
	Count(value int, name string, props ...interface{})
}

type discardTelemetry struct{}

// DiscardTelemetry discards telemetry data.
var DiscardTelemetry = &discardTelemetry{}

func (*discardTelemetry) Value(value interface{}, name string, props ...interface{}) {}
func (*discardTelemetry) Count(value int, name string, props ...interface{})         {}

type logTelemetry struct{}

// LogTelemetry sends telemetry to the standard logger.
//
// Examples:
//
//     LogTelemetry.Count(33, "beans")  // prints `APP_METRIC {"stat": "beans", "count": 33}`
//     LogTelemetry.Value(36.6, "temp") // prints `APP_METRIC {"stat": "temp", "value": 36.6}`
var LogTelemetry = &logTelemetry{}

func printAppMetric(key string, value interface{}, name string) {
	var v, n []byte
	switch value.(type) {
	case float32, float64,
		int, int8, int16, int32, int64,
		uint, uint8, uint16, uint32, uint64:
		v, _ = json.Marshal(value)
	default:
		return
	}
	n, _ = json.Marshal(name)
	log.Print(fmt.Sprintf(`APP_METRIC {"stat": %s, "%s": %s}`, n, key, v))
}

func (*logTelemetry) Value(value interface{}, name string, props ...interface{}) {
	printAppMetric("value", value, name)
}

func (*logTelemetry) Count(value int, name string, props ...interface{}) {
	printAppMetric("count", value, name)
}
{'fraction_non_alphanumeric': 0.15345699831365936, 'fraction_numerical': 0.01888701517706577, 'mean_word_length': 4.5335820895522385, 'pattern_counts': {'":': 12, '<': 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': '1620378', 'n_tokens_mistral': 1008, 'n_tokens_neox': 970, 'n_words': 293}