Skip to content

Commit

Permalink
fix: add nextjs example (#22)
Browse files Browse the repository at this point in the history
  • Loading branch information
YunosukeY authored Feb 24, 2024
1 parent 6e65043 commit 6cd00bd
Show file tree
Hide file tree
Showing 20 changed files with 3,248 additions and 7 deletions.
3 changes: 3 additions & 0 deletions examples/nextjs/.eslintrc.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
{
"extends": "next/core-web-vitals"
}
36 changes: 36 additions & 0 deletions examples/nextjs/.gitignore
Original file line number Diff line number Diff line change
@@ -0,0 +1,36 @@
# See https://help.github.com/articles/ignoring-files/ for more about ignoring files.

# dependencies
/node_modules
/.pnp
.pnp.js
.yarn/install-state.gz

# testing
/coverage

# next.js
/.next/
/out/

# production
/build

# misc
.DS_Store
*.pem

# debug
npm-debug.log*
yarn-debug.log*
yarn-error.log*

# local env files
.env*.local

# vercel
.vercel

# typescript
*.tsbuildinfo
next-env.d.ts
44 changes: 44 additions & 0 deletions examples/nextjs/README.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,44 @@
# Next.js Example

## Preparations

1. Clone a `opentelemetry-collector-dev-setup` repository.

```sh
git clone https://github.com/vercel/opentelemetry-collector-dev-setup.git
```

2. Add following lines to the `otel-collector-config.yaml` so that OTelCol can process logs.

```diff
service:
extensions: [pprof, zpages, health_check]
pipelines:
traces:
receivers: [otlp]
processors: [batch]
exporters: [logging, zipkin, jaeger]
metrics:
receivers: [otlp]
processors: [batch]
exporters: [logging, prometheus]
+ logs:
+ receivers: [otlp]
+ processors: [batch]
+ exporters: [logging]
```

3. Launch containers.

```
docker compose up -d
```

## Launch the application

```
yarn install
yarn dev
```

To learn more details, look under `/src/Examples`.
21 changes: 21 additions & 0 deletions examples/nextjs/next.config.mjs
Original file line number Diff line number Diff line change
@@ -0,0 +1,21 @@
/** @type {import('next').NextConfig} */
const nextConfig = {
async rewrites() {
return [
{
source: "/v1/traces",
destination: "http://localhost:4318/v1/traces",
},
{
source: "/v1/metrics",
destination: "http://localhost:4318/v1/metrics",
},
{
source: "/v1/logs",
destination: "http://localhost:4318/v1/logs",
},
];
},
};

export default nextConfig;
25 changes: 25 additions & 0 deletions examples/nextjs/package.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,25 @@
{
"name": "nextjs",
"version": "0.1.0",
"private": true,
"scripts": {
"dev": "next dev",
"build": "next build",
"start": "next start",
"lint": "next lint"
},
"dependencies": {
"@kimitsu/otel-sdk-web": "^1.0.0",
"next": "14.1.0",
"react": "^18",
"react-dom": "^18"
},
"devDependencies": {
"@types/node": "^20",
"@types/react": "^18",
"@types/react-dom": "^18",
"eslint": "^8",
"eslint-config-next": "14.1.0",
"typescript": "^5"
}
}
1 change: 1 addition & 0 deletions examples/nextjs/public/next.svg
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
1 change: 1 addition & 0 deletions examples/nextjs/public/vercel.svg
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
15 changes: 15 additions & 0 deletions examples/nextjs/src/Examples/LogExample.tsx
Original file line number Diff line number Diff line change
@@ -0,0 +1,15 @@
import type React from "react";
import { logger } from "./otel";
import { SeverityNumber } from "@opentelemetry/api-logs";

const LogExample: React.FC = () => {
logger.emit({
severityNumber: SeverityNumber.INFO,
severityText: "INFO",
body: "client component",
});

return null;
};

export default LogExample;
14 changes: 14 additions & 0 deletions examples/nextjs/src/Examples/MetricExample.tsx
Original file line number Diff line number Diff line change
@@ -0,0 +1,14 @@
import type React from "react";
import { useEffect } from "react";
import { meter } from "./otel";

const ClientMetricsExample: React.FC = () => {
useEffect(() => {
const counter = meter.createCounter("client_counter");
counter.add(1);
}, []);

return null;
};

export default ClientMetricsExample;
22 changes: 22 additions & 0 deletions examples/nextjs/src/Examples/TraceExample.tsx
Original file line number Diff line number Diff line change
@@ -0,0 +1,22 @@
import { context, trace } from "@opentelemetry/api";
import type React from "react";
import { useEffect } from "react";
import { tracer } from "./otel";

const ClientFetchTraceExample: React.FC = () => {
useEffect(() => {
const span = tracer.startSpan("client manual Span");
// eslint-disable-next-line @typescript-eslint/no-floating-promises
context.with(trace.setSpan(context.active(), span), async () => {
await fetch("https://example.com/", {
method: "GET",
mode: "no-cors",
});
span.end();
});
}, []);

return null;
};

export default ClientFetchTraceExample;
18 changes: 18 additions & 0 deletions examples/nextjs/src/Examples/index.tsx
Original file line number Diff line number Diff line change
@@ -0,0 +1,18 @@
"use client";

import React from "react";
import TraceExample from "./TraceExample";
import LogExample from "./LogExample";
import MetricExample from "./MetricExample";

const Examples: React.FC = () => {
return (
<>
<TraceExample />
<MetricExample />
<LogExample />
</>
);
};

export default Examples;
11 changes: 11 additions & 0 deletions examples/nextjs/src/Examples/otel.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,11 @@
"use client";

import { start } from "@kimitsu/otel-sdk-web";

export const { tracer, meter, logger } = start({
serviceName: "client",
otelcolOrigin: "http://localhost:3000",
tracerName: "app",
meterName: "app",
loggerName: "app",
});
Binary file added examples/nextjs/src/app/favicon.ico
Binary file not shown.
107 changes: 107 additions & 0 deletions examples/nextjs/src/app/globals.css
Original file line number Diff line number Diff line change
@@ -0,0 +1,107 @@
:root {
--max-width: 1100px;
--border-radius: 12px;
--font-mono: ui-monospace, Menlo, Monaco, "Cascadia Mono", "Segoe UI Mono",
"Roboto Mono", "Oxygen Mono", "Ubuntu Monospace", "Source Code Pro",
"Fira Mono", "Droid Sans Mono", "Courier New", monospace;

--foreground-rgb: 0, 0, 0;
--background-start-rgb: 214, 219, 220;
--background-end-rgb: 255, 255, 255;

--primary-glow: conic-gradient(
from 180deg at 50% 50%,
#16abff33 0deg,
#0885ff33 55deg,
#54d6ff33 120deg,
#0071ff33 160deg,
transparent 360deg
);
--secondary-glow: radial-gradient(
rgba(255, 255, 255, 1),
rgba(255, 255, 255, 0)
);

--tile-start-rgb: 239, 245, 249;
--tile-end-rgb: 228, 232, 233;
--tile-border: conic-gradient(
#00000080,
#00000040,
#00000030,
#00000020,
#00000010,
#00000010,
#00000080
);

--callout-rgb: 238, 240, 241;
--callout-border-rgb: 172, 175, 176;
--card-rgb: 180, 185, 188;
--card-border-rgb: 131, 134, 135;
}

@media (prefers-color-scheme: dark) {
:root {
--foreground-rgb: 255, 255, 255;
--background-start-rgb: 0, 0, 0;
--background-end-rgb: 0, 0, 0;

--primary-glow: radial-gradient(rgba(1, 65, 255, 0.4), rgba(1, 65, 255, 0));
--secondary-glow: linear-gradient(
to bottom right,
rgba(1, 65, 255, 0),
rgba(1, 65, 255, 0),
rgba(1, 65, 255, 0.3)
);

--tile-start-rgb: 2, 13, 46;
--tile-end-rgb: 2, 5, 19;
--tile-border: conic-gradient(
#ffffff80,
#ffffff40,
#ffffff30,
#ffffff20,
#ffffff10,
#ffffff10,
#ffffff80
);

--callout-rgb: 20, 20, 20;
--callout-border-rgb: 108, 108, 108;
--card-rgb: 100, 100, 100;
--card-border-rgb: 200, 200, 200;
}
}

* {
box-sizing: border-box;
padding: 0;
margin: 0;
}

html,
body {
max-width: 100vw;
overflow-x: hidden;
}

body {
color: rgb(var(--foreground-rgb));
background: linear-gradient(
to bottom,
transparent,
rgb(var(--background-end-rgb))
)
rgb(var(--background-start-rgb));
}

a {
color: inherit;
text-decoration: none;
}

@media (prefers-color-scheme: dark) {
html {
color-scheme: dark;
}
}
22 changes: 22 additions & 0 deletions examples/nextjs/src/app/layout.tsx
Original file line number Diff line number Diff line change
@@ -0,0 +1,22 @@
import type { Metadata } from "next";
import { Inter } from "next/font/google";
import "./globals.css";

const inter = Inter({ subsets: ["latin"] });

export const metadata: Metadata = {
title: "Create Next App",
description: "Generated by create next app",
};

export default function RootLayout({
children,
}: Readonly<{
children: React.ReactNode;
}>) {
return (
<html lang="en">
<body className={inter.className}>{children}</body>
</html>
);
}
Loading

0 comments on commit 6cd00bd

Please sign in to comment.