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

Add support for user defined attributes in OTLPHandler #1952

Merged
merged 14 commits into from
Aug 16, 2021

Conversation

adriangb
Copy link
Contributor

@adriangb adriangb commented Jul 14, 2021

Description

This gives users the ability to inject custom attributes into emitted logs.

I think this should solve the discussion in https://github.com/open-telemetry/opentelemetry-python/pull/1903/files#r653092699

Type of change

Please delete options that are not relevant.

  • Bug fix (non-breaking change which fixes an issue)
  • New feature (non-breaking change which adds functionality)
  • Breaking change (fix or feature that would cause existing functionality to not work as expected)
  • This change requires a documentation update

How Has This Been Tested?

I added 2 tests for the functionality verifying that the attributes end up in the emitted LogRecord and that the user can specify a sub-key to pull attributes from.

Does This PR Require a Contrib Repo Change?

  • Yes. - Link to PR:
  • No.

Checklist:

  • Followed the style guidelines of this project
  • Changelogs have been updated
  • Unit tests have been added
  • Documentation has been updated

@adriangb adriangb requested review from a team, ocelotl and srikanthccv and removed request for a team July 14, 2021 04:13
@linux-foundation-easycla
Copy link

linux-foundation-easycla bot commented Jul 14, 2021

CLA Signed

The committers are authorized under a signed CLA.

Copy link
Member

@srikanthccv srikanthccv left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Thanks for trying to complete todo item. This was originally added because there is some clarification I wanted to get from logs sig and some questions on semantic conventions. I am not sure if we want to proceed with this as is.

@codeboten
Copy link
Contributor

@lonewolf3739 any update from the SIG on this one? Just wanted to make sure we can get this reviewed if it's still useful.

@srikanthccv
Copy link
Member

This is fine. There were other changes when this PR opened and now it just has the extra attributes support.

@@ -244,20 +244,58 @@ def force_flush(self, timeout_millis: int = 30000) -> bool:
return True


