mirror of
https://github.com/kubeshark/kubeshark.git
synced 2025-06-24 07:14:15 +00:00
Add telemetry to config (#152)
This commit is contained in:
parent
71eff5ea04
commit
0824524d62
@ -21,13 +21,13 @@ type CommandLineFlag struct {
|
|||||||
CommandLineName string
|
CommandLineName string
|
||||||
YamlHierarchyName string
|
YamlHierarchyName string
|
||||||
DefaultValue interface{}
|
DefaultValue interface{}
|
||||||
Type reflect.Kind
|
|
||||||
}
|
}
|
||||||
|
|
||||||
const (
|
const (
|
||||||
ConfigurationKeyAnalyzingDestination = "tap.dest"
|
ConfigurationKeyAnalyzingDestination = "tap.dest"
|
||||||
ConfigurationKeyUploadInterval = "tap.uploadInterval"
|
ConfigurationKeyUploadInterval = "tap.uploadInterval"
|
||||||
ConfigurationKeyMizuImage = "mizuImage"
|
ConfigurationKeyMizuImage = "mizuImage"
|
||||||
|
ConfigurationKeyTelemetry = "telemetry"
|
||||||
)
|
)
|
||||||
|
|
||||||
var allowedSetFlags = []CommandLineFlag{
|
var allowedSetFlags = []CommandLineFlag{
|
||||||
@ -35,20 +35,22 @@ var allowedSetFlags = []CommandLineFlag{
|
|||||||
CommandLineName: "dest",
|
CommandLineName: "dest",
|
||||||
YamlHierarchyName: ConfigurationKeyAnalyzingDestination,
|
YamlHierarchyName: ConfigurationKeyAnalyzingDestination,
|
||||||
DefaultValue: "up9.app",
|
DefaultValue: "up9.app",
|
||||||
Type: reflect.String,
|
|
||||||
// TODO: maybe add short description that we can show
|
// TODO: maybe add short description that we can show
|
||||||
},
|
},
|
||||||
{
|
{
|
||||||
CommandLineName: "uploadInterval",
|
CommandLineName: "uploadInterval",
|
||||||
YamlHierarchyName: ConfigurationKeyUploadInterval,
|
YamlHierarchyName: ConfigurationKeyUploadInterval,
|
||||||
DefaultValue: 10,
|
DefaultValue: 10,
|
||||||
Type: reflect.Int,
|
|
||||||
},
|
},
|
||||||
{
|
{
|
||||||
CommandLineName: "mizuImage",
|
CommandLineName: "mizuImage",
|
||||||
YamlHierarchyName: ConfigurationKeyMizuImage,
|
YamlHierarchyName: ConfigurationKeyMizuImage,
|
||||||
DefaultValue: fmt.Sprintf("gcr.io/up9-docker-hub/mizu/%s:%s", Branch, SemVer),
|
DefaultValue: fmt.Sprintf("gcr.io/up9-docker-hub/mizu/%s:%s", Branch, SemVer),
|
||||||
Type: reflect.String,
|
},
|
||||||
|
{
|
||||||
|
CommandLineName: "telemetry",
|
||||||
|
YamlHierarchyName: ConfigurationKeyTelemetry,
|
||||||
|
DefaultValue: true,
|
||||||
},
|
},
|
||||||
}
|
}
|
||||||
|
|
||||||
@ -56,13 +58,25 @@ func GetString(key string) string {
|
|||||||
return fmt.Sprintf("%v", getValueFromMergedConfig(key))
|
return fmt.Sprintf("%v", getValueFromMergedConfig(key))
|
||||||
}
|
}
|
||||||
|
|
||||||
|
func GetBool(key string) bool {
|
||||||
|
stringVal := GetString(key)
|
||||||
|
Log.Debugf("Found string value %v", stringVal)
|
||||||
|
|
||||||
|
val, err := strconv.ParseBool(stringVal)
|
||||||
|
if err != nil {
|
||||||
|
Log.Warningf(uiUtils.Red, fmt.Sprintf( "Invalid value %v for key %s, expected bool", stringVal, key))
|
||||||
|
os.Exit(1)
|
||||||
|
}
|
||||||
|
return val
|
||||||
|
}
|
||||||
|
|
||||||
func GetInt(key string) int {
|
func GetInt(key string) int {
|
||||||
stringVal := GetString(key)
|
stringVal := GetString(key)
|
||||||
Log.Debugf("Found string value %v", stringVal)
|
Log.Debugf("Found string value %v", stringVal)
|
||||||
|
|
||||||
val, err := strconv.Atoi(stringVal)
|
val, err := strconv.Atoi(stringVal)
|
||||||
if err != nil {
|
if err != nil {
|
||||||
Log.Warningf("Invalid value %v for key %s", val, key)
|
Log.Warningf(uiUtils.Red, fmt.Sprintf("Invalid value %v for key %s, expected int", stringVal, key))
|
||||||
os.Exit(1)
|
os.Exit(1)
|
||||||
}
|
}
|
||||||
return val
|
return val
|
||||||
@ -140,13 +154,13 @@ func mergeConfigFile() error {
|
|||||||
|
|
||||||
func addToConfig(prefix string, value interface{}) {
|
func addToConfig(prefix string, value interface{}) {
|
||||||
typ := reflect.TypeOf(value).Kind()
|
typ := reflect.TypeOf(value).Kind()
|
||||||
if typ == reflect.Int || typ == reflect.String || typ == reflect.Slice {
|
if typ == reflect.Map {
|
||||||
validateConfigFileKey(prefix)
|
|
||||||
configObj[prefix] = value
|
|
||||||
} else if typ == reflect.Map {
|
|
||||||
for k1, v1 := range value.(map[string]interface{}) {
|
for k1, v1 := range value.(map[string]interface{}) {
|
||||||
addToConfig(fmt.Sprintf("%s.%s", prefix, k1), v1)
|
addToConfig(fmt.Sprintf("%s.%s", prefix, k1), v1)
|
||||||
}
|
}
|
||||||
|
} else {
|
||||||
|
validateConfigFileKey(prefix)
|
||||||
|
configObj[prefix] = value
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
@ -161,26 +175,23 @@ func mergeCommandLineFlags(commandLineValues []string) error {
|
|||||||
return errors.New(fmt.Sprintf("invalid set argument %s", e))
|
return errors.New(fmt.Sprintf("invalid set argument %s", e))
|
||||||
}
|
}
|
||||||
setFlagKey, argumentValue := split[0], split[1]
|
setFlagKey, argumentValue := split[0], split[1]
|
||||||
argumentNameInConfig, expectedType, err := flagFromAllowed(setFlagKey)
|
argumentNameInConfig, err := flagFromAllowed(setFlagKey)
|
||||||
if err != nil {
|
if err != nil {
|
||||||
return err
|
return err
|
||||||
}
|
}
|
||||||
argumentType := reflect.ValueOf(argumentValue).Kind()
|
|
||||||
if argumentType != expectedType {
|
|
||||||
return errors.New(fmt.Sprintf("Invalid value for argument %s (should be type %s but got %s", setFlagKey, expectedType, argumentType))
|
|
||||||
}
|
|
||||||
configObj[argumentNameInConfig] = argumentValue
|
configObj[argumentNameInConfig] = argumentValue
|
||||||
}
|
}
|
||||||
return nil
|
return nil
|
||||||
}
|
}
|
||||||
|
|
||||||
func flagFromAllowed(setFlagKey string) (string, reflect.Kind, error) {
|
func flagFromAllowed(setFlagKey string) (string, error) {
|
||||||
for _, allowedFlag := range allowedSetFlags {
|
for _, allowedFlag := range allowedSetFlags {
|
||||||
if strings.ToLower(allowedFlag.CommandLineName) == strings.ToLower(setFlagKey) {
|
if strings.ToLower(allowedFlag.CommandLineName) == strings.ToLower(setFlagKey) {
|
||||||
return allowedFlag.YamlHierarchyName, allowedFlag.Type, nil
|
return allowedFlag.YamlHierarchyName, nil
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
return "", reflect.Invalid, errors.New(fmt.Sprintf("invalid set argument %s", setFlagKey))
|
return "", errors.New(fmt.Sprintf("invalid set argument %s", setFlagKey))
|
||||||
}
|
}
|
||||||
|
|
||||||
func validateConfigFileKey(configFileKey string) {
|
func validateConfigFileKey(configFileKey string) {
|
||||||
@ -195,7 +206,7 @@ func validateConfigFileKey(configFileKey string) {
|
|||||||
|
|
||||||
func addToConfigObj(key string, value interface{}, configObj map[string]interface{}) {
|
func addToConfigObj(key string, value interface{}, configObj map[string]interface{}) {
|
||||||
typ := reflect.TypeOf(value).Kind()
|
typ := reflect.TypeOf(value).Kind()
|
||||||
if typ == reflect.Int || typ == reflect.String || typ == reflect.Slice {
|
if typ != reflect.Map {
|
||||||
if strings.Contains(key, ".") {
|
if strings.Contains(key, ".") {
|
||||||
split := strings.SplitN(key, ".", 2)
|
split := strings.SplitN(key, ".", 2)
|
||||||
firstLevelKey := split[0]
|
firstLevelKey := split[0]
|
||||||
@ -208,4 +219,3 @@ func addToConfigObj(key string, value interface{}, configObj map[string]interfac
|
|||||||
}
|
}
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
@ -10,6 +10,11 @@ import (
|
|||||||
const telemetryUrl = "https://us-east4-up9-prod.cloudfunctions.net/mizu-telemetry"
|
const telemetryUrl = "https://us-east4-up9-prod.cloudfunctions.net/mizu-telemetry"
|
||||||
|
|
||||||
func ReportRun(cmd string, args interface{}) {
|
func ReportRun(cmd string, args interface{}) {
|
||||||
|
if !GetBool(ConfigurationKeyTelemetry) {
|
||||||
|
Log.Debugf("not reporting due to config value")
|
||||||
|
return
|
||||||
|
}
|
||||||
|
|
||||||
if Branch != "main" {
|
if Branch != "main" {
|
||||||
Log.Debugf("reporting only on main branch")
|
Log.Debugf("reporting only on main branch")
|
||||||
return
|
return
|
||||||
|
Loading…
Reference in New Issue
Block a user