Skip to content

Commit

Permalink
Merge pull request #1052 from moleculerjs/tracing_flattenTags
Browse files Browse the repository at this point in the history
Add safetyTags property to tracer exporters
  • Loading branch information
icebob authored Jan 8, 2022
2 parents c48d5a0 + 3b95ec5 commit 633817e
Show file tree
Hide file tree
Showing 17 changed files with 359 additions and 28 deletions.
111 changes: 111 additions & 0 deletions dev/issue-777.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,111 @@
"use strict";

/*const tracer = require("dd-trace").init({
service: "moleculer", // shows up as Service in Datadog UI
url: "http://127.0.0.1:8126",
debug: true,
samplingPriority: "USER_KEEP"
});
tracer.use("http");
tracer.use("ioredis");
*/
const ServiceBroker = require("../src/service-broker");
("use strict");

const { MoleculerError } = require("../src/errors");
const _ = require("lodash");
const { inspect } = require("util");

const THROW_ERR = false;

// Create broker
const broker = new ServiceBroker({
nodeID: "node-1",
logger: console,
logLevel: "info",
//transporter: "redis://localhost:6379",
//cacher: true, //"redis://localhost:6379",

tracing: {
events: true,
stackTrace: true,
sampling: {
rate: 1
//tracesPerSecond: 1
},
exporter: [
/*{
type: "Console",
options: {
width: 100,
gaugeWidth: 30,
logger: console.info
}
},*/
/*{
type: "Datadog",
options: {
tracer
}
}*/
{
type: "Zipkin",
options: {
safetyTags: false,
baseURL: "http://127.0.0.1:9411"
}
},
{
type: "Jaeger",
options: {
safetyTags: false,
endpoint: "http://localhost:14268/api/traces"
}
}
/*{
type: "Event",
options: {
}
}*/
/*{
type: "EventLegacy"
}*/
]
}
});

broker.createService({
name: "greeter",
actions: {
hello: {
tracing: {
safetyTags: true
},
handler(ctx) {
return `Hello!`;
}
}
}
});

// Start server
broker.start().then(() => {
broker.repl();

const a = {
aa: 5,
c: "John"
};
const b = {
bb: 10,
a: a
};

a.b = b;

// Call action
setInterval(() => {
broker.call("greeter.hello", a).then(console.log).catch(console.error);
}, 5000);
});
15 changes: 8 additions & 7 deletions dev/tracing.js
Original file line number Diff line number Diff line change
Expand Up @@ -2,8 +2,8 @@

const tracer = require("dd-trace").init({
service: "moleculer", // shows up as Service in Datadog UI
url: "http://192.168.0.181:8126",
debug: true,
url: "http://localhost:8126",
debug: false,
samplingPriority: "USER_KEEP"
});

