Skip to content

Commit

Permalink
examples(lgtm): add code sample for blog post (#4047)
Browse files Browse the repository at this point in the history
* examples(lgtm): add code for blogpost

* examples(lgtm): update grafana db

* examples(lgtm): edit readme
  • Loading branch information
Adnan Rahić authored Oct 11, 2024
1 parent 1d837b4 commit 66c33f3
Show file tree
Hide file tree
Showing 17 changed files with 3,578 additions and 0 deletions.
4 changes: 4 additions & 0 deletions examples/lgtm-end-to-end-observability-testing/.dockerignore
Original file line number Diff line number Diff line change
@@ -0,0 +1,4 @@
node_modules
docker-compose
tempo-data
grafana
6 changes: 6 additions & 0 deletions examples/lgtm-end-to-end-observability-testing/.env.sample
Original file line number Diff line number Diff line change
@@ -0,0 +1,6 @@
# Get the required information here: https://app.tracetest.io/retrieve-token

TRACETEST_TOKEN=
TRACETEST_ENVIRONMENT_ID=

OTEL_EXPORTER_OTLP_TRACES_ENDPOINT="http://tempo:4318/v1/traces"
3 changes: 3 additions & 0 deletions examples/lgtm-end-to-end-observability-testing/.gitignore
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
.env
node_modules
tempo-data
6 changes: 6 additions & 0 deletions examples/lgtm-end-to-end-observability-testing/Dockerfile
Original file line number Diff line number Diff line change
@@ -0,0 +1,6 @@
FROM node:slim
WORKDIR /usr/src/app/
COPY package*.json ./
RUN npm install
COPY . .
EXPOSE 8081
68 changes: 68 additions & 0 deletions examples/lgtm-end-to-end-observability-testing/docker-compose.yml
Original file line number Diff line number Diff line change
@@ -0,0 +1,68 @@
version: '3.8'

services:
app:
image: adnanrahic/tracetest-app
build: .
command: npm run index-with-tracer
ports:
- "8081:8081"
environment:
- OTEL_EXPORTER_OTLP_TRACES_ENDPOINT=${OTEL_EXPORTER_OTLP_TRACES_ENDPOINT}
depends_on:
tempo:
condition: service_started
tracetest-agent:
condition: service_started

prometheus:
image: prom/prometheus
volumes:
- ./prometheus.yml:/etc/prometheus/prometheus.yml
ports:
- "9090:9090"

loki:
image: grafana/loki:2.9.10
ports:
- "3100:3100"
environment:
- LOKI_ENABLE_API=true

init:
image: &tempoImage grafana/tempo:latest
user: root
entrypoint:
- "chown"
- "10001:10001"
- "/var/tempo"
volumes:
- ./tempo-data:/var/tempo

tempo:
image: *tempoImage
command: [ "-config.file=/etc/tempo.yaml" ]
volumes:
- ./tempo.yaml:/etc/tempo.yaml
- ./tempo-data:/var/tempo
ports:
- "3200:80" # tempo http
- "9095:9095" # tempo grpc
depends_on:
- init

tracetest-agent:
image: kubeshop/tracetest-agent:v1.7.1
environment:
- TRACETEST_API_KEY=${TRACETEST_TOKEN}
- TRACETEST_ENVIRONMENT_ID=${TRACETEST_ENVIRONMENT_ID}

grafana:
image: grafana/grafana:latest
ports:
- "3000:3000"
environment:
- GF_SECURITY_ADMIN_PASSWORD=admin
volumes:
- ./grafana:/var/lib/grafana

Binary file not shown.
37 changes: 37 additions & 0 deletions examples/lgtm-end-to-end-observability-testing/index.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,37 @@
const express = require('express');
const logger = require('./logger');
const meter = require('./meter');
// const tracer = require('./tracer');

// Create an Express app
const app = express();

// Define a custom metric (e.g., a request counter)
const requestCounter = meter.createCounter('http_requests', {
description: 'Counts HTTP requests',
});

// Middleware to increment the counter on every request
app.use((req, res, next) => {
// Increment the request counter
logger.info(`Received request for ${req.url}`);
requestCounter.add(1, { method: req.method, route: req.path });
next();
});

// Define a simple route
app.get('/', (req, res) => {
// const span = tracer.startSpan('handle_root_request');

// Simulate some work
setTimeout(() => {
res.send('Hello, World!');
// span.end();
}, 100);
});

// Start the server
app.listen(8081, () => {
logger.info('Server is running on http://localhost:5000');
console.log('Server is running on http://localhost:5000');
});
21 changes: 21 additions & 0 deletions examples/lgtm-end-to-end-observability-testing/logger.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,21 @@
// logger.js
const winston = require('winston');
const LokiTransport = require('winston-loki');

// Configure Winston to send logs to Loki
const logger = winston.createLogger({
level: 'info',
format: winston.format.json(),
transports: [
new LokiTransport({
host: 'http://loki:3100', // Assuming Loki is accessible at this URL
labels: { job: 'loki-service' },
json: true,
batching: true,
interval: 5, // Send logs in batches every 5 seconds
}),
],
});

module.exports = logger;

30 changes: 30 additions & 0 deletions examples/lgtm-end-to-end-observability-testing/meter.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,30 @@
const { MeterProvider } = require('@opentelemetry/sdk-metrics');
const { PrometheusExporter } = require('@opentelemetry/exporter-prometheus');
const { Resource } = require('@opentelemetry/resources');
const { SemanticResourceAttributes } = require('@opentelemetry/semantic-conventions');

// Prometheus Exporter for metrics
const prometheusExporter = new PrometheusExporter({
port: 9464, // Port where metrics will be exposed
endpoint: '/metrics', // Endpoint for Prometheus to scrape
}, () => {
console.log('Prometheus scrape endpoint: http://localhost:9464/metrics');
});

// MeterProvider for manual metrics instrumentation
const meterProvider = new MeterProvider({
resource: new Resource({
[SemanticResourceAttributes.SERVICE_NAME]: 'hello-world-app', // Use semantic attributes for service name
}),
});

// Bind the PrometheusExporter as a MetricReader to the MeterProvider
meterProvider.addMetricReader(prometheusExporter);

// Create a meter from the meterProvider
const meter = meterProvider.getMeter('hello-world-meter');

module.exports = meter;



Loading

0 comments on commit 66c33f3

Please sign in to comment.