Files
magpie/internal/otlp/otel_resource_test.go
T
yetoneandreyoung 83bdfe70d2 otlp: OTEL_RESOURCE_ATTRIBUTES and OTEL_SERVICE_NAME name the server a trace or metric came from (#5167)
Several magpie servers exporting to one collector or to Langfuse all sent
the same resource, service.name=magpie alone, so nothing said which server
an event came from. Both signals' resource now merges the standard
OTEL_RESOURCE_ATTRIBUTES over that default, and OTEL_SERVICE_NAME over
that (otelResourceAttributes in internal/otlp/otel_resource.go). Nothing
about the machine is looked up; only what the user set is sent. Neither
variable turns export on.

Ported from reyoung's 7ed03b819 (feat/otel-resource-attributes, base
4ca55c6b6), reviewed line by line; nothing in it reached past its goal.
Changed from it:
- service.name precedence. The resource SDK spec makes code-supplied
  attributes win over OTEL_RESOURCE_ATTRIBUTES, but magpie's "magpie" is
  the fallback in the role the spec gives unknown_service, not identity
  the user supplied, and magpie's default never overrides a value the
  user set (code standards). So a service.name in the variable wins, and
  OTEL_SERVICE_NAME, which the spec's env table puts over the variable's,
  wins over both. An empty service.name or blank OTEL_SERVICE_NAME counts
  as unset (the spec's "empty value = unset"), so the resource never
  carries service.name="".
- Bytes that aren't UTF-8 once decoded (%FF) are a decoding error and
  discard the variable; before, json.Marshal sent U+FFFD.
- The variable was parsed and, when bad, logged on every flush of each
  signal. It is now parsed once per value and logged once, naming the
  entry by position (never its contents).
- otlp tests clear both variables in otelConfig, so a developer's shell
  can't change what the existing wire tests see.
Kept: whole variable discarded on any invalid entry (no '=', empty key,
bad escape, empty entry such as a trailing comma), as the spec says;
keys and values trimmed and percent-decoded with PathUnescape ('+' stays
'+'); last repeated key wins; values are strings.

Docs: reference.md's OTLP export, plugin-runtime.md's internal/otlp row,
and the privacy pages (en, zh, ja) now say the resource carries what the
user sets there.

Verified:
- TestOTelResourceAttributes reads the resource a test collector gets on
  /v1/traces and /v1/metrics, 17 cases. On the old otelEnvelope it FAILs
  (server, override and duplicate, encoding and empty, unencoded UTF-8,
  OTEL_SERVICE_NAME cases: "traces resource attributes =
  map[service.name:magpie]", same for metrics). Broken by hand one at a
  time, each went red: UTF-8 check off (not_UTF-8_once_decoded), no
  OTEL_SERVICE_NAME (3 cases), empty service.name kept (empty_service_
  name_is_unset), valid entries kept from a bad variable (missing equals,
  trailing comma, empty key, ...), and the per-value cache off
  (TestOTelResourceAttributesLoggedOnceWithoutContents: "logged 4 times,
  want once"). All green restored.
- On mote: go test -tags nogui ./internal/otlp ok; go test -race -count=3
  -run TestOTel ./internal/otlp ok; go vet, GOOS=linux go vet -tags nogui,
  GOOS=windows go vet (internal/otlp); go build, GOOS=linux go build -tags
  nogui, GOOS=windows go build; node --test site/*.test.mjs; gofmt -l
  empty.
- Full suite: run after the push (go test -tags nogui -timeout 45m ./...
  on mote); reds, if any, filed separately.

Not tried: a real collector or Langfuse; Windows/Linux runs of the
package (the code has no OS-specific path).

Co-authored-by: reyoung <728699+reyoung@users.noreply.github.com>
2026-11-04 13:50:09 +08:00

151 lines
5.6 KiB
Go

package otlp
import (
"bytes"
"encoding/json"
"io"
"log"
"net/http"
"net/http/httptest"
"reflect"
"strings"
"testing"
"time"
"github.com/yetone/magpie/internal/settings"
)
// exportResources flushes one record and returns the resource attributes
// the collector got on /v1/traces and on /v1/metrics, in that order.
func exportResources(t *testing.T, attrs, name string) [2]map[string]string {
t.Helper()
type received struct {
path string
body []byte
}
requests := make(chan received, 2)
collector := httptest.NewServer(http.HandlerFunc(func(w http.ResponseWriter, r *http.Request) {
body, err := io.ReadAll(r.Body)
if err != nil {
t.Error(err)
}
requests <- received{r.URL.Path, body}
io.WriteString(w, `{}`)
}))
defer collector.Close()
otelConfig(t, settings.OTel{Endpoint: collector.URL, Metrics: true})
t.Setenv("OTEL_RESOURCE_ATTRIBUTES", attrs)
t.Setenv("OTEL_SERVICE_NAME", name)
config, err := settings.OTelExport()
if err != nil {
t.Fatal(err)
}
e := newOTelExporter()
defer e.cancel()
defer e.client.CloseIdleConnections()
e.flush([]otelItem{{record: Record{Time: time.Now(), Status: 200, Input: 1}, config: config}})
var out [2]map[string]string
for i, signal := range []string{"Spans", "Metrics"} {
var request received
select {
case request = <-requests:
default:
t.Fatalf("collector received no %s export", signal)
}
if want := map[string]string{"Spans": "/v1/traces", "Metrics": "/v1/metrics"}[signal]; request.path != want {
t.Fatalf("path = %q, want %q", request.path, want)
}
var payload map[string][]struct {
Resource struct {
Attributes []otelAttribute `json:"attributes"`
} `json:"resource"`
}
if err := json.Unmarshal(request.body, &payload); err != nil {
t.Fatal(err)
}
resources := payload["resource"+signal]
if len(resources) != 1 {
t.Fatalf("%s: got %d resources, want 1", signal, len(resources))
}
got := map[string]string{}
for _, attr := range resources[0].Resource.Attributes {
value, ok := attr.Value["stringValue"].(string)
if !ok {
t.Fatalf("%s: attribute %q is not a string: %v", signal, attr.Key, attr.Value)
}
if _, dup := got[attr.Key]; dup {
t.Fatalf("%s: duplicate attribute %q", signal, attr.Key)
}
got[attr.Key] = value
}
out[i] = got
}
return out
}
// A server identity set in OTEL_RESOURCE_ATTRIBUTES reaches the resource
// of both signals, as the collector receives them (#5167).
func TestOTelResourceAttributes(t *testing.T) {
magpie := map[string]string{"service.name": "magpie"}
for _, tc := range []struct {
name, attrs, service string
want map[string]string
}{
{"default", "", "", magpie},
{"whitespace", " \t ", "", magpie},
{"server", "service.instance.id=magpie-prod-01,host.name=server-a,deployment.environment.name=production", "", map[string]string{
"service.name": "magpie", "service.instance.id": "magpie-prod-01", "host.name": "server-a", "deployment.environment.name": "production",
}},
{"override and duplicate", "service.name=first,service.name=custom,host.name=old,host.name=new", "", map[string]string{"service.name": "custom", "host.name": "new"}},
{"encoding and empty", " label%2Ckey = a%2Cb%3Dc%20d+e , empty=,host.name=%E6%9C%BA%E5%99%A8", "", map[string]string{
"service.name": "magpie", "label,key": "a,b=c d+e", "empty": "", "host.name": "机器",
}},
{"unencoded UTF-8 kept", "host.name=机器", "", map[string]string{"service.name": "magpie", "host.name": "机器"}},
{"empty service name is unset", "service.name=,host.name=a", "", map[string]string{"service.name": "magpie", "host.name": "a"}},
{"OTEL_SERVICE_NAME wins", "service.name=from-attrs,host.name=a", " gw-eu ", map[string]string{"service.name": "gw-eu", "host.name": "a"}},
{"OTEL_SERVICE_NAME alone", "", "gw-eu", map[string]string{"service.name": "gw-eu"}},
{"blank OTEL_SERVICE_NAME is unset", "service.name=custom", " ", map[string]string{"service.name": "custom"}},
{"OTEL_SERVICE_NAME over a discarded variable", "host.name=a,broken", "gw-eu", map[string]string{"service.name": "gw-eu"}},
{"missing equals", "host.name=server-a,broken", "", magpie},
{"trailing comma", "host.name=server-a,", "", magpie},
{"empty key", "host.name=server-a, =value", "", magpie},
{"bad key escape", "host.name=server-a,bad%ZZ=value", "", magpie},
{"bad value escape", "host.name=server-a,label=bad%ZZ", "", magpie},
{"not UTF-8 once decoded", "host.name=server-a,label=%FF", "", magpie},
} {
t.Run(tc.name, func(t *testing.T) {
got := exportResources(t, tc.attrs, tc.service)
for i, signal := range []string{"traces", "metrics"} {
if !reflect.DeepEqual(got[i], tc.want) {
t.Errorf("%s resource attributes = %v, want %v", signal, got[i], tc.want)
}
}
})
}
}
// A discarded variable is reported once, by position, never by its
// contents, however many exports read it.
func TestOTelResourceAttributesLoggedOnceWithoutContents(t *testing.T) {
var buf bytes.Buffer
prev, flags := log.Writer(), log.Flags()
log.SetOutput(&buf)
log.SetFlags(0)
defer func() { log.SetOutput(prev); log.SetFlags(flags) }()
attrs := "host.name=secret-host-7731,label=bad%ZZ"
exportResources(t, attrs, "")
exportResources(t, attrs, "")
out := buf.String()
if n := strings.Count(out, "OTEL_RESOURCE_ATTRIBUTES ignored"); n != 1 {
t.Fatalf("logged %d times, want once:\n%s", n, out)
}
if !strings.Contains(out, "entry 2") {
t.Errorf("log doesn't name the entry: %q", out)
}
for _, secret := range []string{"secret-host-7731", "host.name", "bad%ZZ"} {
if strings.Contains(out, secret) {
t.Errorf("log quotes %q: %q", secret, out)
}
}
}