Files
magpie/internal/gui/otel_test.go
T
Aliceandraper 0260716c9d feat(usage): 可选的 OTLP 元数据导出(Trace/Metrics,支持 Langfuse) (#412)
* usage: add opt-in OTLP HTTP metadata export

Export gateway traces and optional duration/token histograms asynchronously with a bounded queue, retries, proxy support and a bounded shutdown. Add bilingual Settings controls and MAGPIE_OTEL_* overrides. Keep prompts, replies, sessions and provider credentials local; use the same OTLP path for Langfuse.

Validated: full Go tests (nogui), usage race tests, focused go vet, and Chromium/WebKit settings tests in English and Chinese.

Refs #213

* otel: protect collector credentials in backups and settings

Remove all OTLP headers from keyless backups and restrict both new and existing settings files to 0600 before saving. Clarify SIGTERM export behavior. Addresses the two blocking review findings in #412.

* otel: preserve local credentials and read-only settings on restore

---------

Co-authored-by: raper <raper@raperdeMacBook-Air.local>
2026-10-02 01:51:04 +08:00

40 lines
1.4 KiB
Go

package gui
import (
"encoding/json"
"net/http"
"net/http/httptest"
"path/filepath"
"strings"
"testing"
"github.com/yetone/magpie/internal/settings"
)
func TestOTelPreferencesAPI(t *testing.T) {
t.Setenv("XDG_CONFIG_HOME", filepath.Join(t.TempDir(), "config"))
t.Setenv("XDG_CACHE_HOME", t.TempDir())
t.Setenv("MAGPIE_OTEL_ENDPOINT", "https://environment.test")
t.Setenv("MAGPIE_OTEL_HEADERS", "Authorization=PRIVATE-ENV-SECRET")
h := Handler(nil, nil)
r := httptest.NewRecorder()
h.ServeHTTP(r, httptest.NewRequest("POST", "/api/settings", strings.NewReader(`{"otel":{"enabled":true,"endpoint":"https://saved.test/","headers":{"Authorization":"Bearer saved"}}}`)))
if r.Code != http.StatusOK {
t.Fatalf("save: %d %s", r.Code, r.Body)
}
r = httptest.NewRecorder()
h.ServeHTTP(r, httptest.NewRequest("GET", "/api/settings", nil))
var got settingsJSON
if err := json.Unmarshal(r.Body.Bytes(), &got); err != nil {
t.Fatal(err)
}
if !got.OTelEnv || !got.OTel.Enabled || got.OTel.Endpoint != "https://saved.test" || strings.Contains(r.Body.String(), "PRIVATE-ENV-SECRET") {
t.Fatalf("preferences: %s", r.Body)
}
r = httptest.NewRecorder()
h.ServeHTTP(r, httptest.NewRequest("POST", "/api/settings", strings.NewReader(`{"otel":{"enabled":true,"endpoint":"file:///tmp/secret"}}`)))
if r.Code == http.StatusOK || settings.Load().OTel.Endpoint != "https://saved.test" {
t.Fatalf("invalid endpoint overwrote preferences: %d %s", r.Code, r.Body)
}
}