Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Hackathon - log drain #1373

Draft
wants to merge 36 commits into
base: main
Choose a base branch
from
Draft
Show file tree
Hide file tree
Changes from all commits
Commits
Show all changes
36 commits
Select commit Hold shift + click to select a range
2b58fce
interface
lkostrowski May 28, 2024
77d334e
wip
lkostrowski May 28, 2024
4d85cf5
wip
lkostrowski May 28, 2024
63a972d
wip
lkostrowski May 28, 2024
154f6eb
Validate attributes, fix hrTime mapping
witoszekdev May 28, 2024
3faaec1
Add validation of attributes as in OTEL spec
witoszekdev May 28, 2024
f50edbf
Check dropped attributes length
witoszekdev May 28, 2024
530c8eb
Add NaN validation
witoszekdev May 28, 2024
c5fcd57
wip
lkostrowski May 28, 2024
c747686
Basic otel section
krzysztofzuraw May 28, 2024
09a9b67
change to multiple tansporters
lkostrowski May 28, 2024
af79cbc
Map log severity to a number
witoszekdev May 28, 2024
d6da7b7
full UI
krzysztofzuraw May 28, 2024
3a2a770
refactor
lkostrowski May 28, 2024
2d27c2a
Fix use case
witoszekdev May 28, 2024
7fab4ad
fix test
lkostrowski May 28, 2024
dd5f505
fix deps
lkostrowski May 28, 2024
fd5fb8d
Add spanContext
witoszekdev May 28, 2024
780a05b
Save logs settings into metdata
krzysztofzuraw May 28, 2024
5be49e0
add waituntil
lkostrowski May 28, 2024
0bf9fee
add checkout id
lkostrowski May 28, 2024
53b2791
wip
lkostrowski May 28, 2024
97f8f45
refactor attrs
lkostrowski May 28, 2024
793d2f2
Fix error handling in log drain
witoszekdev May 28, 2024
9665e5c
Add UI for otel settings
krzysztofzuraw May 28, 2024
3bacfcd
remove logger
krzysztofzuraw May 28, 2024
d02bced
Add different logs for errors
witoszekdev May 29, 2024
fb86891
Update message
witoszekdev May 29, 2024
116c12a
Add better error messages
witoszekdev May 29, 2024
79c1bcd
json logs
lkostrowski May 29, 2024
e18e065
add otel transport toggle
krzysztofzuraw May 29, 2024
4eb753e
add http transport ui
krzysztofzuraw May 29, 2024
38f1cdd
Add dynamic transporters
krzysztofzuraw May 29, 2024
a049f15
Add logs to order-confirmed webhook event
witoszekdev May 29, 2024
d1f324c
remove transports from constructor
krzysztofzuraw May 29, 2024
315d27d
try/catch
krzysztofzuraw May 29, 2024
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
3 changes: 3 additions & 0 deletions .gitignore
Original file line number Diff line number Diff line change
Expand Up @@ -49,3 +49,6 @@ apps/**/generated

# bruno
cloud.bru


analyze
1 change: 1 addition & 0 deletions apps/avatax/package.json
Original file line number Diff line number Diff line change
Expand Up @@ -52,6 +52,7 @@
"@trpc/react-query": "10.43.1",
"@trpc/server": "10.43.1",
"@urql/exchange-auth": "^2.1.4",
"@vercel/functions": "1.0.2",
"avatax": "^23.7.0",
"dotenv": "^16.3.1",
"graphql": "16.7.1",
Expand Down
10 changes: 9 additions & 1 deletion apps/avatax/src/lib/app-config.test.ts
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
import { describe, it, expect } from "vitest";
import { describe, expect, it } from "vitest";
import { AppConfig } from "./app-config";

describe("AppConfig", () => {
Expand Down Expand Up @@ -50,6 +50,10 @@ describe("AppConfig", () => {
isAutocommit: false,
isDocumentRecordingEnabled: false,
shippingTaxCode: "123",
logsSettings: {
otel: {},
json: {},
},
},
},
],
Expand Down Expand Up @@ -118,6 +122,10 @@ describe("AppConfig", () => {
isAutocommit: false,
isDocumentRecordingEnabled: false,
shippingTaxCode: "123",
logsSettings: {
otel: {},
json: {},
},
},
},
],
Expand Down
10 changes: 7 additions & 3 deletions apps/avatax/src/lib/app-configuration-logger.test.ts
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
import { describe, it, expect, vi, beforeEach } from "vitest";
import { AppConfigurationLogger } from "./app-configuration-logger";
import { AppConfig } from "./app-config";
import { ObservabilityAttributes } from "@saleor/apps-otel/src/lib/observability-attributes";
import { beforeEach, describe, expect, it, vi } from "vitest";
import { AppConfig } from "./app-config";
import { AppConfigurationLogger } from "./app-configuration-logger";

