mirror of
https://github.com/kopia/kopia.git
synced 2025-12-23 22:57:50 -05:00
Refactor `--profile-*` flags: - Multiple profile types can be enabled at once, before only a single type profiling could be done during a process execution. - The new `--profiles-store-on-exit` enables all available profile types, except for CPU profiling which needs to be explicitly enabled. - Profiling parameters can now be set via new flags. This allows setting the profile parameters for the pprof endpoint, as well as when saving profiles to files on exit. - Group profiling flags with other observability flags - Adds a `--diagnostics-output-directory` flag that unifies and supersedes the `--profile-dir` and `--metrics-directory` flags Enhancements and behavior changes: - Profile flags now have effect for all kopia commands, including `server start`. Before these flags did not have any effect in a few commands. - Multiple profile types can be enabled at once, before only a single type profiling could be done during a process execution. - The new `--profiles-store-on-exit` enables all available profile types, except for CPU profiling which needs to be explicitly enabled. - Profiling parameters can now be set via new flags. This allows setting the profile parameters for the pprof endpoint, as well as when saving profiles to files on exit. The following flags have been removed: - `--profile-dir`: superseded by the `--diagnostics-output-directory` flag - `--profile-blocking`: the `--profile-store-on-exit` flag enables blocking profiling. Use `--profile-blocking-rate=0` to explicitly disable it. - `--profile-memory`: the `--profile-store-on-exit` flag enables memory profiling. Use `--profile-memory-rate=0` to explicitly disable it. - `--profile-mutex`: the `--profile-store-on-exit` flag enables mutex profiling. Use `--profile-mutex-fraction=0` to explicitly disable it. Add CLI test for profile flags.
106 lines
2.8 KiB
Go
106 lines
2.8 KiB
Go
package cli_test
|
|
|
|
import (
|
|
"io"
|
|
"net/http"
|
|
"net/http/httptest"
|
|
"os"
|
|
"sync"
|
|
"testing"
|
|
|
|
"github.com/stretchr/testify/require"
|
|
|
|
"github.com/kopia/kopia/internal/testutil"
|
|
"github.com/kopia/kopia/tests/testenv"
|
|
)
|
|
|
|
func TestMetricsPushFlags(t *testing.T) {
|
|
env := testenv.NewCLITest(t, testenv.RepoFormatNotImportant, testenv.NewInProcRunner(t))
|
|
|
|
mux := http.NewServeMux()
|
|
|
|
var (
|
|
mu sync.Mutex
|
|
urls []string
|
|
bodies []string
|
|
auths []string
|
|
)
|
|
|
|
mux.HandleFunc("/", func(w http.ResponseWriter, r *http.Request) {
|
|
mu.Lock()
|
|
defer mu.Unlock()
|
|
|
|
urls = append(urls, r.RequestURI)
|
|
d, _ := io.ReadAll(r.Body)
|
|
bodies = append(bodies, r.Method+":"+string(d))
|
|
u, p, _ := r.BasicAuth()
|
|
|
|
auths = append(auths, u+":"+p)
|
|
})
|
|
|
|
tmp1 := testutil.TempDirectory(t)
|
|
|
|
server := httptest.NewServer(mux)
|
|
defer server.Close()
|
|
|
|
env.RunAndExpectSuccess(t, "repo", "create", "filesystem", "--path", tmp1,
|
|
"--metrics-push-addr="+server.URL,
|
|
"--metrics-push-interval=30s",
|
|
"--metrics-push-format=text",
|
|
)
|
|
|
|
env.RunAndExpectSuccess(t, "repo", "status",
|
|
"--metrics-push-addr="+server.URL,
|
|
"--metrics-push-interval=30s",
|
|
"--metrics-push-grouping=a:b",
|
|
"--metrics-push-username=user1",
|
|
"--metrics-push-password=pass1",
|
|
"--metrics-push-format=proto-text",
|
|
)
|
|
|
|
require.Equal(t, []string{
|
|
"/metrics/job/kopia", // initial
|
|
"/metrics/job/kopia", // final
|
|
"/metrics/job/kopia/a/b", // initial
|
|
"/metrics/job/kopia/a/b", // final
|
|
}, urls)
|
|
|
|
require.Equal(t, "user1:pass1", auths[len(auths)-1])
|
|
|
|
for _, b := range bodies {
|
|
// make sure bodies include some kopia metrics, don't need more
|
|
require.Contains(t, b, "kopia_cache_hit_bytes_total")
|
|
}
|
|
|
|
env.RunAndExpectFailure(t, "repo", "status",
|
|
"--metrics-push-addr="+server.URL,
|
|
"--metrics-push-grouping=a=s",
|
|
)
|
|
}
|
|
|
|
func TestOTLPFlags(t *testing.T) {
|
|
env := testenv.NewCLITest(t, testenv.RepoFormatNotImportant, testenv.NewInProcRunner(t))
|
|
|
|
// deprecated flag
|
|
env.RunAndExpectFailure(t, "benchmark", "crypto", "--repeat=1", "--block-size=1KB", "--print-options", "--enable-jaeger-collector")
|
|
|
|
// this has no effect whether OTLP collector is running or not.
|
|
env.RunAndExpectSuccess(t, "benchmark", "crypto", "--repeat=1", "--block-size=1KB", "--print-options", "--otlp-trace")
|
|
}
|
|
|
|
func TestMetricsSaveToOutputDirFlags(t *testing.T) {
|
|
env := testenv.NewCLITest(t, testenv.RepoFormatNotImportant, testenv.NewInProcRunner(t))
|
|
|
|
tmp1 := testutil.TempDirectory(t)
|
|
|
|
env.RunAndExpectSuccess(t, "repo", "create", "filesystem", "--path", tmp1)
|
|
|
|
tmp2 := testutil.TempDirectory(t)
|
|
|
|
env.RunAndExpectSuccess(t, "repo", "status", "--diagnostics-output-directory", tmp2, "--metrics-store-on-exit")
|
|
|
|
entries, err := os.ReadDir(tmp2)
|
|
require.NoError(t, err)
|
|
require.Len(t, entries, 1, "a metrics output file should have been created")
|
|
}
|