Skip to content

Commit

Permalink
feat(rum): allow creating blocking spans from agent api (elastic#875)
Browse files Browse the repository at this point in the history
  • Loading branch information
vigneshshanmugam authored Aug 18, 2020
1 parent 8b24836 commit 50e692d
Show file tree
Hide file tree
Showing 5 changed files with 38 additions and 7 deletions.
18 changes: 15 additions & 3 deletions docs/agent-api.asciidoc
Original file line number Diff line number Diff line change
Expand Up @@ -141,7 +141,7 @@ NOTE: The payload will be dropped if one of the filters return a falsy value.

[source,js]
----
apm.startTransaction(name, type, options)
const transaction = apm.startTransaction(name, type, options)
----


Expand Down Expand Up @@ -185,17 +185,29 @@ NOTE: This method returns `undefined` if apm is disabled or if <<active,active>>

[source,js]
----
var span = apm.startSpan(name, type)
const span = apm.startSpan(name, type, options)
----

Starts and returns a new span on the current transaction.
Starts and returns a new span associated with the current active transaction.

Arguments:

* `name` - The name of the span (string). Defaults to `Unknown`

* `type` - The type of the span (string). Defaults to `custom`

* `options` - The following options are supported:

** `blocking` - Blocks the associated transaction from ending till this span is ended. Blocked spans
automatically creates an internal task. Defaults to false

** `parentId` - Parent id associated with the new span. Defaults to current transaction id

** `sync` - Denotes if the span is synchronous or asynchronous. Defaults to null


Blocked spans allow users to control the early closing of <<custom-managed-transactions, managed transactions>> in few cases when the app contains lots of async activity which cannot be tracked by the agent.

NOTE: This method returns `undefined` if apm is disabled or if <<active,active>> flag is set to `false` in the config.


Expand Down
2 changes: 1 addition & 1 deletion docs/custom-transactions.asciidoc
Original file line number Diff line number Diff line change
Expand Up @@ -46,7 +46,7 @@ const span = transaction.startSpan('async-task', 'app', { blocking: true })
setTimeout(() => {
span.end()
/**
* This would also end the managed transaction once all the blocking tasks are completed and
* This would also end the managed transaction once all the blocking spans are completed and
* transaction would also contain other timing information and spans similar to auto
* instrumented transactions like `page-load` and `route-change`.
*/
Expand Down
2 changes: 1 addition & 1 deletion docs/transaction-api.asciidoc
Original file line number Diff line number Diff line change
Expand Up @@ -65,7 +65,7 @@ Arguments:
* `options` - The following options are supported:

** `blocking` - Blocks the associated transaction from ending till this span is ended. Blocked spans
automatically internally creates a task. Defaults to false
automatically creates an internal task. Defaults to false

** `parentId` - Parent id associated with the new span. Defaults to current transaction id

Expand Down
4 changes: 2 additions & 2 deletions packages/rum/src/apm-base.js
Original file line number Diff line number Diff line change
Expand Up @@ -250,12 +250,12 @@ export default class ApmBase {
}
}

startSpan(name, type) {
startSpan(name, type, options) {
if (this.isEnabled()) {
var transactionService = this.serviceFactory.getService(
'TransactionService'
)
return transactionService.startSpan(name, type)
return transactionService.startSpan(name, type, options)
}
}

Expand Down
19 changes: 19 additions & 0 deletions packages/rum/test/specs/apm-base.spec.js
Original file line number Diff line number Diff line change
Expand Up @@ -326,6 +326,25 @@ describe('ApmBase', function() {
expect(configService.get('context.tags')).toEqual(labels)
})

it('should allow creating blocked spans on current transaction', () => {
apmBase.init({
serviceName,
serverUrl,
instrument: false
})
const tr = apmBase.startTransaction('blocking-transaction', 'custom', {
managed: true
})
const span1 = apmBase.startSpan('span1')
const span2 = apmBase.startSpan('span2', 'custom', { blocking: true })

expect(tr._activeTasks.size).toBe(1)
span1.end()
expect(tr.ended).toBe(false)
span2.end()
expect(tr.ended).toBe(true)
})

it('should fetch central config', done => {
const apmServer = serviceFactory.getService('ApmServer')
const configService = serviceFactory.getService('ConfigService')
Expand Down

0 comments on commit 50e692d

Please sign in to comment.