diff --git a/apps/search/package.json b/apps/search/package.json index 4698ed1..e73ea96 100644 --- a/apps/search/package.json +++ b/apps/search/package.json @@ -19,6 +19,7 @@ }, "dependencies": { "@hookform/resolvers": "^3.1.0", + "@opentelemetry/api": "^1.4.1", "@opentelemetry/exporter-trace-otlp-http": "^0.41.1", "@opentelemetry/resources": "^1.15.1", "@opentelemetry/sdk-node": "^0.41.1", diff --git a/apps/search/src/otel.ts b/apps/search/src/otel.ts index ac68115..fa2fa39 100644 --- a/apps/search/src/otel.ts +++ b/apps/search/src/otel.ts @@ -3,6 +3,7 @@ import { OTLPTraceExporter } from "@opentelemetry/exporter-trace-otlp-http"; import { Resource } from "@opentelemetry/resources"; import { SemanticResourceAttributes } from "@opentelemetry/semantic-conventions"; import { SimpleSpanProcessor } from "@opentelemetry/sdk-trace-node"; +import { trace } from "@opentelemetry/api"; const sdk = new NodeSDK({ resource: new Resource({ @@ -12,3 +13,5 @@ const sdk = new NodeSDK({ }); sdk.start(); + +export const tracer = trace.getTracer("saleor.app.search"); diff --git a/apps/search/src/pages/api/configuration.ts b/apps/search/src/pages/api/configuration.ts index 971af83..632d783 100644 --- a/apps/search/src/pages/api/configuration.ts +++ b/apps/search/src/pages/api/configuration.ts @@ -9,6 +9,7 @@ import { AppConfigurationFields, AppConfigurationSchema } from "../../domain/con import { AlgoliaSearchProvider } from "../../lib/algolia/algoliaSearchProvider"; import { WebhookActivityTogglerService } from "../../domain/WebhookActivityToggler.service"; import { algoliaConfigurationRepository } from "../../domain/algolia-configuration/AlgoliaConfigurationRepository"; +import { tracer } from "../../otel"; const logger = createLogger({ handler: "api/configuration", @@ -27,6 +28,8 @@ export const handler = async ( const client = createClient(saleorApiUrl, async () => Promise.resolve({ token: token })); + console.log(req.method); + // todo extract endpoints, add trpc if (req.method === "GET") { logger.debug("Returning configuration"); @@ -42,47 +45,61 @@ export const handler = async ( success: false, }); } else if (req.method === "POST") { - logger.debug("Updating the configuration"); + tracer.startActiveSpan("update-configuration", async (span) => { + console.log("test log console"); - const { appId, secretKey, indexNamePrefix } = JSON.parse(req.body) as AppConfigurationFields; + span.addEvent("update-configuration POST", { + foo: "BAR", + }); - const algoliaClient = new AlgoliaSearchProvider({ - appId, - apiKey: secretKey, - indexNamePrefix: indexNamePrefix, - }); + logger.debug("Updating the configuration - log logger pino"); - try { - logger.debug("Will ping Algolia"); - await algoliaClient.ping(); + const { appId, secretKey, indexNamePrefix } = JSON.parse(req.body) as AppConfigurationFields; - logger.debug("Algolia connection is ok. Will save settings"); - - const configuration = await algoliaConfigurationRepository.setConfiguration(saleorApiUrl, { + const algoliaClient = new AlgoliaSearchProvider({ appId, - secretKey, - indexNamePrefix, + apiKey: secretKey, + indexNamePrefix: indexNamePrefix, }); - logger.debug("Settings set"); + try { + logger.info("Will ping Algolia"); + await algoliaClient.ping(); - const webhooksToggler = new WebhookActivityTogglerService(ctx.authData.appId, client); + logger.debug("Algolia connection is ok. Will save settings"); - await webhooksToggler.enableOwnWebhooks(); + const configuration = await algoliaConfigurationRepository.setConfiguration(saleorApiUrl, { + appId, + secretKey, + indexNamePrefix, + }); - logger.debug("Webhooks enabled"); + logger.debug("Settings set"); - return res.status(200).send({ - success: true, - data: AppConfigurationSchema.parse(configuration), // todo probably remove Zod at this point - }); - } catch (e) { - return res.status(400).end(); - } + const webhooksToggler = new WebhookActivityTogglerService(ctx.authData.appId, client); + + await webhooksToggler.enableOwnWebhooks(); + + logger.debug("Webhooks enabled"); + + res.status(200).send({ + success: true, + data: AppConfigurationSchema.parse(configuration), // todo probably remove Zod at this point + }); + + span.end(); + return; + } catch (e) { + return res.status(400).end(); + } + + return; + }); + } else { + logger.error("Method not supported"); + + return res.status(405).end(); } - logger.error("Method not supported"); - - res.status(405).end(); }; export default createProtectedHandler(handler, saleorApp.apl, ["MANAGE_APPS", "MANAGE_PRODUCTS"]); diff --git a/pnpm-lock.yaml b/pnpm-lock.yaml index a0dafd8..d232e6d 100644 --- a/pnpm-lock.yaml +++ b/pnpm-lock.yaml @@ -1170,6 +1170,9 @@ importers: '@hookform/resolvers': specifier: ^3.1.0 version: 3.1.0(react-hook-form@7.43.9) + '@opentelemetry/api': + specifier: ^1.4.1 + version: 1.4.1 '@opentelemetry/exporter-trace-otlp-http': specifier: ^0.41.1 version: 0.41.1(@opentelemetry/api@1.4.1) @@ -4842,7 +4845,7 @@ packages: /@changesets/apply-release-plan@6.1.3: resolution: {integrity: sha512-ECDNeoc3nfeAe1jqJb5aFQX7CqzQhD2klXRez2JDb/aVpGUbX673HgKrnrgJRuQR/9f2TtLoYIzrGB9qwD77mg==} dependencies: - '@babel/runtime': 7.22.3 + '@babel/runtime': 7.22.5 '@changesets/config': 2.3.0 '@changesets/get-version-range-type': 0.3.2 '@changesets/git': 2.0.0 @@ -4877,7 +4880,7 @@ packages: /@changesets/assemble-release-plan@5.2.3: resolution: {integrity: sha512-g7EVZCmnWz3zMBAdrcKhid4hkHT+Ft1n0mLussFMcB1dE2zCuwcvGoy9ec3yOgPGF4hoMtgHaMIk3T3TBdvU9g==} dependencies: - '@babel/runtime': 7.22.3 + '@babel/runtime': 7.22.5 '@changesets/errors': 0.1.4 '@changesets/get-dependents-graph': 1.3.5 '@changesets/types': 5.2.1 @@ -5027,7 +5030,7 @@ packages: /@changesets/get-release-plan@3.0.16: resolution: {integrity: sha512-OpP9QILpBp1bY2YNIKFzwigKh7Qe9KizRsZomzLe6pK8IUo8onkAAVUD8+JRKSr8R7d4+JRuQrfSSNlEwKyPYg==} dependencies: - '@babel/runtime': 7.22.3 + '@babel/runtime': 7.22.5 '@changesets/assemble-release-plan': 5.2.3 '@changesets/config': 2.3.0 '@changesets/pre': 1.0.14 @@ -5053,7 +5056,7 @@ packages: /@changesets/git@2.0.0: resolution: {integrity: sha512-enUVEWbiqUTxqSnmesyJGWfzd51PY4H7mH9yUw0hPVpZBJ6tQZFMU3F3mT/t9OJ/GjyiM4770i+sehAn6ymx6A==} dependencies: - '@babel/runtime': 7.22.3 + '@babel/runtime': 7.22.5 '@changesets/errors': 0.1.4 '@changesets/types': 5.2.1 '@manypkg/get-packages': 1.1.3 @@ -5075,7 +5078,7 @@ packages: /@changesets/pre@1.0.14: resolution: {integrity: sha512-dTsHmxQWEQekHYHbg+M1mDVYFvegDh9j/kySNuDKdylwfMEevTeDouR7IfHNyVodxZXu17sXoJuf2D0vi55FHQ==} dependencies: - '@babel/runtime': 7.22.3 + '@babel/runtime': 7.22.5 '@changesets/errors': 0.1.4 '@changesets/types': 5.2.1 '@manypkg/get-packages': 1.1.3 @@ -5084,7 +5087,7 @@ packages: /@changesets/read@0.5.9: resolution: {integrity: sha512-T8BJ6JS6j1gfO1HFq50kU3qawYxa4NTbI/ASNVVCBTsKquy2HYwM9r7ZnzkiMe8IEObAJtUVGSrePCOxAK2haQ==} dependencies: - '@babel/runtime': 7.22.3 + '@babel/runtime': 7.22.5 '@changesets/git': 2.0.0 '@changesets/logger': 0.0.5 '@changesets/parse': 0.3.16 @@ -5102,7 +5105,7 @@ packages: /@changesets/write@0.2.3: resolution: {integrity: sha512-Dbamr7AIMvslKnNYsLFafaVORx4H0pvCA2MHqgtNCySMe1blImEyAEOzDmcgKAkgz4+uwoLz7demIrX+JBr/Xw==} dependencies: - '@babel/runtime': 7.22.3 + '@babel/runtime': 7.22.5 '@changesets/types': 5.2.1 fs-extra: 7.0.1 human-id: 1.0.2 @@ -6659,7 +6662,7 @@ packages: /@manypkg/get-packages@1.1.3: resolution: {integrity: sha512-fo+QhuU3qE/2TQMQmbVMqaQ6EWbMhi4ABWP+O4AM1NqPBuy0OrApV5LO6BrrgnhtAHS2NH6RrVk9OL181tTi8A==} dependencies: - '@babel/runtime': 7.22.3 + '@babel/runtime': 7.22.5 '@changesets/types': 4.1.0 '@manypkg/find-root': 1.1.0 fs-extra: 8.1.0 @@ -6819,7 +6822,7 @@ packages: '@types/react': optional: true dependencies: - '@babel/runtime': 7.22.3 + '@babel/runtime': 7.22.5 '@emotion/hash': 0.8.0 '@material-ui/types': 5.1.0(@types/react@18.0.14) '@material-ui/utils': 4.11.3(react-dom@18.2.0)(react@18.2.0) @@ -6852,7 +6855,7 @@ packages: '@types/react': optional: true dependencies: - '@babel/runtime': 7.22.3 + '@babel/runtime': 7.22.5 '@emotion/hash': 0.8.0 '@material-ui/types': 5.1.0(@types/react@18.0.27) '@material-ui/utils': 4.11.3(react-dom@18.2.0)(react@18.2.0) @@ -6883,7 +6886,7 @@ packages: '@types/react': optional: true dependencies: - '@babel/runtime': 7.22.3 + '@babel/runtime': 7.22.5 '@material-ui/utils': 4.11.3(react-dom@18.2.0)(react@18.2.0) '@types/react': 18.0.14 csstype: 2.6.21 @@ -6903,7 +6906,7 @@ packages: '@types/react': optional: true dependencies: - '@babel/runtime': 7.22.3 + '@babel/runtime': 7.22.5 '@material-ui/utils': 4.11.3(react-dom@18.2.0)(react@18.2.0) '@types/react': 18.0.27 csstype: 2.6.21 @@ -6939,7 +6942,7 @@ packages: react: ^16.8.0 || ^17.0.0 react-dom: ^16.8.0 || ^17.0.0 dependencies: - '@babel/runtime': 7.22.3 + '@babel/runtime': 7.22.5 prop-types: 15.8.1 react: 18.2.0 react-dom: 18.2.0(react@18.2.0) @@ -10125,7 +10128,7 @@ packages: /@swc/helpers@0.4.14: resolution: {integrity: sha512-4C7nX/dvpzB7za4Ql9K81xK3HPxCpHMgwTZVyf+9JQ6VUbn9jjZVN7/Nkdz/Ugzs2CSjqnL/UPXroiVBVHUWUw==} dependencies: - tslib: 2.5.3 + tslib: 2.6.0 /@szmarczak/http-timer@4.0.6: resolution: {integrity: sha512-4BAffykYOgO+5nzBWYwE3W90sBgLJoUPRWWcL8wlyiM8IB8ipJz3UMJ9KXQd1RKQXpKp8Tutn80HZtWsu2u76w==} @@ -10183,7 +10186,7 @@ packages: engines: {node: '>=12'} dependencies: '@babel/code-frame': 7.21.4 - '@babel/runtime': 7.22.3 + '@babel/runtime': 7.22.5 '@types/aria-query': 5.0.1 aria-query: 5.1.3 chalk: 4.1.2 @@ -12208,7 +12211,7 @@ packages: /broadcast-channel@3.7.0: resolution: {integrity: sha512-cIAKJXAxGJceNZGTZSBzMxzyOn72cVgPnKx4dc6LRjQgbaJUQqhy5rzL3zbMxkMWsGKkv2hSFkPRMEXfoMZ2Mg==} dependencies: - '@babel/runtime': 7.22.3 + '@babel/runtime': 7.22.5 detect-node: 2.1.0 js-sha3: 0.8.0 microseconds: 0.2.0 @@ -16993,7 +16996,7 @@ packages: /match-sorter@6.3.1: resolution: {integrity: sha512-mxybbo3pPNuA+ZuCUhm5bwNkXrJTbsk5VWbR5wiwz/GC6LIiegBGn2w3O08UG/jdbYLinw51fSQ5xNU1U3MgBw==} dependencies: - '@babel/runtime': 7.22.3 + '@babel/runtime': 7.22.5 remove-accents: 0.4.2 dev: false @@ -19336,7 +19339,7 @@ packages: peerDependencies: react: '>=16.13.1' dependencies: - '@babel/runtime': 7.22.3 + '@babel/runtime': 7.22.5 react: 18.2.0 dev: true @@ -19696,7 +19699,7 @@ packages: react: '>=16.6.0' react-dom: '>=16.6.0' dependencies: - '@babel/runtime': 7.22.3 + '@babel/runtime': 7.22.5 dom-helpers: 5.2.1 loose-envify: 1.4.0 prop-types: 15.8.1