diff --git a/diagnostics.go b/diagnostics.go index 6b42abb..e2b557e 100644 --- a/diagnostics.go +++ b/diagnostics.go @@ -81,22 +81,31 @@ type tags struct { Name *string `json:"name,omitempty"` } +var DEFAULT_SAMPLING_RATES = map[string]int{ + "initialize": 10000, + "config_sync": 0, + "api_call": 0, +} + func newDiagnostics(options *Options) *diagnostics { return &diagnostics{ initDiagnostics: &diagnosticsBase{ - context: InitializeContext, - markers: make([]marker, 0), - options: options, + context: InitializeContext, + markers: make([]marker, 0), + options: options, + samplingRates: DEFAULT_SAMPLING_RATES, }, syncDiagnostics: &diagnosticsBase{ - context: ConfigSyncContext, - markers: make([]marker, 0), - options: options, + context: ConfigSyncContext, + markers: make([]marker, 0), + options: options, + samplingRates: DEFAULT_SAMPLING_RATES, }, apiDiagnostics: &diagnosticsBase{ - context: ApiCallContext, - markers: make([]marker, 0), - options: options, + context: ApiCallContext, + markers: make([]marker, 0), + options: options, + samplingRates: DEFAULT_SAMPLING_RATES, }, } } @@ -112,30 +121,23 @@ func (d *diagnosticsBase) logProcess(msg string) { Logger().LogStep(process, msg) } -func (d *diagnosticsBase) serializeWithSampling() map[string]interface{} { +func (d *diagnosticsBase) serializeWithSampling() (map[string]interface{}, bool) { d.mu.RLock() defer d.mu.RUnlock() - markers := make([]marker, 0) - sampledKeys := make(map[string]bool) - for _, marker := range d.markers { - markerKey := string(*marker.Key) - if _, exists := sampledKeys[markerKey]; !exists { - sampleRate, exists := d.samplingRates[markerKey] - if !exists { - sampledKeys[markerKey] = false - } else { - sampledKeys[markerKey] = sample(sampleRate) - } - } - if !sampledKeys[markerKey] { - markers = append(markers, marker) - } + samplingRate, hasSamplingRate := d.samplingRates[string(d.context)] + if !hasSamplingRate || len(d.markers) == 0 { + return map[string]interface{}{}, false + } + shouldSample := sample(samplingRate) + if !shouldSample { + return map[string]interface{}{}, false } + return map[string]interface{}{ "context": d.context, - "markers": markers, - } + "markers": d.markers, + }, true } func (d *diagnosticsBase) updateSamplingRates(samplingRates map[string]int) { diff --git a/download_config_specs.json b/download_config_specs.json index 8de2f76..e5da8e0 100644 --- a/download_config_specs.json +++ b/download_config_specs.json @@ -467,5 +467,13 @@ }, "has_updates": true, "time": 1631638014811, - "id_lists": { "list_1": true, "list_2": true } + "id_lists": { "list_1": true, "list_2": true }, + "diagnostics": { + "download_config_specs": 10000, + "get_id_list": 10000, + "get_id_list_sources": 10000, + "api_call": 10000, + "config_sync": 10000, + "initialize": 10000 + } } diff --git a/download_config_specs_with_diagnostics_sampling.json b/download_config_specs_with_diagnostics_sampling.json index c96e5ce..3cb9ad6 100644 --- a/download_config_specs_with_diagnostics_sampling.json +++ b/download_config_specs_with_diagnostics_sampling.json @@ -468,6 +468,9 @@ "diagnostics": { "download_config_specs": 5000, "get_id_list": 5000, - "get_id_list_sources": 5000 + "get_id_list_sources": 5000, + "api_call": 5000, + "config_sync": 5000, + "initialize": 5000 } } diff --git a/logger.go b/logger.go index 6fdb213..6a07d84 100644 --- a/logger.go +++ b/logger.go @@ -262,9 +262,9 @@ func (l *logger) logDiagnosticsEvent(d *diagnosticsBase) { if d.isDisabled() { return } - serialized := d.serializeWithSampling() + serialized, shouldSample := d.serializeWithSampling() markers, exists := serialized["markers"] - if !exists { + if !shouldSample || !exists { return } markersTyped, ok := markers.([]marker) diff --git a/store.go b/store.go index 137b0cb..9af818c 100644 --- a/store.go +++ b/store.go @@ -393,6 +393,7 @@ func (s *store) parseTargetValueMapFromSpec(spec *configSpec) { func (s *store) setConfigSpecs(specs downloadConfigSpecResponse) (bool, bool) { s.diagnostics.initDiagnostics.updateSamplingRates(specs.DiagnosticsSampleRates) s.diagnostics.syncDiagnostics.updateSamplingRates(specs.DiagnosticsSampleRates) + s.diagnostics.apiDiagnostics.updateSamplingRates(specs.DiagnosticsSampleRates) if specs.HashedSDKKeyUsed != "" && specs.HashedSDKKeyUsed != getDJB2Hash(s.sdkKey) { s.errorBoundary.logException(fmt.Errorf("SDK key mismatch. Key used to generate response does not match key provided. Expected %s, got %s", getDJB2Hash(s.sdkKey), specs.HashedSDKKeyUsed))