github.com/grafana/pyroscope@v1.18.0/public/app/services/flamegraphcom.ts (about)

     1  import { parseResponse, RequestNotOkError } from '@pyroscope/services/base';
     2  import { z, ZodError } from 'zod';
     3  import { Result } from '@pyroscope/util/fp';
     4  import type { RequestError } from '@pyroscope/services/base';
     5  import { Profile } from '@pyroscope/legacy/models';
     6  
     7  export async function flameGraphUpload(
     8    name: string,
     9    flamebearer: Profile
    10  ): Promise<Result<string, RequestError | ZodError>> {
    11    const response = await fetch('https://flamegraph.com/api/upload/v1', {
    12      method: 'POST',
    13      headers: {
    14        'content-type': 'application/json',
    15      },
    16      body: JSON.stringify({
    17        fileTypeData: {
    18          units: flamebearer.metadata.units,
    19          spyName: flamebearer.metadata.spyName,
    20        },
    21        name,
    22        profile: btoa(JSON.stringify(flamebearer)),
    23        type: 'json',
    24      }),
    25    });
    26    if (!response.ok) {
    27      return Result.err(
    28        new RequestNotOkError(
    29          response.status,
    30          `Failed to upload to flamegraph.com: ${response.statusText}`
    31        )
    32      );
    33    }
    34    const body = await response.text();
    35    return parseResponse(
    36      Result.ok(JSON.parse(body)),
    37      z
    38        .preprocess(
    39          (arg) => {
    40            return arg;
    41          },
    42          z.object({
    43            key: z.string(),
    44            url: z.string(),
    45          })
    46        )
    47        .transform((arg) => arg.url)
    48    );
    49  }