Skip to content

Commit

Permalink
diagnostics changes (#182)
Browse files Browse the repository at this point in the history
failing kong test change is in another pr
![Screenshot 2024-05-10 at 1 31 01
PM](https://github.com/statsig-io/private-go-sdk/assets/167801639/55c73feb-a814-4537-942f-7210d4304ce5)
  • Loading branch information
kat-statsig authored May 13, 2024
1 parent 9fd3b2f commit eb645d9
Show file tree
Hide file tree
Showing 5 changed files with 45 additions and 31 deletions.
56 changes: 29 additions & 27 deletions diagnostics.go
Original file line number Diff line number Diff line change
Expand Up @@ -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,
},
}
}
Expand All @@ -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) {
Expand Down
10 changes: 9 additions & 1 deletion download_config_specs.json
Original file line number Diff line number Diff line change
Expand Up @@ -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
}
}
5 changes: 4 additions & 1 deletion download_config_specs_with_diagnostics_sampling.json
Original file line number Diff line number Diff line change
Expand Up @@ -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
}
}
4 changes: 2 additions & 2 deletions logger.go
Original file line number Diff line number Diff line change
Expand Up @@ -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)
Expand Down
1 change: 1 addition & 0 deletions store.go
Original file line number Diff line number Diff line change
Expand Up @@ -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))
Expand Down

0 comments on commit eb645d9

Please sign in to comment.