describe("AppConfigurationLogger", () => {
const mockWarn = vi.fn();
Expand Down Expand Up @@ -72,6 +72,10 @@ describe("AppConfigurationLogger", () => {
isAutocommit: false,
isDocumentRecordingEnabled: false,
shippingTaxCode: "123",
logsSettings: {
otel: {},
json: {},
},
},
},
],
Expand Down
8 changes: 6 additions & 2 deletions apps/avatax/src/modules/app/get-app-config.test.ts
Original file line number Diff line number Diff line change
@@ -1,9 +1,9 @@
import { encrypt } from "@saleor/app-sdk/settings-manager";
import { getAppConfig } from "./get-app-config";
import { describe, expect, it, vi } from "vitest";
import { ProviderConnections } from "../provider-connections/provider-connections";
import { MetadataItem } from "../../../generated/graphql";
import { ChannelsConfig } from "../channel-configuration/channel-config";
import { ProviderConnections } from "../provider-connections/provider-connections";
import { getAppConfig } from "./get-app-config";

const mockedSecretKey = "test_secret_key";
const mockedProviders: ProviderConnections = [
Expand All @@ -28,6 +28,10 @@ const mockedProviders: ProviderConnections = [
street: "123 Main St",
zip: "10001",
},
logsSettings: {
otel: {},
json: {},
},
},
},
];
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -18,6 +18,10 @@ const defaultAvataxConfig: AvataxConfig = {
password: "password",
username: "username",
},
logsSettings: {
otel: {},
json: {},
},
};

const testingScenariosMap = {
Expand Down
24 changes: 24 additions & 0 deletions apps/avatax/src/modules/avatax/avatax-connection-schema.ts
Original file line number Diff line number Diff line change
Expand Up @@ -29,6 +29,18 @@ export const avataxConfigSchema = z
shippingTaxCode: z.string().optional(),
isDocumentRecordingEnabled: z.boolean().default(true),
address: addressSchema,
logsSettings: z.object({
otel: z.object({
enabled: z.boolean().optional(),
url: z.string().optional(),
headers: z.string().optional(),
}),
json: z.object({
enabled: z.boolean().optional(),
url: z.string().optional(),
headers: z.string().optional(),
Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

here we should add json validation, so it doesnt shoot anyone in the foot. it will be hard to notice missing " and it will fallback to {} causing auth errors. so we need custom validation, as simple as json.parse() in try/catch

}),
}),
})
.merge(baseAvataxConfigSchema);

Expand All @@ -52,6 +64,18 @@ export const defaultAvataxConfig: AvataxConfig = {
street: "",
zip: "",
},
logsSettings: {
otel: {
enabled: false,
url: "",
headers: "",
},
json: {
enabled: false,
url: "",
headers: "",
},
},
};

