Skip to content

Commit

Permalink
error rates
Browse files Browse the repository at this point in the history
  • Loading branch information
boufni95 committed Jan 9, 2025
1 parent 4aeb565 commit 16d4198
Show file tree
Hide file tree
Showing 13 changed files with 323 additions and 9 deletions.
24 changes: 24 additions & 0 deletions proto/autogenerated/client.md
Original file line number Diff line number Diff line change
Expand Up @@ -98,6 +98,11 @@ The nostr server will send back a message response, and inside the body there wi
- This methods has an __empty__ __request__ body
- output: [DebitAuthorizations](#DebitAuthorizations)

- GetErrorStats
- auth type: __Metrics__
- This methods has an __empty__ __request__ body
- output: [ErrorStats](#ErrorStats)

- GetHttpCreds
- auth type: __User__
- This methods has an __empty__ __request__ body
Expand Down Expand Up @@ -467,6 +472,13 @@ The nostr server will send back a message response, and inside the body there wi
- This methods has an __empty__ __request__ body
- output: [DebitAuthorizations](#DebitAuthorizations)

- GetErrorStats
- auth type: __Metrics__
- http method: __post__
- http route: __/api/reports/errors__
- This methods has an __empty__ __request__ body
- output: [ErrorStats](#ErrorStats)

- GetHttpCreds
- auth type: __User__
- http method: __post__
Expand Down Expand Up @@ -986,6 +998,18 @@ The nostr server will send back a message response, and inside the body there wi
### EnrollAdminTokenRequest
- __admin_token__: _string_

### ErrorStat
- __errors__: _number_
- __from_unix__: _number_
- __total__: _number_

### ErrorStats
- __past10m__: _[ErrorStat](#ErrorStat)_
- __past1h__: _[ErrorStat](#ErrorStat)_
- __past1m__: _[ErrorStat](#ErrorStat)_
- __past24h__: _[ErrorStat](#ErrorStat)_
- __past6h__: _[ErrorStat](#ErrorStat)_

### FrequencyRule
- __amount__: _number_
- __interval__: _[IntervalType](#IntervalType)_
Expand Down
27 changes: 27 additions & 0 deletions proto/autogenerated/go/http_client.go
Original file line number Diff line number Diff line change
Expand Up @@ -78,6 +78,7 @@ type Client struct {
GetAppUserLNURLInfo func(req GetAppUserLNURLInfoRequest) (*LnurlPayInfoResponse, error)
GetAppsMetrics func(req AppsMetricsRequest) (*AppsMetrics, error)
GetDebitAuthorizations func() (*DebitAuthorizations, error)
GetErrorStats func() (*ErrorStats, error)
GetHttpCreds func() (*HttpCreds, error)
GetInviteLinkState func(req GetInviteTokenStateRequest) (*GetInviteTokenStateResponse, error)
GetLNURLChannelLink func() (*LnurlLinkResponse, error)
Expand Down Expand Up @@ -758,6 +759,32 @@ func NewClient(params ClientParams) *Client {
}
return &res, nil
},
GetErrorStats: func() (*ErrorStats, error) {
auth, err := params.RetrieveMetricsAuth()
if err != nil {
return nil, err
}
finalRoute := "/api/reports/errors"
body := []byte{}
resBody, err := doPostRequest(params.BaseURL+finalRoute, body, auth)
if err != nil {
return nil, err
}
result := ResultError{}
err = json.Unmarshal(resBody, &result)
if err != nil {
return nil, err
}
if result.Status == "ERROR" {
return nil, fmt.Errorf(result.Reason)
}
res := ErrorStats{}
err = json.Unmarshal(resBody, &res)
if err != nil {
return nil, err
}
return &res, nil
},
// server streaming method: GetHttpCreds not implemented
GetInviteLinkState: func(req GetInviteTokenStateRequest) (*GetInviteTokenStateResponse, error) {
auth, err := params.RetrieveAdminAuth()
Expand Down
12 changes: 12 additions & 0 deletions proto/autogenerated/go/types.go
Original file line number Diff line number Diff line change
Expand Up @@ -250,6 +250,18 @@ type EncryptionExchangeRequest struct {
type EnrollAdminTokenRequest struct {
Admin_token string `json:"admin_token"`
}
type ErrorStat struct {
Errors int64 `json:"errors"`
From_unix int64 `json:"from_unix"`
Total int64 `json:"total"`
}
type ErrorStats struct {
Past10m *ErrorStat `json:"past10m"`
Past1h *ErrorStat `json:"past1h"`
Past1m *ErrorStat `json:"past1m"`
Past24h *ErrorStat `json:"past24h"`
Past6h *ErrorStat `json:"past6h"`
}
type FrequencyRule struct {
Amount int64 `json:"amount"`
Interval IntervalType `json:"interval"`
Expand Down
19 changes: 19 additions & 0 deletions proto/autogenerated/ts/express_server.ts
Original file line number Diff line number Diff line change
Expand Up @@ -885,6 +885,25 @@ export default (methods: Types.ServerMethods, opts: ServerOptions) => {
opts.metricsCallback([{ ...info, ...stats, ...authContext }])
} catch (ex) { const e = ex as any; logErrorAndReturnResponse(e, e.message || e, res, logger, { ...info, ...stats, ...authCtx }, opts.metricsCallback); if (opts.throwErrors) throw e }
})
if (!opts.allowNotImplementedMethods && !methods.GetErrorStats) throw new Error('method: GetErrorStats is not implemented')
app.post('/api/reports/errors', async (req, res) => {
const info: Types.RequestInfo = { rpcName: 'GetErrorStats', batch: false, nostr: false, batchSize: 0}
const stats: Types.RequestStats = { startMs:req.startTimeMs || 0, start:req.startTime || 0n, parse: process.hrtime.bigint(), guard: 0n, validate: 0n, handle: 0n }
let authCtx: Types.AuthContext = {}
try {
if (!methods.GetErrorStats) throw new Error('method: GetErrorStats is not implemented')
const authContext = await opts.MetricsAuthGuard(req.headers['authorization'])
authCtx = authContext
stats.guard = process.hrtime.bigint()
stats.validate = stats.guard
const query = req.query
const params = req.params
const response = await methods.GetErrorStats({rpcName:'GetErrorStats', ctx:authContext })
stats.handle = process.hrtime.bigint()
res.json({status: 'OK', ...response})
opts.metricsCallback([{ ...info, ...stats, ...authContext }])
} catch (ex) { const e = ex as any; logErrorAndReturnResponse(e, e.message || e, res, logger, { ...info, ...stats, ...authCtx }, opts.metricsCallback); if (opts.throwErrors) throw e }
})
if (!opts.allowNotImplementedMethods && !methods.GetInviteLinkState) throw new Error('method: GetInviteLinkState is not implemented')
app.post('/api/admin/app/invite/get', async (req, res) => {
const info: Types.RequestInfo = { rpcName: 'GetInviteLinkState', batch: false, nostr: false, batchSize: 0}
Expand Down
14 changes: 14 additions & 0 deletions proto/autogenerated/ts/http_client.ts
Original file line number Diff line number Diff line change
Expand Up @@ -332,6 +332,20 @@ export default (params: ClientParams) => ({
}
return { status: 'ERROR', reason: 'invalid response' }
},
GetErrorStats: async (): Promise<ResultError | ({ status: 'OK' }& Types.ErrorStats)> => {
const auth = await params.retrieveMetricsAuth()
if (auth === null) throw new Error('retrieveMetricsAuth() returned null')
let finalRoute = '/api/reports/errors'
const { data } = await axios.post(params.baseUrl + finalRoute, {}, { headers: { 'authorization': auth } })
if (data.status === 'ERROR' && typeof data.reason === 'string') return data
if (data.status === 'OK') {
const result = data
if(!params.checkResult) return { status: 'OK', ...result }
const error = Types.ErrorStatsValidate(result)
if (error === null) { return { status: 'OK', ...result } } else return { status: 'ERROR', reason: error.message }
}
return { status: 'ERROR', reason: 'invalid response' }
},
GetHttpCreds: async (cb: (v:ResultError | ({ status: 'OK' }& Types.HttpCreds)) => void): Promise<void> => { throw new Error('http streams are not supported')},
GetInviteLinkState: async (request: Types.GetInviteTokenStateRequest): Promise<ResultError | ({ status: 'OK' }& Types.GetInviteTokenStateResponse)> => {
const auth = await params.retrieveAdminAuth()
Expand Down
14 changes: 14 additions & 0 deletions proto/autogenerated/ts/nostr_client.ts
Original file line number Diff line number Diff line change
Expand Up @@ -247,6 +247,20 @@ export default (params: NostrClientParams, send: (to:string, message: NostrRequ
}
return { status: 'ERROR', reason: 'invalid response' }
},
GetErrorStats: async (): Promise<ResultError | ({ status: 'OK' }& Types.ErrorStats)> => {
const auth = await params.retrieveNostrMetricsAuth()
if (auth === null) throw new Error('retrieveNostrMetricsAuth() returned null')
const nostrRequest: NostrRequest = {}
const data = await send(params.pubDestination, {rpcName:'GetErrorStats',authIdentifier:auth, ...nostrRequest })
if (data.status === 'ERROR' && typeof data.reason === 'string') return data
if (data.status === 'OK') {
const result = data
if(!params.checkResult) return { status: 'OK', ...result }
const error = Types.ErrorStatsValidate(result)
if (error === null) { return { status: 'OK', ...result } } else return { status: 'ERROR', reason: error.message }
}
return { status: 'ERROR', reason: 'invalid response' }
},
GetHttpCreds: async (cb: (res:ResultError | ({ status: 'OK' }& Types.HttpCreds)) => void): Promise<void> => {
const auth = await params.retrieveNostrUserAuth()
if (auth === null) throw new Error('retrieveNostrUserAuth() returned null')
Expand Down
13 changes: 13 additions & 0 deletions proto/autogenerated/ts/nostr_transport.ts
Original file line number Diff line number Diff line change
Expand Up @@ -634,6 +634,19 @@ export default (methods: Types.ServerMethods, opts: NostrOptions) => {
opts.metricsCallback([{ ...info, ...stats, ...authContext }])
}catch(ex){ const e = ex as any; logErrorAndReturnResponse(e, e.message || e, res, logger, { ...info, ...stats, ...authCtx }, opts.metricsCallback); if (opts.throwErrors) throw e }
break
case 'GetErrorStats':
try {
if (!methods.GetErrorStats) throw new Error('method: GetErrorStats is not implemented')
const authContext = await opts.NostrMetricsAuthGuard(req.appId, req.authIdentifier)
stats.guard = process.hrtime.bigint()
authCtx = authContext
stats.validate = stats.guard
const response = await methods.GetErrorStats({rpcName:'GetErrorStats', ctx:authContext })
stats.handle = process.hrtime.bigint()
res({status: 'OK', ...response})
opts.metricsCallback([{ ...info, ...stats, ...authContext }])
}catch(ex){ const e = ex as any; logErrorAndReturnResponse(e, e.message || e, res, logger, { ...info, ...stats, ...authCtx }, opts.metricsCallback); if (opts.throwErrors) throw e }
break
case 'GetHttpCreds':
try {
if (!methods.GetHttpCreds) throw new Error('method: GetHttpCreds is not implemented')
Expand Down
79 changes: 77 additions & 2 deletions proto/autogenerated/ts/types.ts
Original file line number Diff line number Diff line change
Expand Up @@ -27,8 +27,8 @@ export type GuestWithPubMethodOutputs = LinkNPubThroughToken_Output | UseInviteL
export type MetricsContext = {
operator_id: string
}
export type MetricsMethodInputs = GetAppsMetrics_Input | GetLndMetrics_Input | GetUsageMetrics_Input
export type MetricsMethodOutputs = GetAppsMetrics_Output | GetLndMetrics_Output | GetUsageMetrics_Output
export type MetricsMethodInputs = GetAppsMetrics_Input | GetErrorStats_Input | GetLndMetrics_Input | GetUsageMetrics_Input
export type MetricsMethodOutputs = GetAppsMetrics_Output | GetErrorStats_Output | GetLndMetrics_Output | GetUsageMetrics_Output
export type UserContext = {
app_id: string
app_user_id: string
Expand Down Expand Up @@ -110,6 +110,9 @@ export type GetAppsMetrics_Output = ResultError | ({ status: 'OK' } & AppsMetric
export type GetDebitAuthorizations_Input = {rpcName:'GetDebitAuthorizations'}
export type GetDebitAuthorizations_Output = ResultError | ({ status: 'OK' } & DebitAuthorizations)

export type GetErrorStats_Input = {rpcName:'GetErrorStats'}
export type GetErrorStats_Output = ResultError | ({ status: 'OK' } & ErrorStats)

export type GetHttpCreds_Input = {rpcName:'GetHttpCreds', cb:(res: HttpCreds, err:Error|null)=> void}
export type GetHttpCreds_Output = ResultError | { status: 'OK' }

Expand Down Expand Up @@ -300,6 +303,7 @@ export type ServerMethods = {
GetAppUserLNURLInfo?: (req: GetAppUserLNURLInfo_Input & {ctx: AppContext }) => Promise<LnurlPayInfoResponse>
GetAppsMetrics?: (req: GetAppsMetrics_Input & {ctx: MetricsContext }) => Promise<AppsMetrics>
GetDebitAuthorizations?: (req: GetDebitAuthorizations_Input & {ctx: UserContext }) => Promise<DebitAuthorizations>
GetErrorStats?: (req: GetErrorStats_Input & {ctx: MetricsContext }) => Promise<ErrorStats>
GetHttpCreds?: (req: GetHttpCreds_Input & {ctx: UserContext }) => Promise<void>
GetInviteLinkState?: (req: GetInviteLinkState_Input & {ctx: AdminContext }) => Promise<GetInviteTokenStateResponse>
GetLNURLChannelLink?: (req: GetLNURLChannelLink_Input & {ctx: UserContext }) => Promise<LnurlLinkResponse>
Expand Down Expand Up @@ -1371,6 +1375,77 @@ export const EnrollAdminTokenRequestValidate = (o?: EnrollAdminTokenRequest, opt
return null
}

export type ErrorStat = {
errors: number
from_unix: number
total: number
}
export const ErrorStatOptionalFields: [] = []
export type ErrorStatOptions = OptionsBaseMessage & {
checkOptionalsAreSet?: []
errors_CustomCheck?: (v: number) => boolean
from_unix_CustomCheck?: (v: number) => boolean
total_CustomCheck?: (v: number) => boolean
}
export const ErrorStatValidate = (o?: ErrorStat, opts: ErrorStatOptions = {}, path: string = 'ErrorStat::root.'): Error | null => {
if (opts.checkOptionalsAreSet && opts.allOptionalsAreSet) return new Error(path + ': only one of checkOptionalsAreSet or allOptionalNonDefault can be set for each message')
if (typeof o !== 'object' || o === null) return new Error(path + ': object is not an instance of an object or is null')

if (typeof o.errors !== 'number') return new Error(`${path}.errors: is not a number`)
if (opts.errors_CustomCheck && !opts.errors_CustomCheck(o.errors)) return new Error(`${path}.errors: custom check failed`)

if (typeof o.from_unix !== 'number') return new Error(`${path}.from_unix: is not a number`)
if (opts.from_unix_CustomCheck && !opts.from_unix_CustomCheck(o.from_unix)) return new Error(`${path}.from_unix: custom check failed`)

if (typeof o.total !== 'number') return new Error(`${path}.total: is not a number`)
if (opts.total_CustomCheck && !opts.total_CustomCheck(o.total)) return new Error(`${path}.total: custom check failed`)

return null
}

export type ErrorStats = {
past10m: ErrorStat
past1h: ErrorStat
past1m: ErrorStat
past24h: ErrorStat
past6h: ErrorStat
}
export const ErrorStatsOptionalFields: [] = []
export type ErrorStatsOptions = OptionsBaseMessage & {
checkOptionalsAreSet?: []
past10m_Options?: ErrorStatOptions
past1h_Options?: ErrorStatOptions
past1m_Options?: ErrorStatOptions
past24h_Options?: ErrorStatOptions
past6h_Options?: ErrorStatOptions
}
export const ErrorStatsValidate = (o?: ErrorStats, opts: ErrorStatsOptions = {}, path: string = 'ErrorStats::root.'): Error | null => {
if (opts.checkOptionalsAreSet && opts.allOptionalsAreSet) return new Error(path + ': only one of checkOptionalsAreSet or allOptionalNonDefault can be set for each message')
if (typeof o !== 'object' || o === null) return new Error(path + ': object is not an instance of an object or is null')

const past10mErr = ErrorStatValidate(o.past10m, opts.past10m_Options, `${path}.past10m`)
if (past10mErr !== null) return past10mErr


const past1hErr = ErrorStatValidate(o.past1h, opts.past1h_Options, `${path}.past1h`)
if (past1hErr !== null) return past1hErr


const past1mErr = ErrorStatValidate(o.past1m, opts.past1m_Options, `${path}.past1m`)
if (past1mErr !== null) return past1mErr


const past24hErr = ErrorStatValidate(o.past24h, opts.past24h_Options, `${path}.past24h`)
if (past24hErr !== null) return past24hErr


const past6hErr = ErrorStatValidate(o.past6h, opts.past6h_Options, `${path}.past6h`)
if (past6hErr !== null) return past6hErr


return null
}

export type FrequencyRule = {
amount: number
interval: IntervalType
Expand Down
6 changes: 6 additions & 0 deletions proto/service/methods.proto
Original file line number Diff line number Diff line change
Expand Up @@ -178,6 +178,12 @@ service LightningPub {
option (http_route) = "/api/reports/usage";
option (nostr) = true;
}
rpc GetErrorStats(structs.Empty) returns (structs.ErrorStats) {
option (auth_type) = "Metrics";
option (http_method) = "post";
option (http_route) = "/api/reports/errors";
option (nostr) = true;
}

rpc GetAppsMetrics(structs.AppsMetricsRequest) returns (structs.AppsMetrics) {
option (auth_type) = "Metrics";
Expand Down
14 changes: 14 additions & 0 deletions proto/service/structs.proto
Original file line number Diff line number Diff line change
Expand Up @@ -19,6 +19,20 @@ message UserHealthState {
string downtime_reason = 1;
}

message ErrorStat {
int64 from_unix = 1;
int64 total = 2;
int64 errors = 3;
}

message ErrorStats {
ErrorStat past24h = 1;
ErrorStat past6h = 2;
ErrorStat past1h = 3;
ErrorStat past10m = 4;
ErrorStat past1m = 5;
}

message UsageMetric {
int64 processed_at_ms = 1;
int64 parsed_in_nano = 2;
Expand Down
44 changes: 43 additions & 1 deletion src/services/metrics/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -72,6 +72,48 @@ export default class Handler {
return this.storage.metricsEventStorage.LoadLatestMetrics()
}

async GetErrorStats(): Promise<Types.ErrorStats> {
const last24h = this.storage.metricsEventStorage.getlast24hCache()
const nowUnix = Math.floor(Date.now() / 1000)
const stats: Types.ErrorStats = {
past24h: { errors: 0, total: 0, from_unix: nowUnix - 60 * 60 * 24 },
past6h: { errors: 0, total: 0, from_unix: nowUnix - 60 * 60 * 6 },
past1h: { errors: 0, total: 0, from_unix: nowUnix - 60 * 60 },
past10m: { errors: 0, total: 0, from_unix: nowUnix - 60 * 10 },
past1m: { errors: 0, total: 0, from_unix: nowUnix - 60 },
}
for (let i = last24h.length; i >= 0; i--) {
const e = last24h[i]
if (e.ts < stats.past24h.from_unix) {
break
}

stats.past24h.total += e.ok + e.fail
stats.past24h.errors += e.fail

if (e.ts >= stats.past6h.from_unix) {
stats.past6h.total += e.ok + e.fail
stats.past6h.errors += e.fail
}

if (e.ts >= stats.past1h.from_unix) {
stats.past1h.total += e.ok + e.fail
stats.past1h.errors += e.fail
}

if (e.ts >= stats.past10m.from_unix) {
stats.past10m.total += e.ok + e.fail
stats.past10m.errors += e.fail
}

if (e.ts >= stats.past1m.from_unix) {
stats.past1m.total += e.ok + e.fail
stats.past1m.errors += e.fail
}
}
return stats
}



AddMetrics(newMetrics: (Types.RequestMetric & { app_id?: string })[]) {
Expand All @@ -91,7 +133,7 @@ export default class Handler {
processed_at_ms: m.startMs
}
const tlv = usageMetricsToTlv(um)
this.storage.metricsEventStorage.AddMetricEvent(appId, m.rpcName, encodeTLV(tlv))
this.storage.metricsEventStorage.AddMetricEvent(appId, m.rpcName, encodeTLV(tlv), !m.error)
})
}

Expand Down
Loading

0 comments on commit 16d4198

Please sign in to comment.