2018-02-09 10:55:10 +08: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 08:05:31 +08:00
|
|
|
package telemetry
|
2018-02-09 10:55:10 +08:00
|
|
|
|
|
|
|
import (
|
|
|
|
"log"
|
|
|
|
|
|
|
|
"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.
|
|
|
|
func Init(instanceID uuid.UUID) {
|
|
|
|
if enabled {
|
|
|
|
panic("already initialized")
|
|
|
|
}
|
2018-02-09 12:15:28 +08:00
|
|
|
if str := instanceID.String(); str == "" ||
|
2018-03-22 07:01:14 +08:00
|
|
|
str == "00000000-0000-0000-0000-000000000000" {
|
2018-02-09 10:55:10 +08:00
|
|
|
panic("empty UUID")
|
|
|
|
}
|
|
|
|
instanceUUID = instanceID
|
|
|
|
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 07:01:14 +08: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 10:55:10 +08:00
|
|
|
func StopEmitting() {
|
|
|
|
if !enabled {
|
|
|
|
return
|
|
|
|
}
|
|
|
|
updateTimerMu.Lock()
|
|
|
|
if updateTimer == nil {
|
|
|
|
updateTimerMu.Unlock()
|
|
|
|
return
|
|
|
|
}
|
|
|
|
updateTimerMu.Unlock()
|
2018-03-22 07:01:14 +08:00
|
|
|
logEmit(true) // likely too early; may take minutes to return
|
|
|
|
}
|
|
|
|
|
|
|
|
// Reset empties the current payload buffer.
|
|
|
|
func Reset() {
|
|
|
|
resetBuffer()
|
2018-02-09 10:55:10 +08: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 09:50:38 +08:00
|
|
|
if !enabled || isDisabled(key) {
|
2018-02-09 10:55:10 +08:00
|
|
|
return
|
|
|
|
}
|
|
|
|
bufferMu.Lock()
|
|
|
|
if bufferItemCount >= maxBufferItems {
|
|
|
|
bufferMu.Unlock()
|
|
|
|
return
|
|
|
|
}
|
|
|
|
if _, ok := buffer[key]; !ok {
|
|
|
|
bufferItemCount++
|
|
|
|
}
|
|
|
|
buffer[key] = val
|
|
|
|
bufferMu.Unlock()
|
|
|
|
}
|
|
|
|
|
|
|
|
// 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-11 03:59:23 +08:00
|
|
|
// a panic is logged, and this is a no-op.
|
2018-02-09 10:55:10 +08:00
|
|
|
func Append(key string, value interface{}) {
|
2018-03-23 09:50:38 +08:00
|
|
|
if !enabled || isDisabled(key) {
|
2018-02-09 10:55:10 +08: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 08:05:31 +08:00
|
|
|
log.Printf("[PANIC] Telemetry: key %s already used for non-slice value", key)
|
2018-02-09 10:55:10 +08:00
|
|
|
return
|
|
|
|
}
|
|
|
|
if sliceVal == nil {
|
|
|
|
buffer[key] = []interface{}{value}
|
|
|
|
} else if sliceOk {
|
|
|
|
buffer[key] = append(sliceVal, value)
|
|
|
|
}
|
|
|
|
bufferItemCount++
|
|
|
|
bufferMu.Unlock()
|
|
|
|
}
|
|
|
|
|
2018-03-22 07:01:14 +08:00
|
|
|
// AppendUnique adds value to a set named key.
|
2018-02-09 10:55:10 +08:00
|
|
|
// Set items are unordered. Values in the set
|
2018-02-11 03:59:23 +08:00
|
|
|
// are unique, but how many times they are
|
|
|
|
// appended is counted.
|
2018-02-09 10:55:10 +08:00
|
|
|
//
|
2018-02-11 03:59:23 +08: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 09:50:38 +08:00
|
|
|
if !enabled || isDisabled(key) {
|
2018-02-09 10:55:10 +08:00
|
|
|
return
|
|
|
|
}
|
|
|
|
bufferMu.Lock()
|
|
|
|
bufVal, inBuffer := buffer[key]
|
2018-02-11 03:59:23 +08:00
|
|
|
setVal, setOk := bufVal.(countingSet)
|
|
|
|
if inBuffer && !setOk {
|
2018-02-09 10:55:10 +08:00
|
|
|
bufferMu.Unlock()
|
2018-03-23 08:05:31 +08:00
|
|
|
log.Printf("[PANIC] Telemetry: key %s already used for non-counting-set value", key)
|
2018-02-09 10:55:10 +08:00
|
|
|
return
|
|
|
|
}
|
2018-02-11 03:59:23 +08: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 10:55:10 +08:00
|
|
|
bufferItemCount++
|
2018-02-11 03:59:23 +08:00
|
|
|
} else if setOk {
|
|
|
|
// unique value already exists, so just increment counter
|
|
|
|
setVal[value]++
|
2018-02-09 10:55:10 +08:00
|
|
|
}
|
|
|
|
bufferMu.Unlock()
|
|
|
|
}
|
|
|
|
|
2018-03-22 07:01:14 +08:00
|
|
|
// Add adds amount to a value named key.
|
2018-02-09 10:55:10 +08:00
|
|
|
// If it does not exist, it is created with
|
|
|
|
// a value of 1. If key maps to a type that
|
2018-02-11 03:59:23 +08:00
|
|
|
// is not an integer, a panic is logged,
|
2018-02-09 10:55:10 +08:00
|
|
|
// and this is a no-op.
|
2018-03-22 07:01:14 +08:00
|
|
|
func Add(key string, amount int) {
|
|
|
|
atomicAdd(key, amount)
|
2018-02-09 10:55:10 +08:00
|
|
|
}
|
|
|
|
|
2018-03-22 07:01:14 +08:00
|
|
|
// Increment is a shortcut for Add(key, 1)
|
|
|
|
func Increment(key string) {
|
|
|
|
atomicAdd(key, 1)
|
2018-02-09 10:55:10 +08:00
|
|
|
}
|
|
|
|
|
2018-03-22 07:01:14 +08:00
|
|
|
// atomicAdd adds amount (negative to subtract)
|
|
|
|
// to key.
|
|
|
|
func atomicAdd(key string, amount int) {
|
2018-03-23 09:50:38 +08:00
|
|
|
if !enabled || isDisabled(key) {
|
2018-02-09 10:55:10 +08:00
|
|
|
return
|
|
|
|
}
|
|
|
|
bufferMu.Lock()
|
|
|
|
bufVal, inBuffer := buffer[key]
|
|
|
|
intVal, intOk := bufVal.(int)
|
|
|
|
if inBuffer && !intOk {
|
|
|
|
bufferMu.Unlock()
|
2018-03-23 08:05:31 +08:00
|
|
|
log.Printf("[PANIC] Telemetry: key %s already used for non-integer value", key)
|
2018-02-09 10:55:10 +08:00
|
|
|
return
|
|
|
|
}
|
|
|
|
if !inBuffer {
|
|
|
|
if bufferItemCount >= maxBufferItems {
|
|
|
|
bufferMu.Unlock()
|
|
|
|
return
|
|
|
|
}
|
|
|
|
bufferItemCount++
|
|
|
|
}
|
2018-03-22 07:01:14 +08:00
|
|
|
buffer[key] = intVal + amount
|
2018-02-09 10:55:10 +08:00
|
|
|
bufferMu.Unlock()
|
|
|
|
}
|
2018-03-23 09:50:38 +08: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 {
|
|
|
|
disabledMetricsMu.RLock()
|
|
|
|
_, ok := disabledMetrics[key]
|
|
|
|
disabledMetricsMu.RUnlock()
|
|
|
|
return ok
|
|
|
|
}
|