@@ -4,6 +4,7 @@ import { type NextRequest, NextResponse } from 'next/server'
44import { z } from 'zod'
55import { recordUsage } from '@/lib/billing/core/usage-log'
66import { checkAndBillOverageThreshold } from '@/lib/billing/threshold-billing'
7+ import { TraceAttr } from '@/lib/copilot/generated/trace-attributes-v1'
78import { TraceSpan } from '@/lib/copilot/generated/trace-spans-v1'
89import { checkInternalApiKey } from '@/lib/copilot/request/http'
910import { withIncomingGoSpan } from '@/lib/copilot/request/otel'
@@ -59,8 +60,8 @@ async function updateCostInner(
5960 logger . info ( `[${ requestId } ] Update cost request started` )
6061
6162 if ( ! isBillingEnabled ) {
62- span . setAttribute ( 'billing.outcome' , 'billing_disabled' )
63- span . setAttribute ( 'http.status_code' , 200 )
63+ span . setAttribute ( TraceAttr . BillingOutcome , 'billing_disabled' )
64+ span . setAttribute ( TraceAttr . HttpStatusCode , 200 )
6465 return NextResponse . json ( {
6566 success : true ,
6667 message : 'Billing disabled, cost update skipped' ,
@@ -76,8 +77,8 @@ async function updateCostInner(
7677 const authResult = checkInternalApiKey ( req )
7778 if ( ! authResult . success ) {
7879 logger . warn ( `[${ requestId } ] Authentication failed: ${ authResult . error } ` )
79- span . setAttribute ( 'billing.outcome' , 'auth_failed' )
80- span . setAttribute ( 'http.status_code' , 401 )
80+ span . setAttribute ( TraceAttr . BillingOutcome , 'auth_failed' )
81+ span . setAttribute ( TraceAttr . HttpStatusCode , 401 )
8182 return NextResponse . json (
8283 {
8384 success : false ,
@@ -95,8 +96,8 @@ async function updateCostInner(
9596 errors : validation . error . issues ,
9697 body,
9798 } )
98- span . setAttribute ( 'billing.outcome' , 'invalid_body' )
99- span . setAttribute ( 'http.status_code' , 400 )
99+ span . setAttribute ( TraceAttr . BillingOutcome , 'invalid_body' )
100+ span . setAttribute ( TraceAttr . HttpStatusCode , 400 )
100101 return NextResponse . json (
101102 {
102103 success : false ,
@@ -112,14 +113,14 @@ async function updateCostInner(
112113 const isMcp = source === 'mcp_copilot'
113114
114115 span . setAttributes ( {
115- 'user.id' : userId ,
116- 'gen_ai.request.model' : model ,
117- 'billing.source' : source ,
118- 'billing.cost_usd' : cost ,
119- 'gen_ai.usage.input_tokens' : inputTokens ,
120- 'gen_ai.usage.output_tokens' : outputTokens ,
121- 'billing.is_mcp' : isMcp ,
122- ...( idempotencyKey ? { 'billing.idempotency_key' : idempotencyKey } : { } ) ,
116+ [ TraceAttr . UserId ] : userId ,
117+ [ TraceAttr . GenAiRequestModel ] : model ,
118+ [ TraceAttr . BillingSource ] : source ,
119+ [ TraceAttr . BillingCostUsd ] : cost ,
120+ [ TraceAttr . GenAiUsageInputTokens ] : inputTokens ,
121+ [ TraceAttr . GenAiUsageOutputTokens ] : outputTokens ,
122+ [ TraceAttr . BillingIsMcp ] : isMcp ,
123+ ...( idempotencyKey ? { [ TraceAttr . BillingIdempotencyKey ] : idempotencyKey } : { } ) ,
123124 } )
124125
125126 claim = idempotencyKey
@@ -132,8 +133,8 @@ async function updateCostInner(
132133 userId,
133134 source,
134135 } )
135- span . setAttribute ( 'billing.outcome' , 'duplicate_idempotency_key' )
136- span . setAttribute ( 'http.status_code' , 409 )
136+ span . setAttribute ( TraceAttr . BillingOutcome , 'duplicate_idempotency_key' )
137+ span . setAttribute ( TraceAttr . HttpStatusCode , 409 )
137138 return NextResponse . json (
138139 {
139140 success : false ,
@@ -198,9 +199,9 @@ async function updateCostInner(
198199 cost,
199200 } )
200201
201- span . setAttribute ( 'billing.outcome' , 'billed' )
202- span . setAttribute ( 'http.status_code' , 200 )
203- span . setAttribute ( 'billing.duration_ms' , duration )
202+ span . setAttribute ( TraceAttr . BillingOutcome , 'billed' )
203+ span . setAttribute ( TraceAttr . HttpStatusCode , 200 )
204+ span . setAttribute ( TraceAttr . BillingDurationMs , duration )
204205 return NextResponse . json ( {
205206 success : true ,
206207 data : {
@@ -235,9 +236,9 @@ async function updateCostInner(
235236 )
236237 }
237238
238- span . setAttribute ( 'billing.outcome' , 'internal_error' )
239- span . setAttribute ( 'http.status_code' , 500 )
240- span . setAttribute ( 'billing.duration_ms' , duration )
239+ span . setAttribute ( TraceAttr . BillingOutcome , 'internal_error' )
240+ span . setAttribute ( TraceAttr . HttpStatusCode , 500 )
241+ span . setAttribute ( TraceAttr . BillingDurationMs , duration )
241242 return NextResponse . json (
242243 {
243244 success : false ,
0 commit comments