2018-02-09 05:55:10 +03:00
|
|
|
// Copyright 2015 Light Code Labs, LLC
|
|
|
|
//
|
|
|
|
// Licensed under the Apache License, Version 2.0 (the "License");
|
|
|
|
// you may not use this file except in compliance with the License.
|
|
|
|
// You may obtain a copy of the License at
|
|
|
|
//
|
|
|
|
// http://www.apache.org/licenses/LICENSE-2.0
|
|
|
|
//
|
|
|
|
// Unless required by applicable law or agreed to in writing, software
|
|
|
|
// distributed under the License is distributed on an "AS IS" BASIS,
|
|
|
|
// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
|
|
|
|
// See the License for the specific language governing permissions and
|
|
|
|
// limitations under the License.
|
|
|
|
|
2018-03-23 03:05:31 +03:00
|
|
|
package telemetry
|
2018-02-09 05:55:10 +03:00
|
|
|
|
|
|
|
import (
|
|
|
|
"log"
|
2018-05-08 01:09:39 +03:00
|
|
|
"strings"
|
2018-02-09 05:55:10 +03:00
|
|
|
|
|
|
|
"github.com/google/uuid"
|
|
|
|
)
|
|
|
|
|
|
|
|
// Init initializes this package so that it may
|
|
|
|
// be used. Do not call this function more than
|
|
|
|
// once. Init panics if it is called more than
|
|
|
|
// once or if the UUID value is empty. Once this
|
|
|
|
// function is called, the rest of the package
|
|
|
|
// may safely be used. If this function is not
|
|
|
|
// called, the collector functions may still be
|
|
|
|
// invoked, but they will be no-ops.
|
2018-03-24 08:44:16 +03:00
|
|
|
//
|
|
|
|
// Any metrics keys that are passed in the second
|
|
|
|
// argument will be permanently disabled for the
|
|
|
|
// lifetime of the process.
|
|
|
|
func Init(instanceID uuid.UUID, disabledMetricsKeys []string) {
|
2018-02-09 05:55:10 +03:00
|
|
|
if enabled {
|
|
|
|
panic("already initialized")
|
|
|
|
}
|
2018-02-09 07:15:28 +03:00
|
|
|
if str := instanceID.String(); str == "" ||
|
2018-03-22 02:01:14 +03:00
|
|
|
str == "00000000-0000-0000-0000-000000000000" {
|
2018-02-09 05:55:10 +03:00
|
|
|
panic("empty UUID")
|
|
|
|
}
|
|
|
|
instanceUUID = instanceID
|
2018-03-24 08:44:16 +03:00
|
|
|
disabledMetricsMu.Lock()
|
|
|
|
for _, key := range disabledMetricsKeys {
|
|
|
|
disabledMetrics[key] = false
|
|
|
|
}
|
|
|
|
disabledMetricsMu.Unlock()
|
2018-02-09 05:55:10 +03:00
|
|
|
enabled = true
|
|
|
|
}
|
|
|
|
|
|
|
|
// StartEmitting sends the current payload and begins the
|
|
|
|
// transmission cycle for updates. This is the first
|
|
|
|
// update sent, and future ones will be sent until
|
|
|
|
// StopEmitting is called.
|
|
|
|
//
|
|
|
|
// This function is non-blocking (it spawns a new goroutine).
|
|
|
|
//
|
|
|
|
// This function panics if it was called more than once.
|
|
|
|
// It is a no-op if this package was not initialized.
|
|
|
|
func StartEmitting() {
|
|
|
|
if !enabled {
|
|
|
|
return
|
|
|
|
}
|
|
|
|
updateTimerMu.Lock()
|
|
|
|
if updateTimer != nil {
|
|
|
|
updateTimerMu.Unlock()
|
|
|
|
panic("updates already started")
|
|
|
|
}
|
|
|
|
updateTimerMu.Unlock()
|
|
|
|
updateMu.Lock()
|
|
|
|
if updating {
|
|
|
|
updateMu.Unlock()
|
|
|
|
panic("update already in progress")
|
|
|
|
}
|
|
|
|
updateMu.Unlock()
|
|
|
|
go logEmit(false)
|
|
|
|
}
|
|
|
|
|
|
|
|
// StopEmitting sends the current payload and terminates
|
|
|
|
// the update cycle. No more updates will be sent.
|
|
|
|
//
|
|
|
|
// It is a no-op if the package was never initialized
|
|
|
|
// or if emitting was never started.
|
2018-03-22 02:01:14 +03:00
|
|
|
//
|
|
|
|
// NOTE: This function is blocking. Run in a goroutine if
|
|
|
|
// you want to guarantee no blocking at critical times
|
|
|
|
// like exiting the program.
|
2018-02-09 05:55:10 +03:00
|
|
|
func StopEmitting() {
|
|
|
|
if !enabled {
|
|
|
|
return
|
|
|
|
}
|
|
|
|
updateTimerMu.Lock()
|
|
|
|
if updateTimer == nil {
|
|
|
|
updateTimerMu.Unlock()
|
|
|
|
return
|
|
|
|
}
|
|
|
|
updateTimerMu.Unlock()
|
2018-03-22 02:01:14 +03:00
|
|
|
logEmit(true) // likely too early; may take minutes to return
|
|
|
|
}
|
|
|
|
|
|
|
|
// Reset empties the current payload buffer.
|
|
|
|
func Reset() {
|
|
|
|
resetBuffer()
|
2018-02-09 05:55:10 +03:00
|
|
|
}
|
|
|
|
|
|
|
|
// Set puts a value in the buffer to be included
|
|
|
|
// in the next emission. It overwrites any
|
|
|
|
// previous value.
|
|
|
|
//
|
|
|
|
// This function is safe for multiple goroutines,
|
|
|
|
// and it is recommended to call this using the
|
|
|
|
// go keyword after the call to SendHello so it
|
|
|
|
// doesn't block crucial code.
|
|
|
|
func Set(key string, val interface{}) {
|
2018-03-23 04:50:38 +03:00
|
|
|
if !enabled || isDisabled(key) {
|
2018-02-09 05:55:10 +03:00
|
|
|
return
|
|
|
|
}
|
|
|
|
bufferMu.Lock()
|
|
|
|
if _, ok := buffer[key]; !ok {
|
2018-05-08 01:09:39 +03:00
|
|
|
if bufferItemCount >= maxBufferItems {
|
|
|
|
bufferMu.Unlock()
|
|
|
|
return
|
|
|
|
}
|
2018-02-09 05:55:10 +03:00
|
|
|
bufferItemCount++
|
|
|
|
}
|
|
|
|
buffer[key] = val
|
|
|
|
bufferMu.Unlock()
|
|
|
|
}
|
|
|
|
|
2018-05-08 01:09:39 +03:00
|
|
|
// SetNested puts a value in the buffer to be included
|
|
|
|
// in the next emission, nested under the top-level key
|
|
|
|
// as subkey. It overwrites any previous value.
|
|
|
|
//
|
|
|
|
// This function is safe for multiple goroutines,
|
|
|
|
// and it is recommended to call this using the
|
|
|
|
// go keyword after the call to SendHello so it
|
|
|
|
// doesn't block crucial code.
|
|
|
|
func SetNested(key, subkey string, val interface{}) {
|
|
|
|
if !enabled || isDisabled(key) {
|
|
|
|
return
|
|
|
|
}
|
|
|
|
bufferMu.Lock()
|
|
|
|
if topLevel, ok1 := buffer[key]; ok1 {
|
|
|
|
topLevelMap, ok2 := topLevel.(map[string]interface{})
|
|
|
|
if !ok2 {
|
|
|
|
bufferMu.Unlock()
|
|
|
|
log.Printf("[PANIC] Telemetry: key %s is already used for non-nested-map value", key)
|
|
|
|
return
|
|
|
|
}
|
|
|
|
if _, ok3 := topLevelMap[subkey]; !ok3 {
|
|
|
|
// don't exceed max buffer size
|
|
|
|
if bufferItemCount >= maxBufferItems {
|
|
|
|
bufferMu.Unlock()
|
|
|
|
return
|
|
|
|
}
|
|
|
|
bufferItemCount++
|
|
|
|
}
|
|
|
|
topLevelMap[subkey] = val
|
|
|
|
} else {
|
|
|
|
// don't exceed max buffer size
|
|
|
|
if bufferItemCount >= maxBufferItems {
|
|
|
|
bufferMu.Unlock()
|
|
|
|
return
|
|
|
|
}
|
|
|
|
bufferItemCount++
|
|
|
|
buffer[key] = map[string]interface{}{subkey: val}
|
|
|
|
}
|
|
|
|
bufferMu.Unlock()
|
|
|
|
}
|
|
|
|
|
2018-02-09 05:55:10 +03:00
|
|
|
// Append appends value to a list named key.
|
|
|
|
// If key is new, a new list will be created.
|
|
|
|
// If key maps to a type that is not a list,
|
2018-02-10 22:59:23 +03:00
|
|
|
// a panic is logged, and this is a no-op.
|
2018-02-09 05:55:10 +03:00
|
|
|
func Append(key string, value interface{}) {
|
2018-03-23 04:50:38 +03:00
|
|
|
if !enabled || isDisabled(key) {
|
2018-02-09 05:55:10 +03:00
|
|
|
return
|
|
|
|
}
|
|
|
|
bufferMu.Lock()
|
|
|
|
if bufferItemCount >= maxBufferItems {
|
|
|
|
bufferMu.Unlock()
|
|
|
|
return
|
|
|
|
}
|
|
|
|
// TODO: Test this...
|
|
|
|
bufVal, inBuffer := buffer[key]
|
|
|
|
sliceVal, sliceOk := bufVal.([]interface{})
|
|
|
|
if inBuffer && !sliceOk {
|
|
|
|
bufferMu.Unlock()
|
2018-03-23 03:05:31 +03:00
|
|
|
log.Printf("[PANIC] Telemetry: key %s already used for non-slice value", key)
|
2018-02-09 05:55:10 +03:00
|
|
|
return
|
|
|
|
}
|
|
|
|
if sliceVal == nil {
|
|
|
|
buffer[key] = []interface{}{value}
|
|
|
|
} else if sliceOk {
|
|
|
|
buffer[key] = append(sliceVal, value)
|
|
|
|
}
|
|
|
|
bufferItemCount++
|
|
|
|
bufferMu.Unlock()
|
|
|
|
}
|
|
|
|
|
2018-03-22 02:01:14 +03:00
|
|
|
// AppendUnique adds value to a set named key.
|
2018-02-09 05:55:10 +03:00
|
|
|
// Set items are unordered. Values in the set
|
2018-02-10 22:59:23 +03:00
|
|
|
// are unique, but how many times they are
|
2018-05-08 01:09:39 +03:00
|
|
|
// appended is counted. The value must be
|
|
|
|
// hashable.
|
2018-02-09 05:55:10 +03:00
|
|
|
//
|
2018-02-10 22:59:23 +03:00
|
|
|
// If key is new, a new set will be created for
|
|
|
|
// values with that key. If key maps to a type
|
|
|
|
// that is not a counting set, a panic is logged,
|
|
|
|
// and this is a no-op.
|
|
|
|
func AppendUnique(key string, value interface{}) {
|
2018-03-23 04:50:38 +03:00
|
|
|
if !enabled || isDisabled(key) {
|
2018-02-09 05:55:10 +03:00
|
|
|
return
|
|
|
|
}
|
|
|
|
bufferMu.Lock()
|
|
|
|
bufVal, inBuffer := buffer[key]
|
2018-02-10 22:59:23 +03:00
|
|
|
setVal, setOk := bufVal.(countingSet)
|
|
|
|
if inBuffer && !setOk {
|
2018-02-09 05:55:10 +03:00
|
|
|
bufferMu.Unlock()
|
2018-03-23 03:05:31 +03:00
|
|
|
log.Printf("[PANIC] Telemetry: key %s already used for non-counting-set value", key)
|
2018-02-09 05:55:10 +03:00
|
|
|
return
|
|
|
|
}
|
2018-02-10 22:59:23 +03:00
|
|
|
if setVal == nil {
|
|
|
|
// ensure the buffer is not too full, then add new unique value
|
|
|
|
if bufferItemCount >= maxBufferItems {
|
|
|
|
bufferMu.Unlock()
|
|
|
|
return
|
|
|
|
}
|
|
|
|
buffer[key] = countingSet{value: 1}
|
2018-02-09 05:55:10 +03:00
|
|
|
bufferItemCount++
|
2018-02-10 22:59:23 +03:00
|
|
|
} else if setOk {
|
|
|
|
// unique value already exists, so just increment counter
|
|
|
|
setVal[value]++
|
2018-02-09 05:55:10 +03:00
|
|
|
}
|
|
|
|
bufferMu.Unlock()
|
|
|
|
}
|
|
|
|
|
2018-03-22 02:01:14 +03:00
|
|
|
// Add adds amount to a value named key.
|
2018-02-09 05:55:10 +03:00
|
|
|
// If it does not exist, it is created with
|
|
|
|
// a value of 1. If key maps to a type that
|
2018-02-10 22:59:23 +03:00
|
|
|
// is not an integer, a panic is logged,
|
2018-02-09 05:55:10 +03:00
|
|
|
// and this is a no-op.
|
2018-03-22 02:01:14 +03:00
|
|
|
func Add(key string, amount int) {
|
|
|
|
atomicAdd(key, amount)
|
2018-02-09 05:55:10 +03:00
|
|
|
}
|
|
|
|
|
2018-03-22 02:01:14 +03:00
|
|
|
// Increment is a shortcut for Add(key, 1)
|
|
|
|
func Increment(key string) {
|
|
|
|
atomicAdd(key, 1)
|
2018-02-09 05:55:10 +03:00
|
|
|
}
|
|
|
|
|
2018-03-22 02:01:14 +03:00
|
|
|
// atomicAdd adds amount (negative to subtract)
|
|
|
|
// to key.
|
|
|
|
func atomicAdd(key string, amount int) {
|
2018-03-23 04:50:38 +03:00
|
|
|
if !enabled || isDisabled(key) {
|
2018-02-09 05:55:10 +03:00
|
|
|
return
|
|
|
|
}
|
|
|
|
bufferMu.Lock()
|
|
|
|
bufVal, inBuffer := buffer[key]
|
|
|
|
intVal, intOk := bufVal.(int)
|
|
|
|
if inBuffer && !intOk {
|
|
|
|
bufferMu.Unlock()
|
2018-03-23 03:05:31 +03:00
|
|
|
log.Printf("[PANIC] Telemetry: key %s already used for non-integer value", key)
|
2018-02-09 05:55:10 +03:00
|
|
|
return
|
|
|
|
}
|
|
|
|
if !inBuffer {
|
|
|
|
if bufferItemCount >= maxBufferItems {
|
|
|
|
bufferMu.Unlock()
|
|
|
|
return
|
|
|
|
}
|
|
|
|
bufferItemCount++
|
|
|
|
}
|
2018-03-22 02:01:14 +03:00
|
|
|
buffer[key] = intVal + amount
|
2018-02-09 05:55:10 +03:00
|
|
|
bufferMu.Unlock()
|
|
|
|
}
|
2018-03-23 04:50:38 +03:00
|
|
|
|
|
|
|
// isDisabled returns whether key is
|
|
|
|
// a disabled metric key. ALL collection
|
|
|
|
// functions should call this and not
|
|
|
|
// save the value if this returns true.
|
|
|
|
func isDisabled(key string) bool {
|
2018-05-08 01:09:39 +03:00
|
|
|
// for keys that are augmented with data, such as
|
|
|
|
// "tls_client_hello_ua:<hash>", just
|
|
|
|
// check the prefix "tls_client_hello_ua"
|
|
|
|
checkKey := key
|
|
|
|
if idx := strings.Index(key, ":"); idx > -1 {
|
|
|
|
checkKey = key[:idx]
|
|
|
|
}
|
|
|
|
|
2018-03-23 04:50:38 +03:00
|
|
|
disabledMetricsMu.RLock()
|
2018-05-08 01:09:39 +03:00
|
|
|
_, ok := disabledMetrics[checkKey]
|
2018-03-23 04:50:38 +03:00
|
|
|
disabledMetricsMu.RUnlock()
|
|
|
|
return ok
|
|
|
|
}
|