diff --git a/plugin/sampling/strategystore/static/strategy_store.go b/plugin/sampling/strategystore/static/strategy_store.go index 704c9453226..51c9534c348 100644 --- a/plugin/sampling/strategystore/static/strategy_store.go +++ b/plugin/sampling/strategystore/static/strategy_store.go @@ -66,7 +66,7 @@ func NewStrategyStore(options Options, logger *zap.Logger) (ss.StrategyStore, er return h, nil } - loadFn := samplingStrategyLoader(options.StrategiesFile) + loadFn := h.samplingStrategyLoader(options.StrategiesFile) strategies, err := loadStrategies(loadFn) if err != nil { return nil, err @@ -95,7 +95,8 @@ func (h *strategyStore) Close() { h.cancelFunc() } -func downloadSamplingStrategies(url string) ([]byte, error) { +func (h *strategyStore) downloadSamplingStrategies(url string) ([]byte, error) { + h.logger.Info("Downloading sampling strategies", zap.String("url", url)) resp, err := http.Get(url) if err != nil { return nil, fmt.Errorf("failed to download sampling strategies: %w", err) @@ -126,14 +127,15 @@ func isURL(str string) bool { return err == nil && u.Scheme != "" && u.Host != "" } -func samplingStrategyLoader(strategiesFile string) strategyLoader { +func (h *strategyStore) samplingStrategyLoader(strategiesFile string) strategyLoader { if isURL(strategiesFile) { return func() ([]byte, error) { - return downloadSamplingStrategies(strategiesFile) + return h.downloadSamplingStrategies(strategiesFile) } } return func() ([]byte, error) { + h.logger.Info("Loading sampling strategies", zap.String("filename", strategiesFile)) currBytes, err := ioutil.ReadFile(filepath.Clean(strategiesFile)) if err != nil { return nil, fmt.Errorf("failed to read strategies file %s: %w", strategiesFile, err) diff --git a/plugin/sampling/strategystore/static/strategy_store_test.go b/plugin/sampling/strategystore/static/strategy_store_test.go index 6d9b2f985ce..bff3aa19ac1 100644 --- a/plugin/sampling/strategystore/static/strategy_store_test.go +++ b/plugin/sampling/strategystore/static/strategy_store_test.go @@ -359,7 +359,7 @@ func TestAutoUpdateStrategyWithFile(t *testing.T) { assert.EqualValues(t, makeResponse(sampling.SamplingStrategyType_PROBABILISTIC, 0.8), *s) // verify that reloading is a no-op - value := store.reloadSamplingStrategy(samplingStrategyLoader(dstFile), string(srcBytes)) + value := store.reloadSamplingStrategy(store.samplingStrategyLoader(dstFile), string(srcBytes)) assert.Equal(t, string(srcBytes), value) // update file with new probability of 0.9 @@ -394,7 +394,7 @@ func TestAutoUpdateStrategyWithURL(t *testing.T) { assert.EqualValues(t, makeResponse(sampling.SamplingStrategyType_PROBABILISTIC, 0.8), *s) // verify that reloading in no-op - value := store.reloadSamplingStrategy(samplingStrategyLoader(mockServer.URL), mockStrategy.Load()) + value := store.reloadSamplingStrategy(store.samplingStrategyLoader(mockServer.URL), mockStrategy.Load()) assert.Equal(t, mockStrategy.Load(), value) // update original strategies with new probability of 0.9 @@ -431,25 +431,25 @@ func TestAutoUpdateStrategyErrors(t *testing.T) { defer store.Close() // check invalid file path or read failure - assert.Equal(t, "blah", store.reloadSamplingStrategy(samplingStrategyLoader(tempFile.Name()+"bad-path"), "blah")) + assert.Equal(t, "blah", store.reloadSamplingStrategy(store.samplingStrategyLoader(tempFile.Name()+"bad-path"), "blah")) assert.Len(t, logs.FilterMessage("failed to re-load sampling strategies").All(), 1) // check bad file content require.NoError(t, ioutil.WriteFile(tempFile.Name(), []byte("bad value"), 0644)) - assert.Equal(t, "blah", store.reloadSamplingStrategy(samplingStrategyLoader(tempFile.Name()), "blah")) + assert.Equal(t, "blah", store.reloadSamplingStrategy(store.samplingStrategyLoader(tempFile.Name()), "blah")) assert.Len(t, logs.FilterMessage("failed to update sampling strategies").All(), 1) // check invalid url - assert.Equal(t, "duh", store.reloadSamplingStrategy(samplingStrategyLoader("bad-url"), "duh")) + assert.Equal(t, "duh", store.reloadSamplingStrategy(store.samplingStrategyLoader("bad-url"), "duh")) assert.Len(t, logs.FilterMessage("failed to re-load sampling strategies").All(), 2) // check status code other than 200 mockServer, _ := mockStrategyServer() - assert.Equal(t, "duh", store.reloadSamplingStrategy(samplingStrategyLoader(mockServer.URL+"/bad-status"), "duh")) + assert.Equal(t, "duh", store.reloadSamplingStrategy(store.samplingStrategyLoader(mockServer.URL+"/bad-status"), "duh")) assert.Len(t, logs.FilterMessage("failed to re-load sampling strategies").All(), 3) // check bad content from url - assert.Equal(t, "duh", store.reloadSamplingStrategy(samplingStrategyLoader(mockServer.URL+"/bad-content"), "duh")) + assert.Equal(t, "duh", store.reloadSamplingStrategy(store.samplingStrategyLoader(mockServer.URL+"/bad-content"), "duh")) assert.Len(t, logs.FilterMessage("failed to update sampling strategies").All(), 2) } @@ -469,19 +469,20 @@ func TestServiceNoPerOperationStrategies(t *testing.T) { } func TestSamplingStrategyLoader(t *testing.T) { + store := &strategyStore{logger: zap.NewNop()} // invalid file path - loader := samplingStrategyLoader("not-exists") + loader := store.samplingStrategyLoader("not-exists") _, err := loader() assert.Contains(t, err.Error(), "failed to read strategies file not-exists") // status code other than 200 mockServer, _ := mockStrategyServer() - loader = samplingStrategyLoader(mockServer.URL + "/bad-status") + loader = store.samplingStrategyLoader(mockServer.URL + "/bad-status") _, err = loader() assert.Contains(t, err.Error(), "receiving 404 Not Found while downloading strategies file") // should download content from URL - loader = samplingStrategyLoader(mockServer.URL + "/bad-content") + loader = store.samplingStrategyLoader(mockServer.URL + "/bad-content") content, err := loader() require.NoError(t, err) assert.Equal(t, "bad-content", string(content))