Files
ai/object/attribution_test.go
3837b37766 feat(telemetry): per-project attribution on cloud_usage ledger + gen_ai span (#95)
* feat(telemetry): stamp project + credential ref on cloud_usage ledger and gen_ai span

Adds per-project attribution to the observation-of-record. The TenantContextFilter
threads the caller's project sub-scope (X-Project-Id) and a NON-reversible ref of
the caller credential (SHA-256 of the bearer — never plaintext) onto the Go request
context (object.GenAIAttribution); recordTrace — the ONE funnel every completion
surface passes through — reads it once and stamps BOTH the warehouse row and the
span, so no threading through ~15 emit sites.

- object: GenAIAttribution + WithGenAIAttribution/FromContext (shared context key,
  no routers↔controllers dependency).
- cloud_usage: add `project` column (DDL + additive ADD COLUMN IF NOT EXISTS
  migration); zapWriteUsage writes record.Project.
- gen_ai span: emit gen_ai.hanzo.project + gen_ai.hanzo.api_key_hash when set
  (absent — honest — when unset). Latency is already inherent in span start/end.

This is the ledger column the cloud metrics board's per-project view reads: once
cloud repins this ai version, dropping the one default-project guard in
metricsBoard activates per-project cost/tokens/latency.

Tests: span emits project + key-hash (and hash is a ref, never plaintext); tenant
filter threads attribution onto the request context (beego Request replacement
propagates); hashBearer (Bearer-stripped, empty→empty); attribution round-trip.

* feat(telemetry): thread client session id (X-Session-Id) onto the gen_ai span

Completes the attribution set: the span already emits session.id +
gen_ai.conversation.id when record.Session is set, but no producer set it. The
TenantContextFilter now reads X-Session-Id and threads it through the same
context funnel as project + key-hash; recordTrace stamps record.Session. Empty
(no header) emits nothing — honest, the sessions view lights up the moment a
client supplies a session id.

---------

Co-authored-by: Hanzo Dev <[email protected]>
2026-07-16 09:57:36 -07:00

43 lines
1.6 KiB
Go

// Copyright 2023-2026 Hanzo AI Inc. All Rights Reserved.
//
// Licensed under the Apache License, Version 2.0 (the "License");
// you may not use this file except in compliance with the License.
package object
import (
"context"
"testing"
)
func TestGenAIAttribution_RoundTrip(t *testing.T) {
ctx := WithGenAIAttribution(context.Background(), GenAIAttribution{Project: "research", Session: "conv-42", APIKeyHash: "abc123"})
got := GenAIAttributionFromContext(ctx)
if got.Project != "research" || got.Session != "conv-42" || got.APIKeyHash != "abc123" {
t.Fatalf("round-trip = %+v, want {research conv-42 abc123}", got)
}
}
func TestGenAIAttribution_SessionOnly(t *testing.T) {
// A session-only attribution still wraps the context (it is not empty).
ctx := WithGenAIAttribution(context.Background(), GenAIAttribution{Session: "s1"})
if got := GenAIAttributionFromContext(ctx); got.Session != "s1" {
t.Fatalf("session-only round-trip = %+v", got)
}
}
func TestGenAIAttribution_EmptyIsNoop(t *testing.T) {
base := context.Background()
// An empty attribution must NOT wrap the context (nothing to carry).
if WithGenAIAttribution(base, GenAIAttribution{}) != base {
t.Fatal("empty attribution must return the context unchanged")
}
// Reading from a bare context yields the zero value, never a panic.
if got := GenAIAttributionFromContext(context.Background()); got != (GenAIAttribution{}) {
t.Fatalf("bare context = %+v, want zero", got)
}
if got := GenAIAttributionFromContext(nil); got != (GenAIAttribution{}) { //nolint:staticcheck
t.Fatalf("nil context = %+v, want zero", got)
}
}