Skip to content

Commit

Permalink
fix style
Browse files Browse the repository at this point in the history
  • Loading branch information
trentm committed Dec 19, 2023
1 parent be30f67 commit aa037ec
Show file tree
Hide file tree
Showing 10 changed files with 322 additions and 299 deletions.
3 changes: 1 addition & 2 deletions .eslintrc.json
Original file line number Diff line number Diff line change
Expand Up @@ -12,8 +12,7 @@
"es2022": true // Defines `Promise`, etc.
},
"extends": [
// XXX
// "plugin:prettier/recommended", // comment this out to check without style
"plugin:prettier/recommended", // comment this out to check without style
"eslint:recommended"
],
"plugins": [
Expand Down
60 changes: 34 additions & 26 deletions packages/mockotlpserver/examples/simple-http-server.js
Original file line number Diff line number Diff line change
Expand Up @@ -2,42 +2,50 @@

// NOTE: For simplicity we added the instrumentation coee in the same file
// have a commmand line arg to decide the exporter?
const { NodeSDK } = require('@opentelemetry/sdk-node');
const {NodeSDK} = require('@opentelemetry/sdk-node');
// const { ConsoleSpanExporter } = require('@opentelemetry/sdk-trace-node');
const { OTLPTraceExporter: OTLPTraceExporterHttp } = require('@opentelemetry/exporter-trace-otlp-http');
const { OTLPTraceExporter: OTLPTraceExporterProto } = require('@opentelemetry/exporter-trace-otlp-proto');
const { OTLPTraceExporter: OTLPTraceExporterGrpc } = require('@opentelemetry/exporter-trace-otlp-grpc');
const {
getNodeAutoInstrumentations,
OTLPTraceExporter: OTLPTraceExporterHttp,
} = require('@opentelemetry/exporter-trace-otlp-http');
const {
OTLPTraceExporter: OTLPTraceExporterProto,
} = require('@opentelemetry/exporter-trace-otlp-proto');
const {
OTLPTraceExporter: OTLPTraceExporterGrpc,
} = require('@opentelemetry/exporter-trace-otlp-grpc');
const {
getNodeAutoInstrumentations,
} = require('@opentelemetry/auto-instrumentations-node');

// TODO make this flavor configurable
let otlpFlavor = 'grpc';
const OTLPTraceExporter = {
http: OTLPTraceExporterHttp,
proto: OTLPTraceExporterProto,
grpc: OTLPTraceExporterGrpc
http: OTLPTraceExporterHttp,
proto: OTLPTraceExporterProto,
grpc: OTLPTraceExporterGrpc,
}[otlpFlavor];

const sdk = new NodeSDK({
// traceExporter: new ConsoleSpanExporter(),
traceExporter: new OTLPTraceExporter(),
instrumentations: [getNodeAutoInstrumentations({
// To reduce noise in the console since we just want
// http and mongodb spans
"@opentelemetry/instrumentation-fs": {
enabled: false,
},
"@opentelemetry/instrumentation-connect": {
enabled:false,
},
"@opentelemetry/instrumentation-net": {
enabled: false,
},
"@opentelemetry/instrumentation-dns": {
enabled: false,
}
})],
// traceExporter: new ConsoleSpanExporter(),
traceExporter: new OTLPTraceExporter(),
instrumentations: [
getNodeAutoInstrumentations({
// To reduce noise in the console since we just want
// http and mongodb spans
'@opentelemetry/instrumentation-fs': {
enabled: false,
},
'@opentelemetry/instrumentation-connect': {
enabled: false,
},
'@opentelemetry/instrumentation-net': {
enabled: false,
},
'@opentelemetry/instrumentation-dns': {
enabled: false,
},
}),
],
});

sdk.start();
Expand Down
87 changes: 45 additions & 42 deletions packages/mockotlpserver/lib/client.js
Original file line number Diff line number Diff line change
@@ -1,59 +1,62 @@
const { resolve } = require('path');
const {resolve} = require('path');

const grpc = require('@grpc/grpc-js');
const loader = require('@grpc/proto-loader');

const prefix = resolve(__dirname, '../opentelemetry/proto');
const pkgsBase = resolve(__dirname, '..');
const traceDefinition = loader.loadSync(
`${prefix}/collector/trace/v1/trace_service.proto`,
{
includeDirs: [pkgsBase]
}
`${prefix}/collector/trace/v1/trace_service.proto`,
{
includeDirs: [pkgsBase],
}
);
const traceDescriptor = grpc.loadPackageDefinition(traceDefinition);
const traceNamespace = traceDescriptor.opentelemetry.proto.collector.trace.v1;

const client = new traceNamespace.TraceService(
"localhost:4317",
grpc.credentials.createInsecure()
'localhost:4317',
grpc.credentials.createInsecure()
);

client.Export({
resourceSpans: [
client.Export(
{
resource: {
"attributes": [
{
"key": "service.name",
"value": {
"stringValue": "unknown_service:node"
}
},
{
"key": "telemetry.sdk.language",
"value": {
"stringValue": "nodejs"
}
},
{
"key": "telemetry.sdk.name",
"value": {
"stringValue": "opentelemetry"
}
},
{
"key": "telemetry.sdk.version",
"value": {
"stringValue": "1.15.2"
}
},
resourceSpans: [
{
resource: {
attributes: [
{
key: 'service.name',
value: {
stringValue: 'unknown_service:node',
},
},
{
key: 'telemetry.sdk.language',
value: {
stringValue: 'nodejs',
},
},
{
key: 'telemetry.sdk.name',
value: {
stringValue: 'opentelemetry',
},
},
{
key: 'telemetry.sdk.version',
value: {
stringValue: '1.15.2',
},
},
],
droppedAttributesCount: 0,
},
},
],
"droppedAttributesCount": 0
}
},
(err, response) => {
console.log('From server error', err);
console.log('From server response', response);
}
]
}, (err, response) => {
console.log("From server error", err);
console.log("From server response", response);
})
);
69 changes: 36 additions & 33 deletions packages/mockotlpserver/lib/grpc.js
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
const { resolve } = require('path');
const {resolve} = require('path');

const grpc = require('@grpc/grpc-js');
const loader = require('@grpc/proto-loader');
Expand All @@ -9,31 +9,33 @@ const loader = require('@grpc/proto-loader');
const prefix = resolve(__dirname, '../opentelemetry/proto');
const pkgsBase = resolve(__dirname, '..');
const packages = {
logs: '/collector/logs/v1/logs_service.proto',
metrics: '/collector/metrics/v1/metrics_service.proto',
trace: '/collector/trace/v1/trace_service.proto',
logs: '/collector/logs/v1/logs_service.proto',
metrics: '/collector/metrics/v1/metrics_service.proto',
trace: '/collector/trace/v1/trace_service.proto',
};

for (const [name, path] of Object.entries(packages)) {
const definition = loader.loadSync(`${prefix}${path}`, { includeDirs: [pkgsBase] });
const descriptor = grpc.loadPackageDefinition(definition);
const namespace = descriptor.opentelemetry.proto.collector[name].v1
const definition = loader.loadSync(`${prefix}${path}`, {
includeDirs: [pkgsBase],
});
const descriptor = grpc.loadPackageDefinition(definition);
const namespace = descriptor.opentelemetry.proto.collector[name].v1;

packages[name] = namespace;
packages[name] = namespace;
}

// helper functions

function intakeTraces(call, callback) {
const tracesReq = call.request;
// console.log('grpc spans', tracesReq);
console.dir(tracesReq, { depth: 5 });

callback(null, {
partial_success: {
rejected_spans: 0,
}
});
const tracesReq = call.request;
// console.log('grpc spans', tracesReq);
console.dir(tracesReq, {depth: 5});

callback(null, {
partial_success: {
rejected_spans: 0,
},
});
}

// function intakeMetrics(call, callback) {
Expand All @@ -44,30 +46,31 @@ function intakeTraces(call, callback) {
// // TODO: check proto
// }


/**
*
* @param {Object} options
* @param {string} options.hostname
* @param {number} options.port
*/
function startGrpc(options) {
const { hostname, port } = options;
const grpcServer = new grpc.Server();

console.log(packages.trace.TraceService)
grpcServer.addService(packages.trace.TraceService.service, {
Export: intakeTraces,
});

grpcServer.bindAsync(`${hostname}:${port}`, grpc.ServerCredentials.createInsecure(), () => {
console.log('grpc started', port);
grpcServer.start();
});

const {hostname, port} = options;
const grpcServer = new grpc.Server();

console.log(packages.trace.TraceService);
grpcServer.addService(packages.trace.TraceService.service, {
Export: intakeTraces,
});

grpcServer.bindAsync(
`${hostname}:${port}`,
grpc.ServerCredentials.createInsecure(),
() => {
console.log('grpc started', port);
grpcServer.start();
}
);
}


module.exports = {
startGrpc,
startGrpc,
};
Loading

0 comments on commit aa037ec

Please sign in to comment.