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

test-server redesign #70

Draft
wants to merge 13 commits into
base: main
Choose a base branch
from
Draft
Show file tree
Hide file tree
Changes from all commits
Commits
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
110 changes: 106 additions & 4 deletions package-lock.json

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

2 changes: 1 addition & 1 deletion package.json
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,7 @@
"start:otel-receiver": "lerna run --scope @traceloop/otel-receiver start",
"start:test-servers": "lerna run --scope @traceloop/test-servers start",
"docker:instrument-opentelemetry": "docker build -f packages/instrument-opentelemetry/Dockerfile . -t instrument-opentelemetry",
"docker:test-servers": "docker build -f packages/test-servers/Dockerfile . -t test-servers",
"docker:test-servers": "docker build --platform linux/amd64 -f packages/test-servers/Dockerfile . -t test-servers",
"test": "jest",
"test-ci": "concurrently -k --success \"command-1\" --hide 0 \"npm:start:test-servers\" \"npm:test\"",
"release": "npm run build && lerna publish --conventional-commits --no-private"
Expand Down
3 changes: 3 additions & 0 deletions packages/instrument-opentelemetry/src/tracing.ts
Original file line number Diff line number Diff line change
Expand Up @@ -14,6 +14,9 @@ if (process.env.OTEL_EXPORTER_OTLP_ENDPOINT) {
? new ProtoExporter({
url: process.env.OTEL_EXPORTER_OTLP_ENDPOINT,
timeoutMillis: 100,
// headers: {
// authorization: 'testtttt',
// },
})
: new GRPCExporter({
url: process.env.OTEL_EXPORTER_OTLP_ENDPOINT,
Expand Down
3 changes: 2 additions & 1 deletion packages/test-servers/Dockerfile
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,7 @@ COPY lerna.json ./
COPY tsconfig.json ./
COPY packages/test-servers/. ./packages/test-servers/

RUN npm install @traceloop/instrument-opentelemetry
RUN npm run build

CMD node ./packages/test-servers/dist/index.js
CMD ["sh", "-c", "cd packages/test-servers && npm run start"]
17 changes: 17 additions & 0 deletions packages/test-servers/README.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,17 @@
# test-servers

This is a test environment which sends telemetry data (by default to Traceloop servers).
A single container (listening on multiple ports) mimics different microservices that communicate via http and gRPC.

Services include:

- gateway
- users service
- gigs service
- orders service
- emails service
- bi grpc service

## Architecture

![architecture](./public/architecture.png)
15 changes: 10 additions & 5 deletions packages/test-servers/package.json
Original file line number Diff line number Diff line change
Expand Up @@ -8,11 +8,15 @@
},
"scripts": {
"prebuild": "rm -rf dist",
"build": "rollup -c && cp ./src/helloworld.proto ./dist/helloworld.proto",
"start:orders": "ORDERS_SERVICE=TRUE SERVICE_NAME=orders-service PORT=3000 OTEL_EXPORTER_OTLP_ENDPOINT=http://localhost:4123/v1/traces OTEL_EXPORTER_TYPE=PROTO node -r @traceloop/instrument-opentelemetry dist/index.js",
"start:emails": "EMAILS_SERVICE=TRUE SERVICE_NAME=emails-service PORT=3001 OTEL_EXPORTER_OTLP_ENDPOINT=http://localhost:4123/v1/traces OTEL_EXPORTER_TYPE=PROTO node -r @traceloop/instrument-opentelemetry dist/index.js",
"start:grpc": "GRPC_SERVICE=TRUE SERVICE_NAME=grpc-service PORT=50051 OTEL_EXPORTER_OTLP_ENDPOINT=http://localhost:4123/v1/traces OTEL_EXPORTER_TYPE=PROTO node -r @traceloop/instrument-opentelemetry dist/index.js",
"start": "concurrently \"npm:start:orders\" \"npm:start:emails\" \"npm:start:grpc\""
"build": "rollup -c && cp ./src/bi.proto ./dist/bi.proto",
"start:emails": "EMAILS_SERVICE=TRUE SERVICE_NAME=test-servers-emails OTEL_EXPORTER_OTLP_ENDPOINT=https://api-staging.traceloop.dev/v1/traces OTEL_EXPORTER_TYPE=PROTO node -r @traceloop/instrument-opentelemetry dist/index.js",
"start:grpc": "GRPC_SERVICE=TRUE SERVICE_NAME=test-servers-grpc OTEL_EXPORTER_OTLP_ENDPOINT=https://api-staging.traceloop.dev/v1/traces OTEL_EXPORTER_TYPE=PROTO node -r @traceloop/instrument-opentelemetry dist/index.js",
"start:users": "USERS_SERVICE=TRUE SERVICE_NAME=test-servers-users OTEL_EXPORTER_OTLP_ENDPOINT=https://api-staging.traceloop.dev/v1/traces OTEL_EXPORTER_TYPE=PROTO node -r @traceloop/instrument-opentelemetry dist/index.js",
"start:gigs": "GIGS_SERVICE=TRUE SERVICE_NAME=test-servers-gigs OTEL_EXPORTER_OTLP_ENDPOINT=https://api-staging.traceloop.dev/v1/traces OTEL_EXPORTER_TYPE=PROTO node -r @traceloop/instrument-opentelemetry dist/index.js",
"start:orders": "ORDERS_SERVICE=TRUE SERVICE_NAME=test-servers-orders OTEL_EXPORTER_OTLP_ENDPOINT=https://api-staging.traceloop.dev/v1/traces OTEL_EXPORTER_TYPE=PROTO node -r @traceloop/instrument-opentelemetry dist/index.js",
"start:gateway": "GATEWAY_SERVICE=TRUE SERVICE_NAME=test-servers-gateway PORT=3000 OTEL_EXPORTER_OTLP_ENDPOINT=https://api-staging.traceloop.dev/v1/traces OTEL_EXPORTER_TYPE=PROTO node -r @traceloop/instrument-opentelemetry dist/index.js",
"start:uninstrumented": "NOT_INSTRUMENTED=TRUE node dist/index.js",
"start": "concurrently \"npm:start:users\" \"npm:start:gigs\" \"npm:start:orders\" \"npm:start:gateway\" \"npm:start:emails\" \"npm:start:grpc\" \"npm:start:uninstrumented\""
},
"devDependencies": {
"@types/express": "^4.17.17",
Expand All @@ -25,6 +29,7 @@
"@traceloop/instrument-opentelemetry": "^0.5.0",
"express": "^4.18.2",
"google-protobuf": "^3.0.0",
"http-proxy-middleware": "^2.0.6",
"pg": "^8.9.0",
"typeorm": "^0.3.12",
"uuid": "^9.0.0"
Expand Down
Binary file added packages/test-servers/public/architecture.png
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
32 changes: 32 additions & 0 deletions packages/test-servers/src/bi-grpc-service.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,32 @@
import grpc from '@grpc/grpc-js';
import protoLoader from '@grpc/proto-loader';
import { GRPC_SERVICE_PORT } from './constants';

const PROTO_PATH = __dirname + '/bi.proto';
const packageDefinition = protoLoader.loadSync(PROTO_PATH, {
keepCase: true,
longs: String,
enums: String,
defaults: true,
oneofs: true,
});
const bi_proto = grpc.loadPackageDefinition(packageDefinition).bi;

export const biGrpcService = new grpc.Server();

function reportBi(call: any, callback: any) {
callback(null, { message: 'Received BI event from ' + call.request.name });
}
biGrpcService.addService((bi_proto as any).Bi.service, {
reportBi: reportBi,
});

const client = new (bi_proto as any).Bi(
`localhost:${GRPC_SERVICE_PORT}`,
grpc.credentials.createInsecure(),
);

// should be called from other services (makes an rpc call to the bi grpc service)
export const sendBiEvent = (name: string, id: string) => {
client.reportBi({ name: name, id: id }, function (_: any, response: any) {}); // eslint-disable-line
};
22 changes: 22 additions & 0 deletions packages/test-servers/src/bi.proto
Original file line number Diff line number Diff line change
@@ -0,0 +1,22 @@
syntax = "proto3";

option java_multiple_files = true;
option java_outer_classname = "ReportBiProto";
option objc_class_prefix = "HLW";

package bi;

service Bi {
rpc ReportBi (ReportBiEvent) returns (ReportBiReply) {}
}

// The request message containing the BI data.
message ReportBiEvent {
string name = 1;
string id = 2;
}

// The response message.
message ReportBiReply {
string message = 1;
}
9 changes: 9 additions & 0 deletions packages/test-servers/src/constants.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,9 @@
export const USERS_SERVICE_PORT = 3001,
GIGS_SERVICE_PORT = 3002,
ORDERS_SERVICE_PORT = 3003,
EMAILS_SERVICE_PORT = 3004,
GRPC_SERVICE_PORT = 50051;

export const GATEWAY_SERVICE_PORT = process.env.PORT
? Number(process.env.PORT)
: 3000;
21 changes: 21 additions & 0 deletions packages/test-servers/src/email-service.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,21 @@
import express from 'express';
import axios from 'axios';
import { EMAILS_SERVICE_PORT } from './constants';

export const emailsService = express();

emailsService.post('/emails/send', (req, res) => {
res.send('Email sent!');
});

// should be called from other services (makes an http call to the emails service)
export const sendEmail = async (body: any) => {
try {
return await axios.post(
`http://localhost:${EMAILS_SERVICE_PORT}/emails/send`,
body,
);
} catch (err) {
console.error('Error sending email', err);
}
};
35 changes: 35 additions & 0 deletions packages/test-servers/src/gateway.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,35 @@
import express from 'express';
import { createProxyMiddleware } from 'http-proxy-middleware';
import {
USERS_SERVICE_PORT,
GIGS_SERVICE_PORT,
ORDERS_SERVICE_PORT,
} from './constants';

export const gatewayService = express();

if (process.env.GATEWAY_SERVICE) {
gatewayService.use(
'/users',
createProxyMiddleware({
target: `http://localhost:${USERS_SERVICE_PORT}`,
changeOrigin: true,
}),
);

gatewayService.use(
'/gigs',
createProxyMiddleware({
target: `http://localhost:${GIGS_SERVICE_PORT}`,
changeOrigin: true,
}),
);

gatewayService.use(
'/orders',
createProxyMiddleware({
target: `http://localhost:${ORDERS_SERVICE_PORT}`,
changeOrigin: true,
}),
);
}
Loading