Skip to content

Commit

Permalink
chore: Updated rules engine to account for fallbacks
Browse files Browse the repository at this point in the history
  • Loading branch information
jsumners-nr committed Dec 10, 2024
1 parent c01d724 commit 2c0af80
Show file tree
Hide file tree
Showing 2 changed files with 75 additions and 7 deletions.
47 changes: 41 additions & 6 deletions lib/otel/rules.js
Original file line number Diff line number Diff line change
Expand Up @@ -75,15 +75,26 @@ class Rule {
}

get type() {
return this.#type
if (this.#type) {
return this.#type
}
return this.isServerRule ? 'server' : 'client'
}

get isServerRule() {
return this.#spanKinds.includes(Rule.OTEL_SPAN_KIND_SERVER)
get isClientRule() {
return this.#spanKinds.includes(Rule.OTEL_SPAN_KIND_CLIENT) || this.isProducer
}

get isClientRule() {
return this.#spanKinds.includes(Rule.OTEL_SPAN_KIND_CLIENT)
get isConsumer() {
return this.#spanKinds.includes('consumer')
}

get isProducer() {
return this.#spanKinds.includes('producer')
}

get isServerRule() {
return this.#spanKinds.includes(Rule.OTEL_SPAN_KIND_SERVER) || this.isConsumer
}

/**
Expand Down Expand Up @@ -112,11 +123,23 @@ class Rule {

class RulesEngine {
#serverRules = new Map()
#fallbackServerRules = new Map()
#clientRules = new Map()
#fallbackClientRules = new Map()

constructor() {
for (const inputRule of srcJson) {
const rule = new Rule(inputRule)

if (/fallback/i.test(rule.name) === true) {
if (rule.isServerRule === true) {
this.#fallbackServerRules.set(rule.name, rule)
} else if (rule.isClientRule === true) {
this.#fallbackClientRules.set(rule.name, rule)
}
continue
}

if (rule.isServerRule === true) {
this.#serverRules.set(rule.name, rule)
} else if (rule.isClientRule === true) {
Expand All @@ -136,10 +159,16 @@ class RulesEngine {
test(otelSpan) {
let result

switch (otelSpan.kind) {
detector: switch (otelSpan.kind) {
case SpanKind.SERVER:
case SpanKind.CONSUMER: {
for (const rule of this.#serverRules.values()) {
if (rule.matches(otelSpan) === true) {
result = rule
break detector
}
}
for (const rule of this.#fallbackServerRules.values()) {
if (rule.matches(otelSpan) === true) {
result = rule
break
Expand All @@ -151,6 +180,12 @@ class RulesEngine {
case SpanKind.CLIENT:
case SpanKind.PRODUCER: {
for (const rule of this.#clientRules.values()) {
if (rule.matches(otelSpan) === true) {
result = rule
break detector
}
}
for (const rule of this.#fallbackClientRules.values()) {
if (rule.matches(otelSpan) === true) {
result = rule
break
Expand Down
35 changes: 34 additions & 1 deletion test/unit/lib/otel/rules.test.js
Original file line number Diff line number Diff line change
Expand Up @@ -20,10 +20,10 @@ const spanContext = {
spanId: '6e0c63257de34c92',
traceFlags: TraceFlags.SAMPLED
}
const parentId = '5c1c63257de34c67'

test('engine returns correct matching rule', () => {
const engine = new RulesEngine()
const parentId = '5c1c63257de34c67'
const span = new Span(tracer, ROOT_CONTEXT, 'test-span', spanContext, SpanKind.SERVER, parentId)
span.setAttribute('http.request.method', 'GET')
span.end()
Expand All @@ -32,3 +32,36 @@ test('engine returns correct matching rule', () => {
assert.notEqual(rule, undefined)
assert.equal(rule.name, 'OtelHttpServer1_23')
})

test('consumer does not match fallback rule', () => {
const engine = new RulesEngine()
const span = new Span(tracer, ROOT_CONTEXT, 'test-span', spanContext, SpanKind.CONSUMER, parentId)
span.setAttribute('messaging.operation', 'create')
span.end()

const rule = engine.test(span)
assert.notEqual(rule, undefined)
assert.equal(rule.name, 'OtelMessagingConsumer1_24')
})

test('fallback server rule is met', () => {
const engine = new RulesEngine()
const span = new Span(tracer, ROOT_CONTEXT, 'test-span', spanContext, SpanKind.SERVER, parentId)
span.setAttribute('foo.bar', 'baz')
span.end()

const rule = engine.test(span)
assert.notEqual(rule, undefined)
assert.equal(rule.name, 'FallbackServer')
})

test('fallback client rule is met', () => {
const engine = new RulesEngine()
const span = new Span(tracer, ROOT_CONTEXT, 'test-span', spanContext, SpanKind.PRODUCER, parentId)
span.setAttribute('foo.bar', 'baz')
span.end()

const rule = engine.test(span)
assert.notEqual(rule, undefined)
assert.equal(rule.name, 'FallbackClient')
})

0 comments on commit 2c0af80

Please sign in to comment.