Expand Down Expand Up @@ -48,19 +48,20 @@ const broker = new ServiceBroker({
options: {
tracer
}
}
/*{
},
{
type: "Zipkin",
options: {
baseURL: "http://192.168.0.181:9411",
baseURL: "http://localhost:9411"
}
},
{
type: "Jaeger",
options: {
host: "192.168.0.181",
endpoint: "http://localhost:14268/api/traces"
//host: "localhost",
}
},*/
}
/*{
type: "Event",
options: {
Expand Down
14 changes: 11 additions & 3 deletions src/middlewares/tracing.js
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,7 @@
"use strict";

const _ = require("lodash");
const { isFunction, isPlainObject } = require("../utils");
const { isFunction, isPlainObject, safetyObject } = require("../utils");

module.exports = function TracingMiddleware(broker) {
const tracer = broker.tracer;
Expand All @@ -22,7 +22,7 @@ module.exports = function TracingMiddleware(broker) {
ctx.requestID = ctx.requestID || tracer.getCurrentTraceID();
ctx.parentID = ctx.parentID || tracer.getActiveSpanID();

const tags = {
let tags = {
callingLevel: ctx.level,
action: ctx.action
? {
Expand Down Expand Up @@ -69,6 +69,10 @@ module.exports = function TracingMiddleware(broker) {
tags.meta = _.pick(ctx.meta, actionTags.meta);
}

if (opts.safetyTags) {
tags = safetyObject(tags);
}

let spanName = `action '${ctx.action.name}'`;
if (opts.spanName) {
switch (typeof opts.spanName) {
Expand Down Expand Up @@ -144,7 +148,7 @@ module.exports = function TracingMiddleware(broker) {
ctx.requestID = ctx.requestID || tracer.getCurrentTraceID();
ctx.parentID = ctx.parentID || tracer.getActiveSpanID();

const tags = {
let tags = {
event: {
name: event.name,
group: event.group
Expand Down Expand Up @@ -188,6 +192,10 @@ module.exports = function TracingMiddleware(broker) {
tags.meta = _.pick(ctx.meta, eventTags.meta);
}

if (opts.safetyTags) {
tags = safetyObject(tags);
}

let spanName = `event '${ctx.eventName}' in '${service.fullName}'`;
if (opts.spanName) {
switch (typeof opts.spanName) {
Expand Down
21 changes: 21 additions & 0 deletions src/tracing/exporters/README.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,21 @@
# Moleculer Trace Exporters

## Running Jaeger

```bash
docker run -d --name jaeger -p 5775:5775/udp -p 6831:6831/udp -p 6832:6832/udp -p 5778:5778 -p 16686:16686 -p 14250:14250 -p 14268:14268 -p 14269:14269 jaegertracing/all-in-one:latest
```

**UI:** http://<docker-ip>:16686/

## Running Zipkin

```bash
docker run -d -p 9411:9411 --name=zipkin openzipkin/zipkin
```

## Running DataDog Agent

```bash
docker run -d --name dd-agent --restart unless-stopped -v /var/run/docker.sock:/var/run/docker.sock:ro -v /proc/:/host/proc/:ro -v /sys/fs/cgroup/:/host/sys/fs/cgroup:ro -e DD_API_KEY=123456 -e DD_APM_ENABLED=true -e DD_APM_NON_LOCAL_TRAFFIC=true -p 8126:8126 datadog/agent:latest
```
10 changes: 8 additions & 2 deletions src/tracing/exporters/base.js
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
"use strict";

const _ = require("lodash");
const { isObject } = require("../../utils");
const { isObject, safetyObject } = require("../../utils");

/**
* Abstract Trace Exporter
Expand All @@ -15,7 +15,9 @@ class BaseTraceExporter {
* @memberof BaseTraceExporter
*/
constructor(opts) {
this.opts = opts || {};
this.opts = _.defaultsDeep(opts, {
safetyTags: false
});
this.Promise = Promise; // default promise before logger is initialized
}

Expand Down Expand Up @@ -87,6 +89,10 @@ class BaseTraceExporter {
flattenTags(obj, convertToString = false, path = "") {
if (!obj) return null;

if (this.opts.safetyTags) {
obj = safetyObject(obj);
}

return Object.keys(obj).reduce((res, k) => {
const o = obj[k];
const pp = (path ? path + "." : "") + k;
Expand Down
4 changes: 0 additions & 4 deletions src/tracing/exporters/datadog.js
Original file line number Diff line number Diff line change
Expand Up @@ -5,10 +5,6 @@ const BaseTraceExporter = require("./base");
const asyncHooks = require("async_hooks");
const { isFunction } = require("../../utils");

/*
docker run -d --name dd-agent --restart unless-stopped -v /var/run/docker.sock:/var/run/docker.sock:ro -v /proc/:/host/proc/:ro -v /sys/fs/cgroup/:/host/sys/fs/cgroup:ro -e DD_API_KEY=123456 -e DD_APM_ENABLED=true -e DD_APM_NON_LOCAL_TRAFFIC=true -p 8126:8126 datadog/agent:latest
*/

let DatadogSpanContext;
let DatadogID;

Expand Down
6 changes: 0 additions & 6 deletions src/tracing/exporters/jaeger.js
Original file line number Diff line number Diff line change
Expand Up @@ -17,12 +17,6 @@ let Jaeger, GuaranteedThroughputSampler, RemoteControlledSampler, UDPSender, HTT
*
* http://jaeger.readthedocs.io/en/latest/getting_started/#all-in-one-docker-image
*
* Running Jaeger in Docker:
*
* docker run -d --name jaeger -p5775:5775/udp -p6831:6831/udp -p6832:6832/udp -p5778:5778 -p16686:16686 -p14268:14268 jaegertracing/all-in-one:latest
*
* UI: http://<docker-ip>:16686/
*
* @class JaegerTraceExporter
*/
class JaegerTraceExporter extends BaseTraceExporter {
Expand Down
4 changes: 0 additions & 4 deletions src/tracing/exporters/zipkin.js
Original file line number Diff line number Diff line change
Expand Up @@ -11,10 +11,6 @@ const { isFunction } = require("../../utils");
* API v2: https://zipkin.io/zipkin-api/#/
* API v1: https://zipkin.io/pages/data_model.html
*
* Running Zipkin in Docker:
*
* docker run -d -p 9411:9411 --name=zipkin openzipkin/zipkin
*
* @class ZipkinTraceExporter
*/
class ZipkinTraceExporter extends BaseTraceExporter {
Expand Down
Loading

0 comments on commit 633817e

Please sign in to comment.