Files
speckle-server/packages/frontend-2/lib/core/composables/server.ts
T
Kristaps Fabians Geikins 0a63afb6aa feat: various observability improvements (#2027)
* feat(server): adding userId to req logs

* feat(server): adding userId to gql logs

* feat(fe2): adding userId to logger calls

* feat(fe2): more userId logging additions

* even more thorough logging in FE2

* more adjustments

* add country to fe2 logs

* added prop to help distinguish gql req time logs

* get initial SSR req id in CSR logs

* improved 'fetch failed' error

* better rate limit error message

* minor improvements
2024-02-09 13:56:13 +02:00

93 lines
2.7 KiB
TypeScript

import { useQuery } from '@vue/apollo-composable'
import { nanoid } from 'nanoid'
import { graphql } from '~~/lib/common/generated/gql'
import type { H3Event } from 'h3'
import type { Optional } from '@speckle/shared'
export const mainServerInfoDataQuery = graphql(`
query MainServerInfoData {
serverInfo {
adminContact
blobSizeLimitBytes
canonicalUrl
company
description
guestModeEnabled
inviteOnly
name
termsOfService
version
automateUrl
}
}
`)
export function useServerInfo() {
const { result } = useQuery(mainServerInfoDataQuery)
const serverInfo = computed(() => result.value?.serverInfo)
const isGuestMode = computed(() => !!serverInfo.value?.guestModeEnabled)
return { serverInfo, isGuestMode }
}
/**
* Get the req.id that is/was used in the initial SSR request
*
* Note: In SSR, this can only be used after the 001-logging middleware, cause that's when the ID is set
*/
export function useServerRequestId() {
const nuxt = useNuxtApp()
const state = useState('server_request_id', () => {
// The client side should not need to resolve this info, as it should come from the serialized SSR state
if (process.client || !nuxt.ssrContext) return undefined
return nuxt.ssrContext.event.node.req.id as string
})
return computed(() => state.value)
}
/**
* Get the value that should be used as the request correlation ID
*
* Note: In SSR, this can only be used after the 001-logging middleware, cause that's when the ID is set, otherwise
* we fallback to a new nanoid
*/
export function useRequestId(params?: {
/**
* Specify, if invoking composable from within server-side event handler, cause we don't have access to useNuxtApp()
* or anything of the like there
*/
event?: H3Event
}) {
let id = nanoid()
if (process.server) {
id = (params?.event || useNuxtApp().ssrContext?.event)?.node.req.id as string
if (!id) {
throw new Error("Couldn't determine request ID")
}
return id
} else {
// We retain it in the state so that all reqs going out from the same client-side app session
// have the same id
const state = useState('app_request_correlation_id', () => id)
return state.value
}
}
/**
* Resolve the user's geolocation, if possible (only supported wherever we host behind Cloudflare)
*/
export function useUserCountry() {
const nuxt = useNuxtApp()
const state = useState('active_user_country', () => {
// The client side should not need to resolve this info, as it should come from the serialized SSR state
if (process.client || !nuxt.ssrContext) return undefined
return nuxt.ssrContext.event.node.req.headers['cf-ipcountry'] as Optional<string>
})
return computed(() => state.value)
}