Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
20 changes: 20 additions & 0 deletions apps/api/autumn.config.ts
Original file line number Diff line number Diff line change
Expand Up @@ -30,6 +30,15 @@ export const traces = feature({
consumable: true,
})

// Continuous CPU profiles, in GB of decoded OTLP profiles. Metered by the ingest gateway
// (`PROFILES_FEATURE_ID` in apps/ingest/src/main.rs).
export const profiles = feature({
id: "profiles",
name: "Profiles",
type: "metered",
consumable: true,
})

export const browserSessions = feature({
id: "browser_sessions",
name: "Browser Sessions",
Expand Down Expand Up @@ -141,6 +150,17 @@ export const startup = plan({
interval: "month",
},
},
{
// Priced above the other byte signals: a profile stores about 28 times its wire size.
featureId: "profiles",
included: 10,
price: {
amount: 0.6,
billingUnits: 1,
billingMethod: "usage_based",
interval: "month",
},
Comment thread
Makisuo marked this conversation as resolved.
Resolved
},
{
featureId: "browser_sessions",
included: 5000,
Expand Down
20 changes: 14 additions & 6 deletions apps/api/src/routes/internal/billing.http.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -402,27 +402,35 @@ describe("decodeInvoices", () => {
})

describe("sumDailySpend", () => {
const day = (date: string, logsGB: number, productEvents?: number) =>
const day = (date: string, logsGB: number, productEvents?: number, profilesGB?: number) =>
new DailyVolume({
date,
logsGB,
tracesGB: 1,
metricsGB: 0,
browserSessions: 2,
...(productEvents === undefined ? undefined : { productEvents }),
...(profilesGB === undefined ? undefined : { profilesGB }),
})
const series = (days: ReadonlyArray<DailyVolume>) =>
new DailySpendResponse({ cycleStart: 10, cycleEnd: 20, days })

it("adds a billing org's members into its series, day by day", () => {
const total = sumDailySpend(series([day("2026-10-01", 1, 3), day("2026-10-02", 2)]), [
series([day("2026-10-01", 0.5), day("2026-10-02", 4, 1)]),
const total = sumDailySpend(series([day("2026-10-01", 1, 3, 0.25), day("2026-10-02", 2)]), [
series([day("2026-10-01", 0.5, undefined, 0.5), day("2026-10-02", 4, 1)]),
])
assert.deepStrictEqual(
Arr.map(total.days, (d) => [d.date, d.logsGB, d.tracesGB, d.browserSessions, d.productEvents]),
Arr.map(total.days, (d) => [
d.date,
d.logsGB,
d.tracesGB,
d.browserSessions,
d.productEvents,
d.profilesGB,
]),
[
["2026-10-01", 1.5, 2, 4, 3],
["2026-10-02", 6, 2, 4, 1],
["2026-10-01", 1.5, 2, 4, 3, 0.75],
["2026-10-02", 6, 2, 4, 1, 0],
],
)
assert.strictEqual(total.cycleStart, 10)
Expand Down
2 changes: 2 additions & 0 deletions apps/api/src/routes/internal/billing.http.ts
Original file line number Diff line number Diff line change
Expand Up @@ -118,6 +118,7 @@ export const sumDailySpend = (
logsGB: sum.logsGB + next.logsGB,
tracesGB: sum.tracesGB + next.tracesGB,
metricsGB: sum.metricsGB + next.metricsGB,
profilesGB: (sum.profilesGB ?? 0) + (next.profilesGB ?? 0),
browserSessions: sum.browserSessions + next.browserSessions,
productEvents: (sum.productEvents ?? 0) + (next.productEvents ?? 0),
})
Expand All @@ -131,6 +132,7 @@ const orgUsageTotals = (orgId: OrgId, spend: DailySpendResponse) =>
logsGB: Arr.reduce(spend.days, 0, (sum, day) => sum + day.logsGB),
tracesGB: Arr.reduce(spend.days, 0, (sum, day) => sum + day.tracesGB),
metricsGB: Arr.reduce(spend.days, 0, (sum, day) => sum + day.metricsGB),
profilesGB: Arr.reduce(spend.days, 0, (sum, day) => sum + (day.profilesGB ?? 0)),
Comment thread
Makisuo marked this conversation as resolved.
Resolved
browserSessions: Arr.reduce(spend.days, 0, (sum, day) => sum + day.browserSessions),
productEvents: Arr.reduce(spend.days, 0, (sum, day) => sum + (day.productEvents ?? 0)),
})
Expand Down
102 changes: 100 additions & 2 deletions apps/ingest/src/main.rs
Original file line number Diff line number Diff line change
Expand Up @@ -1500,6 +1500,9 @@ const BROWSER_SESSIONS_FEATURE_ID: &str = "browser_sessions";
/// server-side one; only the transport differs).
const PRODUCT_EVENTS_FEATURE_ID: &str = "product_events";

/// The Autumn feature profiles bill as, in GB of decoded OTLP profiles like the byte signals.
const PROFILES_FEATURE_ID: &str = "profiles";

/// What a DENIED Autumn entitlement means for the batch in flight.
#[derive(Clone, Copy, PartialEq, Eq)]
enum OnDenied {
Expand Down Expand Up @@ -5640,7 +5643,7 @@ fn profile_resource_stamps(

/// `POST /v1development/profiles`, and `/v1/profiles` where the spec moves it: an
/// OTLP profiles export, flattened to rows of `profiles` by `maple_ingest::profiles`.
/// Metered per org but neither gated nor billed: profiles have no plan feature yet.
/// Gated on the org's `profiles` plan feature and billed per GB decoded, like the byte signals.
async fn handle_profiles_inner(
state: &AppState,
headers: &HeaderMap,
Expand Down Expand Up @@ -5684,6 +5687,12 @@ async fn handle_profiles_inner(
}
let pipeline =
native_rows_pipeline_for(state, destination, "Profile storage is not configured")?;
// Before the body is read: an org without the feature, or past its cap, gets a 402.
if org_id != SENTINEL_ORG_ID {
if let Some(error) = entitlement_rejection(state, &org_id, PROFILES_FEATURE_ID).await {
return Err(error);
}
}

let (payload_format, gzip) = profiles_body_encoding(headers)?;
let rules = profile_redaction_rules(state, &org_id).await?;
Expand Down Expand Up @@ -5748,11 +5757,18 @@ async fn handle_profiles_inner(
}

commit_profile_rows(state, pipeline, &org_id, rows, destination).await?;
// The usage counters only: nothing is reported to Autumn until profiles are priced.
// Recorded once the rows are durable, as the byte signals are. An export refused part way
// (only a WAL write failing, since the whole export is reserved first) bills nothing: its
// resend bills the whole export, so billing the stored part too would count it twice.
if let Some(usage) = &state.usage_metrics {
let signal = TelemetrySignal::Profiles.as_str();
usage.record(&org_id, signal, decoded_bytes as u64, count as u64);
}
if org_id != SENTINEL_ORG_ID && decoded_bytes > 0 {
if let Some(tracker) = &state.autumn_tracker {
tracker.track(&org_id, PROFILES_FEATURE_ID, billable_gb(decoded_bytes as u64));
Comment thread
Makisuo marked this conversation as resolved.
Resolved
}
}
state.resolver.note_source_use(&resolved_key, false);
Ok(accepted)
}
Expand Down Expand Up @@ -15166,6 +15182,88 @@ mod tests {
base
}

// Profiles are a plan feature: checked before the body is read, billed in GB once stored.
#[tokio::test]
async fn profiles_are_gated_and_billed_on_the_profiles_feature() {
let queue_dir = unique_main_test_dir("profiles-billing");
let key = "maple_sk_test_profiles_billing";
let store = private_key_store(key, "org_profiles_billing", false);
let (state, mut imports) = profiles_test_state(store, queue_dir.clone()).await;
let (state, mut rx) = with_fake_autumn(state).await;

assert_eq!(
send_sdk_profiles(&state, key).await.expect("an allowed org stores profiles"),
PROFILES_SDK_ROWS
);
assert_eq!(
imported_profile_rows(&mut imports, PROFILES_SDK_ROWS).await.len(),
PROFILES_SDK_ROWS
);
let calls = drain_autumn_calls(&mut rx).await;
assert!(checks(&calls)
.iter()
.any(|call| call.feature_id() == PROFILES_FEATURE_ID));
let billed = tracked(&calls);
assert_eq!(billed.len(), 1, "{billed:?}");
assert_eq!(billed[0].0, PROFILES_FEATURE_ID);
let expected = billable_gb(PROFILES_SDK_EXPORT.len() as u64);
assert!((billed[0].1 - expected).abs() < 1e-12, "{billed:?}");

// An org Autumn denies is refused before anything is decoded or stored.
let denied_dir = unique_main_test_dir("profiles-billing-denied");
let store = private_key_store(key, "org_profiles_denied", false);
let (state, mut imports) = profiles_test_state(store, denied_dir.clone()).await;
let state = with_autumn_denying(state, "org_profiles_denied").await;
let refused = send_sdk_profiles(&state, key)
.await
.expect_err("a denied org is refused");
assert_eq!(refused.status, StatusCode::PAYMENT_REQUIRED);
assert_eq!(state.decode_memory.reserved_bytes(), 0);
assert!(no_profiles_are_stored(&mut imports).await);
drop(std::fs::remove_dir_all(&queue_dir));
drop(std::fs::remove_dir_all(&denied_dir));
}

// A refused commit bills nothing, whatever it stored before the refusal: the resend bills the
// whole export, once. Any commit error returns before usage is recorded.
#[tokio::test]
async fn a_refused_profiles_commit_bills_nothing_and_its_resend_bills_once() {
let key = "maple_sk_test_profiles_refused_billing";
let refused_dir = unique_main_test_dir("profiles-refused-billing");
let store = private_key_store(key, "org_profiles_refused_billing", false);
// The org's queue cannot hold the export, so the commit is refused.
let (state, mut imports) =
profiles_test_state_with_org_queue(store, refused_dir.clone(), 100 * 1024).await;
let (state, mut rx) = with_fake_autumn(state).await;
let refused = send_sdk_profiles(&state, key)
.await
.expect_err("the export is past the org's queue");
assert_eq!(refused.status, StatusCode::PAYLOAD_TOO_LARGE);
assert!(no_profiles_are_stored(&mut imports).await);
assert!(tracked(&drain_autumn_calls(&mut rx).await).is_empty());

// The resend to a gateway with room is stored and billed whole, once.
let resent_dir = unique_main_test_dir("profiles-resent-billing");
let store = private_key_store(key, "org_profiles_refused_billing", false);
let (state, mut imports) = profiles_test_state(store, resent_dir.clone()).await;
let (state, mut rx) = with_fake_autumn(state).await;
assert_eq!(
send_sdk_profiles(&state, key).await.expect("the resend is stored"),
PROFILES_SDK_ROWS
);
assert_eq!(
imported_profile_rows(&mut imports, PROFILES_SDK_ROWS).await.len(),
PROFILES_SDK_ROWS
);
let calls = drain_autumn_calls(&mut rx).await;
let billed = tracked(&calls);
assert_eq!(billed.len(), 1, "{billed:?}");
let expected = billable_gb(PROFILES_SDK_EXPORT.len() as u64);
assert!((billed[0].1 - expected).abs() < 1e-12, "{billed:?}");
drop(std::fs::remove_dir_all(&refused_dir));
drop(std::fs::remove_dir_all(&resent_dir));
}

// The spec's development path and the path it moves to both take an export.
#[tokio::test]
async fn both_profiles_routes_take_an_export() {
Expand Down
4 changes: 2 additions & 2 deletions apps/landing/src/lib/pricing-offer.ts
Original file line number Diff line number Diff line change
Expand Up @@ -13,7 +13,7 @@
* `label: m.pricing_logs()` freezes whichever locale rendered first and every
* other locale inherits it.
*/
import { PRICING_CARD_FEATURE_IDS, isPricingCardFeature } from "@maple/domain/billing"
import { PRICING_CARD_FEATURE_IDS, isAdvertisedPricingFeature } from "@maple/domain/billing"
import { Autumn } from "autumn-js"
import * as m from "../paraglide/messages.js"
import { Array as Arr } from "effect"
Expand Down Expand Up @@ -142,7 +142,7 @@ export async function getOffer(): Promise<Offer> {
hasTrial: !!product.freeTrial,
trialDuration: product.freeTrial?.durationLength,
allotments: Arr.map(
Arr.filter(product.items, (item) => isPricingCardFeature(item.featureId)),
Arr.filter(product.items, (item) => isAdvertisedPricingFeature(item.featureId)),
(item) => {
const featureId = item.featureId
const unit = unitFor(featureId)
Expand Down
4 changes: 4 additions & 0 deletions apps/web/src/lab/billing-orgs-lab.ts
Original file line number Diff line number Diff line change
Expand Up @@ -59,6 +59,7 @@ const plans = Schema.decodeSync(Schema.Array(CatalogPlan))([
{ featureId: "logs", included: 100, price: { amount: 0.3 } },
{ featureId: "traces", included: 100, price: { amount: 0.3 } },
{ featureId: "metrics", included: 100, price: { amount: 0.3 } },
{ featureId: "profiles", included: 10, price: { amount: 0.6 } },
{ featureId: "browser_sessions", included: 5_000, price: { amount: 0.002 } },
{
featureId: "product_events",
Expand All @@ -85,6 +86,7 @@ const customer = Schema.decodeSync(BillingCustomer)({
logs: { granted: 100 },
traces: { granted: 100 },
metrics: { granted: 100 },
profiles: { granted: 10 },
browser_sessions: { granted: 5_000 },
product_events: { granted: 1_000_000 },
ai_credits: { granted: 10 },
Expand All @@ -98,6 +100,7 @@ const usage = (state: LabState) =>
logs: { sum: 142.6 },
traces: { sum: 61.2 },
metrics: { sum: 18.4 },
profiles: { sum: 6.3 },
browser_sessions: { sum: 3_120 },
product_events: { sum: 0 },
ai_credits: { sum: state === "ai-credits" ? 16.11 : 0 },
Expand Down Expand Up @@ -168,6 +171,7 @@ const accountUsage = Schema.decodeSync(BillingAccountUsageResponse)({
logsGB: 120,
tracesGB: 50,
metricsGB: 15,
profilesGB: 6.3,
browserSessions: 3_120,
productEvents: 0,
},
Expand Down
1 change: 1 addition & 0 deletions apps/web/src/lab/entries/onboarding.ts
Original file line number Diff line number Diff line change
Expand Up @@ -48,6 +48,7 @@ export const PLANS = [
meteredItem("logs", 100, 0.3, 1, "then $0.3 per Logs"),
meteredItem("traces", 100, 0.3, 1, "then $0.3 per Traces"),
meteredItem("metrics", 100, 0.3, 1, "then $0.3 per Metrics"),
meteredItem("profiles", 10, 0.6, 1, "then $0.6 per Profiles"),
meteredItem("browser_sessions", 5000, 0.002, 1, "then $0.002 per browser session"),
meteredItem("product_events", 1_000_000, 0.005, 1000, "then $0.005 per 1,000 product events"),
],
Expand Down
17 changes: 17 additions & 0 deletions apps/web/src/page/onboarding/pricing-plans.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -27,6 +27,23 @@ describe("getFeatureRows", () => {
expect(Arr.map(getFeatureRows(plan), (row) => row.featureId)).toEqual(["logs", "ai_credits"])
})

it("leaves profiles off the plan cards until they are advertised", () => {
const plan = new CatalogPlan({
id: "startup",
name: "Startup",
items: [
new CatalogPlanItem({ featureId: "logs", included: 100 }),
new CatalogPlanItem({
featureId: "profiles",
included: 10,
display: { secondaryText: "then $0.6 per 1 profiles" },
}),
],
})

expect(Arr.map(getFeatureRows(plan), (row) => row.featureId)).toEqual(["logs"])
})

it("quotes an overage rate in whole cents and leaves sub-cent rates alone", () => {
const metered = (featureId: string, secondaryText: string) =>
new CatalogPlanItem({ featureId, included: 100, display: { secondaryText } })
Expand Down
8 changes: 5 additions & 3 deletions apps/web/src/page/onboarding/pricing-plans.ts
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
import type { CatalogPlan, CatalogPlanItem } from "@maple/domain/http"
import { isPricingCardFeature } from "@maple/domain/billing"
import { isAdvertisedPricingFeature } from "@maple/domain/billing"
import { formatCurrency } from "@maple/domain/format"
import { formatCount, formatQuoteUnits, quoteUnits } from "../kit/billing/usage"
import type { IconName } from "@maple/views/ui/icon"
Expand All @@ -12,6 +12,7 @@ export const FEATURE_ICONS = new Map<string, IconName>([
["logs", "FileIcon"],
["traces", "PulseIcon"],
["metrics", "ChartLineIcon"],
["profiles", "FireIcon"],
["browser_sessions", "PlayRotateClockwiseIcon"],
["product_events", "GlobePointerIcon"],
["ai_credits", "SquareSparkleIcon"],
Expand All @@ -21,6 +22,7 @@ const DATA_FEATURE_LABELS = new Map<string, string>([
["logs", "Logs"],
["traces", "Traces"],
["metrics", "Metrics"],
["profiles", "Profiles"],
["browser_sessions", "Browser sessions"],
["product_events", "Product events"],
["ai_credits", "AI usage"],
Expand Down Expand Up @@ -72,7 +74,7 @@ const padCents = (text: string): string =>
replaceWith(text, /\$(\d+)\.(\d)(?!\d)/g, (_, dollars = "", tenths = "") => `$${dollars}.${tenths}0`)

const normalizeDetailText = (text: string): string =>
padCents(Str.replace(/\bper\s+(?:[\d,]+\s+)?(?:logs?|traces?|metrics?)\b/i, "per GB")(text))
padCents(Str.replace(/\bper\s+(?:[\d,]+\s+)?(?:logs?|traces?|metrics?|profiles?)\b/i, "per GB")(text))

/** Autumn words the rate per billing block; events are quoted per million, in its style. */
const formatDetail = (item: CatalogPlanItem): string | undefined => {
Expand Down Expand Up @@ -134,7 +136,7 @@ export type FeatureRow = Readonly<{ featureId: string; label: string; value: str

export const getFeatureRows = (plan: CatalogPlan): ReadonlyArray<FeatureRow> =>
Arr.map(
Arr.filter(plan.items, (item) => isPricingCardFeature(item.featureId)),
Arr.filter(plan.items, (item) => isAdvertisedPricingFeature(item.featureId)),
(item) => {
const detail = formatDetail(item)
return {
Expand Down
1 change: 1 addition & 0 deletions apps/web/src/page/settings/billing/model.ts
Original file line number Diff line number Diff line change
Expand Up @@ -31,6 +31,7 @@ export const SpendFeatureId = Schema.Literals([
"logs",
"traces",
"metrics",
"profiles",
"browser_sessions",
"product_events",
"ai_credits",
Expand Down
Loading
Loading