Skip to content

Commit

Permalink
dedup decorator code
Browse files Browse the repository at this point in the history
  • Loading branch information
sabrenner committed Oct 17, 2024
1 parent 79f0d07 commit 7c7a328
Show file tree
Hide file tree
Showing 2 changed files with 8 additions and 40 deletions.
9 changes: 4 additions & 5 deletions packages/dd-trace/src/llmobs/noop.js
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,6 @@ const Span = require('../noop/span')
class NoopLLMObs {
constructor (noopTracer) {
this._tracer = noopTracer
this._span = new Span(noopTracer)
}

get enabled () {
Expand Down Expand Up @@ -46,14 +45,14 @@ class NoopLLMObs {
const ctx = ctxOrPropertyKey
if (ctx.kind !== 'method') return target

return llmobs.wrap(target, { name: ctx.name, ...options })
return llmobs.wrap({ name: ctx.name, ...options }, target)
} else {
const propertyKey = ctxOrPropertyKey
if (descriptor) {
if (typeof descriptor.value !== 'function') return descriptor

const original = descriptor.value
descriptor.value = llmobs.wrap(original, { name: propertyKey, ...options })
descriptor.value = llmobs.wrap({ name: propertyKey, ...options }, original)

return descriptor
} else {
Expand All @@ -62,7 +61,7 @@ class NoopLLMObs {
const original = target[propertyKey]
Object.defineProperty(target, propertyKey, {
...Object.getOwnPropertyDescriptor(target, propertyKey),
value: llmobs.wrap(original, { name: propertyKey, ...options })
value: llmobs.wrap({ name: propertyKey, ...options }, original)
})

return target
Expand All @@ -82,7 +81,7 @@ class NoopLLMObs {
flush () {}

active () {
return this._span
return new Span(this._tracer)
}
}

Expand Down
39 changes: 4 additions & 35 deletions packages/dd-trace/src/llmobs/sdk.js
Original file line number Diff line number Diff line change
Expand Up @@ -21,11 +21,13 @@ const LLMObsTagger = require('./tagger')

// communicating with writer
const { flushCh, evalMetricAppendCh } = require('./channels')
const NoopLLMObs = require('./noop')

class LLMObs {
class LLMObs extends NoopLLMObs {
constructor (tracer, llmobsModule, config) {
super(tracer)

this._config = config
this._tracer = tracer
this._llmobsModule = llmobsModule
this._tagger = new LLMObsTagger(config)
}
Expand Down Expand Up @@ -220,39 +222,6 @@ class LLMObs {
return this._tracer.wrap(name, spanOptions, wrapped)
}

decorate (options = {}) {
const llmobs = this
return function (target, ctxOrPropertyKey, descriptor) {
if (!ctxOrPropertyKey) return target
if (typeof ctxOrPropertyKey === 'object') {
const ctx = ctxOrPropertyKey
if (ctx.kind !== 'method') return target

return llmobs.wrap({ name: ctx.name, ...options }, target)
} else {
const propertyKey = ctxOrPropertyKey
if (descriptor) {
if (typeof descriptor.value !== 'function') return descriptor

const original = descriptor.value
descriptor.value = llmobs.wrap({ name: propertyKey, ...options }, original)

return descriptor
} else {
if (typeof target[propertyKey] !== 'function') return target[propertyKey]

const original = target[propertyKey]
Object.defineProperty(target, propertyKey, {
...Object.getOwnPropertyDescriptor(target, propertyKey),
value: llmobs.wrap({ name: propertyKey, ...options }, original)
})

return target
}
}
}
}

annotate (span, options) {
if (!this.enabled) {
logger.warn(
Expand Down

0 comments on commit 7c7a328

Please sign in to comment.