question-mark
Stuck on an issue?

Lightrun Answers was designed to reduce the constant googling that comes with debugging 3rd party libraries. It collects links to all the places you might be looking at while hunting down a tough bug.

And, if you’re still stuck at the end, we’re happy to hop on a call to see how we can help out.

Custom Properties/Dimensions Design Discussion

See original GitHub issue

I’m happy to help contribute the code, but wanted to share design first and discuss to see if it is right approach.

Is your feature request related to a problem?

Support for custom properties/dimensions was recently added for logging messages using args.

logger.info("my message", {"customProperty": "myValue"})

There were a few concerns with the current approach when applying it to existing code:

  1. Current approach with *args breaks existing logging functionality for other registered logging handlers, which are using args for lazy string interpolation. We would need to go back and change existing log calls to remove existing args (interpolate strings first) and change arg type to dictionary, if we wanted to log custom properties.
  2. It breaks LoggerAdapter and filter patterns for passing contextual information to all messages. For LoggerAdapter pattern, we cannot modify args at all. For filter pattern, we would currently need to overwrite the args there.
  3. We need to pass the custom dimensions into every call to the logger because of concern number 2.
  4. It only supports messages and not metrics/exceptions, so approach is not consistent.

Describe the solution you’d like.

Instead of using the *args as a dict, I would suggest using the **kwargs approach.

For the logging library, **kwargs are added to the dict of LoggerRecord. When we process the record, we could look for customDimensions to be present on the record as an attribute. If it is there, then add it to the properties to be sent to Application Insights.

This solution would leave in place existing behavior, since end users can still use kwargs for storing other important attributes as long as it does not conflict with customDimensions (which is very application insights specific term).

As part of this request, it would also be great to have consistency across messages, exceptions, events, etc, instead of only supporting custom properties for messages.

Below are examples of logging use cases with the suggested approach:

Example 1: Simple Case for Adding Properties Once

properties = {‘customDimensions’: {‘testKey’: ‘testValue’}}
logger.warning(“Starting retry %s”, retries, properties)

Example 2: Pass Custom Properties as Context with LoggerAdaptor pattern

# FYI, just quickly sketching out code for explaining, more checking is needed for nulls, etc.
class CustomAdapter(logging.LoggerAdapter):
    def process(self, msg, kwargs):
	kwargs['customDimensions'].update(self.extra[‘customDimensions’])
        return msg, kwargs

logger = logging.getLogger(__name__)
adapter = CustomAdapter(logger, {‘customDimensions’: {‘testKey’: ‘testValue’}})
adapter.warning(‘hello’)
adapter.info(‘hello %s’, name, {‘customDimensions’: {‘optionalExtraKey’: ‘optionalExtraValue’}})

Example 3: Pass Custom Properties as Context with LoggerFilter pattern

# FYI, just quickly sketching out code for explaining, more checking is needed for nulls, etc.

class ContextFilter(logging.Filter):

    CUSTOM_DIMENSIONS = {‘testKey’: ‘testValue’}

    def filter(self, record):
        setattr(record, ‘customDimensions’, CUSTOM_DIMENSIONS)
	return True

logger = logging.getLogger(__name__)
f = ContextFilter()
logger.addFilter(f)

logger.warning(‘hello’)
logger.info(‘hello %s’, name, {‘customDimensions’: {‘optionalExtraKey’: ‘optionalExtraValue’}})

Describe alternatives you’ve considered.

We tried the new approach with adding properties as args, but it required too much change to existing logs and required passing args to every call. We hope to instead use existing patterns for contextual logging.

Issue Analytics

  • State:closed
  • Created 4 years ago
  • Comments:9 (6 by maintainers)

github_iconTop GitHub Comments

2reactions
lzchencommented, Jan 22, 2020

@ctaggart I’ll be cutting a new release at the end of Jan.

1reaction
ctaggartcommented, Jan 22, 2020

This one is blocking our use of the library. I would love to see a new build released.

Read more comments on GitHub >

github_iconTop Results From Across the Web

drive a dimension using a Custom Property? - Eng-Tips
Here's an example of what I want to do: I create a custom property from File/Properties>Custom tab. I give it a numerical value...
Read more >
A Strategy Guide To CSS Custom Properties
Custom Properties are a little bit like variables in preprocessors but have some important differences. The first and most obvious difference is ...
Read more >
Adding Associative Dimensions Into Custom Properties - 2019
Open the part and display the dimensions to add to Custom Properties. Under Property Name, type a name or choose one from the...
Read more >
How to use Custom Properties inside of dimension text. - Reddit
You can create a note and add the custom property with the $PRP: call out and then the note will give you that...
Read more >
Tech Tip Tuesday: Custom Properties and Title Blocks - Part 1
Part 1 of 2. Many SOLIDWORKS users manually update information in a drawing Title Block which can be time consuming and decrease ...
Read more >

github_iconTop Related Medium Post

No results found

github_iconTop Related StackOverflow Question

No results found

github_iconTroubleshoot Live Code

Lightrun enables developers to add logs, metrics and snapshots to live code - no restarts or redeploys required.
Start Free

github_iconTop Related Reddit Thread

No results found

github_iconTop Related Hackernoon Post

No results found

github_iconTop Related Tweet

No results found

github_iconTop Related Dev.to Post

No results found

github_iconTop Related Hashnode Post

No results found