# skip natural LogRecord attributes
# http://docs.python.org/library/logging.html#logrecord-attributes
_RESERVED_ATTRS = frozenset(
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

We are skipping now to separate the user-defined extra attributes but we will revisit this later if needed.

@adriangb
Copy link
Contributor Author

adriangb commented Aug 4, 2021

@ocelotl anything needed on my end to merge this?

@srikanthccv
Copy link
Member

@adriangb Please fix the lint.

@ocelotl
Copy link
Contributor

ocelotl commented Aug 4, 2021

@ocelotl anything needed on my end to merge this?

Yes, as @lonewolf3739 mentioned, please make sure the lint tests are passing. This might help.

@adriangb
Copy link
Contributor Author

adriangb commented Aug 4, 2021

I'm working on it. Your setup / instructions in CONTRIBUTING.md are very broken (and this has been the case for months, i.e. #1846), so something that should be happening automatically / take 1 minute is taking a while. Please fix that, otherwise contributing is very hard.

@adriangb
Copy link
Contributor Author

adriangb commented Aug 4, 2021

Well using your link I tried tox -e lint (and after cloning the contrib repo) I get:

tox -e lint                                                                                          
lint run-test-pre: commands[2] | python -m pip install .../opentelemetry-python/opentelemetry-python-contrib/opentelemetry-instrumentation
ERROR: Invalid requirement: '.../opentelemetry-python/opentelemetry-python-contrib/opentelemetry-instrumentation'
Hint: It looks like a path. File '.../opentelemetry-python/opentelemetry-python-contrib/opentelemetry-instrumentation' does not exist.

Looking at the contrib-repo, I can see that there is no opentelemetry-instrumentation folder. Am I missing something here or is tox -e lint also broken?

I'm also pretty confused as to why I need stuff from contrib to test (never mind lint) stuff in core. What is the point of having a core package if it depends on the contrib package 😕 ?

@ocelotl
Copy link
Contributor

ocelotl commented Aug 4, 2021

Well using your link I tried tox -e lint (and after cloning the contrib repo) I get:

tox -e lint                                                                                          
lint run-test-pre: commands[2] | python -m pip install .../opentelemetry-python/opentelemetry-python-contrib/opentelemetry-instrumentation
ERROR: Invalid requirement: '.../opentelemetry-python/opentelemetry-python-contrib/opentelemetry-instrumentation'
Hint: It looks like a path. File '.../opentelemetry-python/opentelemetry-python-contrib/opentelemetry-instrumentation' does not exist.

Looking at the contrib-repo, I can see that there is no opentelemetry-instrumentation folder. Am I missing something here or is tox -e lint also broken?

Please make sure your branch is updated against main. We moved opentelemetry-instrumentation to the core repo several commits ago.

@codeboten
Copy link
Contributor

@adriangb the opentelemetry-instrumentation folder was moved from contrib to this repo. Is it possible the tox file is out of sync w/ main?

@adriangb
Copy link
Contributor Author

adriangb commented Aug 4, 2021

Well I just went into the logs and fixed the 1 line pylint was complaining about.

@adriangb
Copy link
Contributor Author

adriangb commented Aug 4, 2021

To avoid this #1952 (comment) in the future, I'd suggest you move away from long-lived feature branches. They're problematic for many reasons.

@codeboten
Copy link
Contributor

To avoid this #1952 (comment) in the future, I'd suggest you move away from long-lived feature branches. They're problematic for many reasons.

I agree, the logs/metrics branches are a special case for the work around experimental features, but yes, long lived branches are terrible

@adriangb
Copy link
Contributor Author

adriangb commented Aug 4, 2021

Looks like we're all ✅ now @ocelotl

@ocelotl
Copy link
Contributor

ocelotl commented Aug 4, 2021

Let's see...

@ocelotl
Copy link
Contributor

ocelotl commented Aug 4, 2021

I see tests that are still running will give this a bit more time

@adriangb
Copy link
Contributor Author

adriangb commented Aug 4, 2021

Are those tests really running or is CI broken / stuck? It's been "running" for almost 2 hours with no output.

Edit: it looks like at least contrib-build (py37, instrumentation, ubuntu-latest) is not even running tests. It's stuck on Install tox. Installing tox cannot take 2 hours.

@adriangb
Copy link
Contributor Author

adriangb commented Aug 4, 2021

@ocelotl can you please restart jobs here too?

@ocelotl
Copy link
Contributor

ocelotl commented Aug 4, 2021

Sure

@staticmethod
def _get_attributes(record: logging.LogRecord) -> Attributes:
return {
k: v for k, v in vars(record).items() if k not in _RESERVED_ATTRS
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Is extra the only keyword that will be filtered in?

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

@adriangb
Copy link
Contributor Author

adriangb commented Aug 4, 2021

@ocelotl doesn't look like CI jobs were restarted, I'm still seeing the same Installing tox stage that's now at 4 hours.

@ocelotl
Copy link
Contributor

ocelotl commented Aug 4, 2021

@ocelotl doesn't look like CI jobs were restarted, I'm still seeing the same Installing tox stage that's now at 4 hours.

Hmm... ok, seems like they have actually restarted now. Sorry for all the inconveniences, apparently the entire Github opentelemetry organization is having issues with CI.

@adriangb
Copy link
Contributor Author

adriangb commented Aug 5, 2021

Looks like we're passing aside from a benchmark that says something is 7x slower (?). I don't think this PR would make anything 7x slower, so I'm guessing it's more strange CI stuff?

@ocelotl
Copy link
Contributor

ocelotl commented Aug 5, 2021

Looks like we're passing aside from a benchmark that says something is 7x slower (?). I don't think this PR would make anything 7x slower, so I'm guessing it's more strange CI stuff?

Yes, seems like more trouble with Github-CI. The only thing I can do now is try running them again 🤷

I have also filed a ticket to Github regarding this problem.

@srikanthccv
Copy link
Member

@codeboten this has different base branch. Same fix should applied to it as well.

@codeboten
Copy link
Contributor

@codeboten this has different base branch. Same fix should applied to it as well.

@lonewolf3739 correct, I created a PR here #2037

@ocelotl
Copy link
Contributor

ocelotl commented Aug 13, 2021

Re-running stuck jobs for this PR ✌️

@adriangb
Copy link
Contributor Author

Re-running stuck jobs for this PR ✌️

Thank you! Looks like everything is passing now except for benchmarks, which I'm guessing is noise? All we're adding there is a single dict-comprehension. Except in some extreme case (all that happens in the entire program is logging and each log has 1000 user defined attributes), this should not change performance at all.

@ocelotl
Copy link
Contributor

ocelotl commented Aug 16, 2021

Re-running stuck jobs for this PR v

Thank you! Looks like everything is passing now except for benchmarks, which I'm guessing is noise? All we're adding there is a single dict-comprehension. Except in some extreme case (all that happens in the entire program is logging and each log has 1000 user defined attributes), this should not change performance at all.

We have seen performance issues with Pypy before, I'll try again.

If it still refuses to pass we may have to take a look into the performance requirements for Pypy, it should be ok that they are lower than the rest.

@lzchen lzchen merged commit d0dbc0b into open-telemetry:logs Aug 16, 2021
lzchen pushed a commit to lzchen/opentelemetry-python that referenced this pull request Oct 15, 2021
owais added a commit that referenced this pull request Nov 3, 2021
* Add initial overall structure and classes for logs sdk (#1894)

* Add global LogEmitterProvider and convenience function get_log_emitter (#1901)

* Add OTLPHandler for standard library logging module (#1903)

* Add LogProcessors implementation (#1916)

* Fix typos in test_handler.py (#1953)

* Add support for OTLP Log exporter (#1943)

* Add support for user defined attributes in OTLPHandler (#1952)

* use timeout in force_flush (#2118)

* use timeout in force_flush

* fix lint

* Update opentelemetry-sdk/src/opentelemetry/sdk/logs/export/__init__.py

Co-authored-by: Srikanth Chekuri <srikanth.chekuri92@gmail.com>

* fix lint

Co-authored-by: Srikanth Chekuri <srikanth.chekuri92@gmail.com>

* add a ConsoleExporter for logging (#2099)

Co-authored-by: Srikanth Chekuri <srikanth.chekuri92@gmail.com>

* Update SDK docs and Add example with OTEL collector logging (debug) exporter (#2050)

* Fix exception in severity number transformation (#2208)

* Fix exception with warning message transformation

* Fix lint

* Fix lint

* fstring

* Demonstrate how to set the Resource for LogEmitterProvider (#2209)

* Demonstrate how to set the Resource for LogEmitterProvider

Added a Resource to the logs example to make it more complete.
Previously it was using the built-in Resource. Now it adds the
service.name and service.instance.id attributes.

The resulting emitted log records look like this:
```
Resource labels:
     -> telemetry.sdk.language: STRING(python)
     -> telemetry.sdk.name: STRING(opentelemetry)
     -> telemetry.sdk.version: STRING(1.5.0)
     -> service.name: STRING(shoppingcart)
     -> service.instance.id: STRING(instance-12)
InstrumentationLibraryLogs #0
InstrumentationLibrary __main__ 0.1
LogRecord #0
Timestamp: 2021-10-14 18:33:43.425820928 +0000 UTC
Severity: ERROR
ShortName:
Body: Hyderabad, we have a major problem.
Trace ID: ce1577e4a703f42d569e72593ad71888
Span ID: f8908ac4258ceff6
Flags: 1
```

* Fix linting

* Use batch processor in example (#2225)

* move logs to _logs (#2240)

* move logs to _logs

* fix lint

* move log_exporter to _log_exporter as it's still experimental (#2252)

Co-authored-by: Srikanth Chekuri <srikanth.chekuri92@gmail.com>
Co-authored-by: Adrian Garcia Badaracco <1755071+adriangb@users.noreply.github.com>
Co-authored-by: Leighton Chen <lechen@microsoft.com>
Co-authored-by: Tigran Najaryan <4194920+tigrannajaryan@users.noreply.github.com>
Co-authored-by: Owais Lone <owais@users.noreply.github.com>
codeboten pushed a commit to codeboten/opentelemetry-python that referenced this pull request Nov 3, 2021
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

5 participants