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