Repository: prisma/prisma
File: packages/client/tests/functional/tracing/tests.ts (TypeScript)

Code before:
import type { PrismaClient } from './generated/prisma/client'

type Tree = {
  name: string
  kind?: string | undefined
  attributes?: Attributes
  children?: Tree[]
}

function buildTree(rootSpan: ReadableSpan, spans: ReadableSpan[]): Tree {
  const childrenSpans = spans
    .sort((a, b) => {
      const normalizeNameForComparison = (name: string) => {
        // Replace client engine specific spans with their classic names as sort keys
        // to ensure stable order regardless of the engine type.
        for (const prefix of ['prisma:client', 'prisma:accelerate']) {
          if (
            [
              `${prefix}:db_query`,
              `${prefix}:start_transaction`,
              `${prefix}:commit_transaction`,
              `${prefix}:rollback_transaction`,
            ].includes(name)
          ) {
            return 'prisma:engine:' + name.slice(`${prefix}:`.length)
          }
        }
        return name
      }
      // Ensures fixed order of children spans regardless of the
      // actual timings. Why use name instead of start time? Sometimes
      // things happen in parallel/different order and startTime does not
      // provide stable ordering guarantee.
      return normalizeNameForComparison(a.name).localeCompare(normalizeNameForComparison(b.name))
    })
    .filter((span) => span.parentSpanContext?.spanId === rootSpan.spanContext().spanId)

  const tree: Tree = {
    name: rootSpan.name,
  }

  if (childrenSpans.length > 0) {
    tree.children = childrenSpans.map((span) => buildTree(span, spans))
  }

  if (Object.keys(rootSpan.attributes).length > 0) {
    tree.attributes = rootSpan.attributes
  }

  if (rootSpan.kind !== SpanKind.INTERNAL) {
    tree.kind = SpanKind[rootSpan.kind]

Review comment (refactor):
instead of doing this, I'd define a const in the test
```ts
const clientSpanName = clientEngineExecutor === 'remote' ? 'accelerate' : 'client'
```
and then just have
```ts
          [
            `prisma:${clientSpanName}:db_query`,
             ...
          ].includes(name)
```

it could also simplify other code like replace `maybeRemoteSpan('db_query')` with just `prisma:${clientSpanName}:db_query`

Code after:
import type { PrismaClient } from './generated/prisma/client'

type Tree = {
  name: string
  kind?: string | undefined
  attributes?: Attributes
  children?: Tree[]
}

declare let prisma: PrismaClient
declare const newPrismaClient: NewPrismaClient<PrismaClient, typeof PrismaClient>

let inMemorySpanExporter: InMemorySpanExporter
let processor: SpanProcessor

beforeAll(() => {
  const contextManager = new AsyncLocalStorageContextManager().enable()
  context.setGlobalContextManager(contextManager)

  inMemorySpanExporter = new InMemorySpanExporter()
  processor = new SimpleSpanProcessor(inMemorySpanExporter)

  const basicTracerProvider = new BasicTracerProvider({
    resource: resourceFromAttributes({
      [ATTR_SERVICE_NAME]: 'test-name',
      [ATTR_SERVICE_VERSION]: '1.0.0',
    }),
    spanProcessors: [processor],
  })

  trace.setGlobalTracerProvider(basicTracerProvider)

  registerInstrumentations({
    instrumentations: [new PrismaInstrumentation()],
  })
})

afterAll(() => {
  context.disable()
})

testMatrix.setupTestSuite(
  ({ provider, driverAdapter, relationMode, clientEngineExecutor }, _suiteMeta, clientMeta) => {
    const executorSpanInfix = clientEngineExecutor === 'remote' ? 'accelerate' : 'client'

    function buildTree(rootSpan: ReadableSpan, spans: ReadableSpan[]): Tree {
      const childrenSpans = spans
        .sort((a, b) => {
          const normalizeNameForComparison = (name: string) => {
            // Replace client engine specific spans with their classic names as sort keys
            // to ensure stable order regardless of the engine type.