Skip to content

Commit

Permalink
Merge pull request #30 from throngar/custom-applicationinsights
Browse files Browse the repository at this point in the history
add option to setup applicationInsights with custom options
  • Loading branch information
ovhemert committed Sep 4, 2020
2 parents f61e65f + ab5c2ae commit e753e88
Show file tree
Hide file tree
Showing 4 changed files with 35 additions and 4 deletions.
17 changes: 16 additions & 1 deletion docs/API.md
Original file line number Diff line number Diff line change
Expand Up @@ -28,10 +28,25 @@ Example:
const writeStream = await insights.createWriteStream({
key: 'instrumentationkey'
})
````
```

#### key

Type: `String` *(optional)*

The Instrumentation Key of the Azure Application Insights account. If not specified, defaults to APPINSIGHTS_INSTRUMENTATIONKEY environment variable.

Or you could configure Azure Application Insights with your custom preferences by passing `setup` callback property:

```js
const writeStream = await insights.createWriteStream({
setup: (applicationInsights) =>
applicationInsights
.setup('instrumentationkey')
.setAutoCollectRequests(false)
.setAutoCollectDependencies(false)
.start()
})
```

The only parameter of the callback is the applicationInsights instate to setup and call `start` on.
8 changes: 6 additions & 2 deletions src/applicationinsights.js
Original file line number Diff line number Diff line change
Expand Up @@ -5,8 +5,12 @@ const stream = require('stream')

class Client {
constructor (options = {}) {
const iKey = options.key || process.env.APPINSIGHTS_INSTRUMENTATIONKEY
appInsights.setup(iKey).start()
if (options.setup) {
options.setup(appInsights)
} else {
const iKey = options.key || process.env.APPINSIGHTS_INSTRUMENTATIONKEY
appInsights.setup(iKey).start()
}
this.insights = appInsights.defaultClient
}

Expand Down
2 changes: 1 addition & 1 deletion src/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,7 @@ const streams = require('./streams')
const pumpify = require('pumpify')

async function createWriteStream (options = {}) {
if (!options.key && !process.env.APPINSIGHTS_INSTRUMENTATIONKEY) { throw Error('Instrumentation key missing') }
if (!options.setup && !options.key && !process.env.APPINSIGHTS_INSTRUMENTATIONKEY) { throw Error('Instrumentation key missing') }
const client = new insights.Client(options)

const parseJsonStream = streams.parseJsonStream()
Expand Down
12 changes: 12 additions & 0 deletions test/applicationinsights.test.js
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,18 @@ test('creates client', t => {
t.end()
})

test('creates client with custom applicationInsights', t => {
const client = new tested.Client({
setup: insights => {
insights.setup('blablabla').setUseDiskRetryCaching(false).start()
insights.defaultClient.config.endpointUrl = 'https://custom.endpoint'
}
})
t.equals(client.insights.config.instrumentationKey, 'blablabla')
t.equals(client.insights.config.endpointUrl, 'https://custom.endpoint')
t.end()
})

test('gets exception from log', t => {
const input = [
{ level: 10, time: 1532081790710, msg: 'trace message' },
Expand Down

0 comments on commit e753e88

Please sign in to comment.