Merged in fix/LOY-287-Telemetry-DTMC (pull request #2631)
fix(LOY-287): telemetry logging for DTMC flow * fix(LOY-287): telemetry logging for DTMC flow Approved-by: Erik Tiekstra Approved-by: Matilda Landström
This commit is contained in:
@@ -2,6 +2,7 @@ import { type NextRequest, NextResponse } from "next/server"
|
||||
|
||||
import { overview } from "@scandic-hotels/common/constants/routes/myPages"
|
||||
import { createLogger } from "@scandic-hotels/common/logger/createLogger"
|
||||
import { createCounter } from "@scandic-hotels/common/telemetry"
|
||||
import * as api from "@scandic-hotels/trpc/api"
|
||||
import { isValidSession } from "@scandic-hotels/trpc/utils/session"
|
||||
|
||||
@@ -34,6 +35,10 @@ async function linkEmployeeToUser(
|
||||
employeeId: string,
|
||||
accessToken: string
|
||||
): Promise<LinkEmployeeResult> {
|
||||
const apiCallCounter = createCounter("dtmc.api", "team_member_card")
|
||||
const metricsApiCall = apiCallCounter.init({ employee_id: employeeId })
|
||||
|
||||
metricsApiCall.start()
|
||||
dtmcLogger.debug(`Linking employee ID ${employeeId}`)
|
||||
let response: Response
|
||||
try {
|
||||
@@ -47,6 +52,7 @@ async function linkEmployeeToUser(
|
||||
}
|
||||
)
|
||||
} catch (networkError) {
|
||||
metricsApiCall.fail(networkError)
|
||||
dtmcLogger.error("Network error during API request:", networkError)
|
||||
return {
|
||||
success: false,
|
||||
@@ -55,6 +61,7 @@ async function linkEmployeeToUser(
|
||||
}
|
||||
|
||||
if (!response.ok) {
|
||||
await metricsApiCall.httpError(response)
|
||||
dtmcLogger.error(`API returned error status ${response.status}`)
|
||||
try {
|
||||
const errorResponse = await response.json()
|
||||
@@ -101,6 +108,7 @@ async function linkEmployeeToUser(
|
||||
dtmcLogger.warn(`Could not parse success response body:`, parseError)
|
||||
}
|
||||
|
||||
metricsApiCall.success()
|
||||
dtmcLogger.debug(`Successfully linked employee ID ${employeeId}`)
|
||||
return { success: true }
|
||||
}
|
||||
@@ -111,6 +119,11 @@ async function linkEmployeeToUser(
|
||||
* here we can check both sessions, the Scandic Friends one and the Azure one.
|
||||
*/
|
||||
export async function GET(request: NextRequest) {
|
||||
const dtmcLinkCounter = createCounter("dtmc.employee", "link")
|
||||
const metricsEmployeeLink = dtmcLinkCounter.init()
|
||||
|
||||
metricsEmployeeLink.start()
|
||||
|
||||
try {
|
||||
const lang = await getLang()
|
||||
const dtmcSession = await dtmcAuth()
|
||||
@@ -119,6 +132,7 @@ export async function GET(request: NextRequest) {
|
||||
dtmcLogger.debug("DTMC Callback handler - using baseUrl:", baseUrl)
|
||||
|
||||
if (!isValidSession(session)) {
|
||||
metricsEmployeeLink.noDataError({ error_detail: "no_curity_session" })
|
||||
dtmcLogger.error("DTMC Callback handler - No valid user session found")
|
||||
const errorUrl = new URL(linkEmploymentError[lang], baseUrl)
|
||||
errorUrl.searchParams.set("error", "no_session")
|
||||
@@ -126,6 +140,7 @@ export async function GET(request: NextRequest) {
|
||||
}
|
||||
|
||||
if (!isValidSession(dtmcSession)) {
|
||||
metricsEmployeeLink.noDataError({ error_detail: "no_dtmc_session" })
|
||||
dtmcLogger.error(
|
||||
"DTMC Callback handler - No valid entra id session found"
|
||||
)
|
||||
@@ -142,6 +157,7 @@ export async function GET(request: NextRequest) {
|
||||
)
|
||||
|
||||
if (!employeeId) {
|
||||
metricsEmployeeLink.dataError("missing_employee_id")
|
||||
dtmcLogger.error("DTMC Callback handler - No employeeId in session")
|
||||
const errorUrl = new URL(linkEmploymentError[lang], baseUrl)
|
||||
errorUrl.searchParams.set("error", "missing_employee_id")
|
||||
@@ -155,6 +171,7 @@ export async function GET(request: NextRequest) {
|
||||
|
||||
const accessToken = session.token.access_token
|
||||
if (!accessToken) {
|
||||
metricsEmployeeLink.dataError("missing_access_token")
|
||||
dtmcLogger.error("DTMC Callback handler - No access token in session")
|
||||
const errorUrl = new URL(linkEmploymentError[lang], baseUrl)
|
||||
errorUrl.searchParams.set("error", "missing_access_token")
|
||||
@@ -168,6 +185,9 @@ export async function GET(request: NextRequest) {
|
||||
)
|
||||
|
||||
if (!result.success) {
|
||||
metricsEmployeeLink.fail(
|
||||
`API call failed with status ${result.statusCode}`
|
||||
)
|
||||
dtmcLogger.error(
|
||||
"DTMC Callback handler - Failed to verify employment:",
|
||||
`Status: ${result.statusCode}, Error: ${result.queryParam}`
|
||||
@@ -182,6 +202,7 @@ export async function GET(request: NextRequest) {
|
||||
return NextResponse.redirect(errorUrl)
|
||||
}
|
||||
|
||||
metricsEmployeeLink.success({ employee_linked: true })
|
||||
dtmcLogger.debug(
|
||||
"DTMC Callback handler - Success! Employee linked with ID:",
|
||||
employeeId
|
||||
@@ -196,6 +217,7 @@ export async function GET(request: NextRequest) {
|
||||
)
|
||||
return NextResponse.redirect(successUrl)
|
||||
} catch (error) {
|
||||
metricsEmployeeLink.fail(error)
|
||||
dtmcLogger.error("DTMC Callback handler - Error in handler:", error)
|
||||
return internalServerError()
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user