Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

feat: Adds new configuration options to add custom tags (labels) to logs #2743

Open
wants to merge 3 commits into
base: main
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
13 changes: 12 additions & 1 deletion lib/aggregators/log-aggregator.js
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,7 @@
'use strict'

const logger = require('../logger').child({ component: 'logs_aggregator' })
const { isLogLabelingEnabled } = require('../../lib/util/application-logging')
const EventAggregator = require('./event-aggregator')

const NAMES = require('../metrics/names')
Expand Down Expand Up @@ -57,12 +58,22 @@ class LogAggregator extends EventAggregator {
}

const commonAttrs = this.agent.getServiceLinkingMetadata()
return [

const payload = [
{
common: { attributes: commonAttrs },
logs: formattedLogs
}
]

if (isLogLabelingEnabled(this.agent.config)) {
payload[0].common.attributes = {
...payload[0].common.attributes,
...this.agent.config.filteredLabels
}
}

return payload
}

add(logLine) {
Expand Down
29 changes: 29 additions & 0 deletions lib/config/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -24,6 +24,7 @@ const harvestConfigValidator = require('./harvest-config-validator')
const mergeServerConfig = new MergeServerConfig()
const { boolean: isTruthular } = require('./formatters')
const configDefinition = definition()
const parseLabels = require('../util/label-parser')

/**
* CONSTANTS -- we gotta lotta 'em
Expand Down Expand Up @@ -195,9 +196,37 @@ function Config(config) {

// 9. Set instance attribute filter using updated context
this.attributeFilter = new AttributeFilter(this)

const excludeList = this.application_logging.forwarding.labels.exclude

// 10. Setup labels after filtering for exclusions
if (excludeList.length > 0) {
this.application_logging.forwarding.labels.exclude = excludeList.map((k) => k.toLowerCase())
}

this._filterLabels()
}
util.inherits(Config, EventEmitter)

/**
* Compares the labels list to the excluded label list and removes any labels that need to be excluded.
* Then prefixing each label with "tags."
*/
Config.prototype._filterLabels = function filterLabels() {
const configuredLabels = parseLabels(this.labels)

const filteredLabels = {}

configuredLabels.forEach((label) => {
if (
!this.application_logging.forwarding.labels.exclude.includes(label.label_type.toLowerCase())
) {
filteredLabels[`tags.${label.label_type}`] = label.label_value
}
})
this.filteredLabels = filteredLabels
}

/**
* Because this module and logger depend on each other, the logger needs
* a way to inject the actual logger instance once it's constructed.
Expand Down
12 changes: 12 additions & 0 deletions lib/util/application-logging.js
Original file line number Diff line number Diff line change
Expand Up @@ -75,6 +75,18 @@ utils.isLogForwardingEnabled = function isLogForwardingEnabled(config, agent) {
)
}

/**
* Checks if application_logging.forwarding.labels is enabled and there are labels in config
*
* @param {object} config agent config
* @returns {boolean} is labeling enabled
*/
utils.isLogLabelingEnabled = function isLogLabelingEnabled(config) {
return !!(
config.application_logging.forwarding.labels.enabled && Object.keys(config.labels).length > 0
)
}

/**
* Increments both `Logging/lines` and `Logging/lines/<level>` call count
*
Expand Down
Loading
Loading