export const avataxConnectionSchema = z.object({
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -78,6 +78,10 @@ export class AvataxConnectionService {
...prevConfig.address,
...nextConfigPartial.address,
},
logsSettings: {
...prevConfig.logsSettings,
...nextConfigPartial.logsSettings,
},
};

await this.checkIfAuthorized(input);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -17,6 +17,7 @@ import { AvataxConfigurationCredentialsFragment } from "./avatax-configuration-c
import { AvataxConfigurationSettingsFragment } from "./avatax-configuration-settings-fragment";
import { useAvataxConfigurationStatus } from "./configuration-status";
import { HelperText } from "./form-helper-text";
import { LogsSettingsFragment } from "./logs-settings-fragment";

type AvataxConfigurationFormProps = {
submit: {
Expand Down Expand Up @@ -83,6 +84,8 @@ export const AvataxConfigurationForm = (props: AvataxConfigurationFormProps) =>
isLoading={props.validateAddress.isLoading}
/>
<Divider marginY={8} />
<LogsSettingsFragment />
<Divider marginY={8} />

<Box display={"flex"} justifyContent={"space-between"} alignItems={"center"}>
{props.leftButton}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -4,8 +4,8 @@ import { useRouter } from "next/router";
import React from "react";
import { z } from "zod";
import { trpcClient } from "../../trpc/trpc-client";
import { AvataxObfuscator } from "../avatax-obfuscator";
import { AvataxConfig, BaseAvataxConfig } from "../avatax-connection-schema";
import { AvataxObfuscator } from "../avatax-obfuscator";
import { AvataxConfigurationForm } from "./avatax-configuration-form";
import { useAvataxConfigurationStatus } from "./configuration-status";

Expand Down Expand Up @@ -141,6 +141,8 @@ export const EditAvataxConfiguration = () => {
);
}

console.log("config", data.config);
Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

to remove


return (
<AvataxConfigurationForm
submit={submit}
Expand Down
91 changes: 91 additions & 0 deletions apps/avatax/src/modules/avatax/ui/logs-settings-fragment.tsx
Original file line number Diff line number Diff line change
@@ -0,0 +1,91 @@
import { Box, Text, Textarea } from "@saleor/macaw-ui";
import { Input } from "@saleor/react-hook-form-macaw";
import { Controller, useFormContext, useWatch } from "react-hook-form";
import { AppToggle } from "../../ui/app-toggle";
import { AvataxConfig } from "../avatax-connection-schema";
import { HelperText } from "./form-helper-text";

export const LogsSettingsFragment = () => {
Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

we should have it in shared packages IMO but I think first we can finish this PR, then plan to enable it in other apps → we will extract form into shared package

UI can be shared easily, but we need to make form decoupled from data source

const { control, formState } = useFormContext<AvataxConfig>();
const isOtelTogleEnabled = useWatch({
control,
name: "logsSettings.otel.enabled",
});
const isJSONTogleEnabled = useWatch({
control,
name: "logsSettings.json.enabled",
});

return (
<>
<Box>
<Text marginBottom={4} as="h3" variant="heading">
Logs settings
</Text>
<Box display="grid" gap={2}>
<HelperText>
Configure where AvaTax should emit logs. This is useful for debugging and
troubleshooting connection issues.
</HelperText>
<AppToggle
name="logsSettings.otel.enabled"
control={control}
label="OTEL transport"
helperText={<HelperText>Enable sending logs using OpenTelemetry protocol.</HelperText>}
Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

we need to also:

  1. write that this is JSON HTTP standard (not be confused with proto)
  2. add some links to docs of open telemetry etc

/>
<Box display="grid" gap={2}>
<Input
control={control}
name="logsSettings.otel.url"
label="URL to send logs to"
helperText={formState.errors.logsSettings?.otel?.url?.message}
disabled={!isOtelTogleEnabled}
/>
<Controller
name="logsSettings.otel.headers"
control={control}
render={({ field, fieldState: { error } }) => (
<Textarea
{...field}
error={!!error}
helperText={formState.errors.logsSettings?.otel?.headers?.message}
label="Request headers in JSON format"
disabled={!isOtelTogleEnabled}
/>
)}
/>
</Box>

<AppToggle
name="logsSettings.json.enabled"
control={control}
label="JSON transport"
helperText={<HelperText>Enable sending logs using json protocol.</HelperText>}
/>
<Box display="grid" gap={2}>
<Input
control={control}
name="logsSettings.json.url"
label="URL to send logs to"
helperText={formState.errors.logsSettings?.json?.url?.message}
disabled={!isJSONTogleEnabled}
/>
<Controller
name="logsSettings.json.headers"
control={control}
render={({ field, fieldState: { error } }) => (
<Textarea
{...field}
error={!!error}
helperText={formState.errors.logsSettings?.json?.headers?.message}
label="Request headers in JSON format"
disabled={!isJSONTogleEnabled}
/>
)}
/>
</Box>
</Box>
</Box>
</>
);
};
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,8 @@ import { beforeEach, describe, expect, it, vi } from "vitest";
import { BaseError } from "../../../error";
import { AppConfig } from "../../../lib/app-config";
import { AppConfigExtractor, IAppConfigExtractor } from "../../../lib/app-config-extractor";
import { PublicLog } from "../../public-log-drain/public-events";
import { PublicLogDrainService } from "../../public-log-drain/public-log-drain.service";
import { AvataxWebhookServiceFactory } from "../../taxes/avatax-webhook-service-factory";
import { CalculateTaxesPayload } from "../../webhooks/payloads/calculate-taxes-payload";
import { CalculateTaxesUseCase } from "./calculate-taxes.use-case";
Expand Down Expand Up @@ -113,6 +115,16 @@ const getMockedAppConfig = (): AppConfig => {
isAutocommit: false,
isDocumentRecordingEnabled: false,
shippingTaxCode: "123",
logsSettings: {
otel: {
url: "https://otel.example.com",
headers: "Authorization",
},
json: {
url: "https://http.example.com",
headers: "Authorization",
},
},
},
},
],
Expand All @@ -127,6 +139,11 @@ describe("CalculateTaxesUseCase", () => {

instance = new CalculateTaxesUseCase({
configExtractor: MockConfigExtractor,
publicLogDrain: new PublicLogDrainService([
{
async emit(log: PublicLog): Promise<void> {},
Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

we should add test that checks if log drain emits when use case runs

},
]),
});
});

Expand Down
Loading
Loading