Skip to content

Commit

Permalink
Send OTEL Data to Grafana
Browse files Browse the repository at this point in the history
  • Loading branch information
timoclsn committed Aug 21, 2024
1 parent 4592973 commit 491bfb0
Show file tree
Hide file tree
Showing 6 changed files with 176 additions and 32 deletions.
1 change: 1 addition & 0 deletions package.json
Original file line number Diff line number Diff line change
Expand Up @@ -40,6 +40,7 @@
"@formkit/auto-animate": "^0.8.2",
"@libsql/client": "^0.6.0",
"@mapbox/polyline": "1.2.1",
"@opentelemetry/exporter-trace-otlp-http": "^0.52.1",
"@opentelemetry/sdk-metrics": "^1.25.1",
"@opentelemetry/sdk-trace-base": "^1.25.1",
"@opentelemetry/sdk-trace-node": "^1.25.1",
Expand Down
143 changes: 136 additions & 7 deletions pnpm-lock.yaml

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

4 changes: 2 additions & 2 deletions src/data/smarthome/action.ts
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,7 @@ import {
DevToolsLive,
HomeeService,
NodeSdkLive,
incrementBalconyCounter,
} from "../../lib/effect";
import { createAction } from "../clients";

Expand All @@ -30,7 +31,6 @@ export const turnOnBalcony = createAction({
const { color } = input;
yield* Effect.logInfo(`Trying to turn on balcony light ${color}`);

const { incrementBalconyCounter } = yield* DatabaseService;
const { playHomeegram } = yield* HomeeService;
const homeegramId = colorHomeegramIds[color];

Expand Down Expand Up @@ -64,7 +64,7 @@ export const turnOnBalcony = createAction({
break;
}

return new ActionError({ message });
return new ActionError({ message, cause: error.cause });
}),
Effect.orDie,
),
Expand Down
3 changes: 1 addition & 2 deletions src/lib/data/errors.ts
Original file line number Diff line number Diff line change
Expand Up @@ -2,11 +2,10 @@ export const DEFAULT_ERROR_MESSAGE = "Something went wrong.";
const ERROR_NAME = "ActionError";

export class ActionError extends Error {
public override readonly cause?: Error;
public readonly log?;
public readonly name = ERROR_NAME;

constructor(opts: { message: string; log?: string; cause?: Error }) {
constructor(opts: { message: string; log?: string; cause?: unknown }) {
super(opts.message);
this.message = opts.message;
this.log = opts.log;
Expand Down
54 changes: 33 additions & 21 deletions src/lib/effect.ts
Original file line number Diff line number Diff line change
Expand Up @@ -6,33 +6,55 @@ import {
HttpClientResponse,
} from "@effect/platform";
import { NodeSocket } from "@effect/platform-node";
import {
BatchSpanProcessor,
ConsoleSpanExporter,
} from "@opentelemetry/sdk-trace-base";
import { OTLPTraceExporter } from "@opentelemetry/exporter-trace-otlp-http";
import { BatchSpanProcessor } from "@opentelemetry/sdk-trace-base";
import { eq, sql } from "drizzle-orm";
import { Context, Data, Effect, Layer, Schedule } from "effect";
import { balconyControl } from "../../db/schema";
import { db } from "./db";

const { HOMEE_ID, HOMEE_ACCESS_TOKEN } = process.env;
const { HOMEE_ID, HOMEE_ACCESS_TOKEN, GRAFANA_CLOUD_API_KEY } = process.env;

// Observability

const exporter = new OTLPTraceExporter({
url: "https://otlp-gateway-prod-eu-west-2.grafana.net/otlp",
headers: {
"api-key": GRAFANA_CLOUD_API_KEY,
},
});

export const NodeSdkLive = NodeSdk.layer(() => ({
resource: { serviceName: "timoclasen-de" },
spanProcessor: new BatchSpanProcessor(new ConsoleSpanExporter()),
spanProcessor: new BatchSpanProcessor(exporter),
}));

export const DevToolsLive = DevTools.layerWebSocket().pipe(
Layer.provide(NodeSocket.layerWebSocketConstructor),
);

// Database service

export class DatabaseService extends Context.Tag("Database")<
DatabaseService,
{
db: typeof db;
}
>() {
static Live = Layer.succeed(DatabaseService, {
db,
});
}

export class IncrementBalconyCounterError extends Data.TaggedError(
"IncrementBalconyCounterError",
)<{ color: string; cause?: unknown }> {}

const makeDatabaseService = Effect.gen(function* () {
const incrementBalconyCounter = (color: "red" | "blue" | "green") =>
Effect.tryPromise({
export const incrementBalconyCounter = (color: "red" | "blue" | "green") =>
Effect.gen(function* () {
const { db } = yield* DatabaseService;

yield* Effect.tryPromise({
try: () =>
db
.update(balconyControl)
Expand All @@ -46,19 +68,9 @@ const makeDatabaseService = Effect.gen(function* () {
cause: error,
}),
}).pipe(Effect.withSpan("incrementBalconyCounter"));
});

return {
db,
incrementBalconyCounter,
} as const;
});

export class DatabaseService extends Context.Tag("Database")<
DatabaseService,
Effect.Effect.Success<typeof makeDatabaseService>
>() {
static Live = Layer.effect(DatabaseService, makeDatabaseService);
}
// homee service

export class PlayHomeegramError extends Data.TaggedError("PlayHomeegramError")<{
cause?: unknown;
Expand Down
3 changes: 3 additions & 0 deletions src/lib/env.ts
Original file line number Diff line number Diff line change
Expand Up @@ -32,6 +32,9 @@ const envSchema = z.object({
TURSO_DATABASE_URL: envVarSchema,
TURSO_AUTH_TOKEN: envVarSchema,

// Grafana
GRAFANA_CLOUD_API_KEY: envVarSchema,

// Client

// Vercel
Expand Down

0 comments on commit 491bfb0

Please sign in to comment.