Skip to content

Fix service name caching based on context path#1326

Merged
eyalkoren merged 2 commits intoelastic:masterfrom
eyalkoren:webapp-redeployment-service-name-issue
Aug 6, 2020
Merged

Fix service name caching based on context path#1326
eyalkoren merged 2 commits intoelastic:masterfrom
eyalkoren:webapp-redeployment-service-name-issue

Conversation

@eyalkoren
Copy link
Contributor

@eyalkoren eyalkoren commented Aug 5, 2020

What does this PR do?

Fixes service name caching.

Context

A bug reported in https://discuss.elastic.co/t/apm-agent-not-working-after-updating-war-in-tomcat/239750 - after un-deploying and re-deploying a web app on a Servlet container, the web-app-specific service name (based on auto-discovery) is not used anymore. Instead, events are reported with the generic process service name.
The problem is with two inconsistent caches of the service name - it is cached in ServletTransactionHelper based on the context path and then in ElasticApmTracer based on the instrumented class's initiating class loader. When redeploying an app, the same context app is used with a different class loader and this causes the problem.

NOTE: I am not sure the first caching even worth it, only a few String comparisons. Maybe it's enough to leave only the second (class-loader-based) caching in ElasticApmTracer).

Checklist

  • This is an enhancement of existing features, or a new feature in existing plugins
    • I have updated CHANGELOG.asciidoc
    • I have added tests that prove my fix is effective or that my feature works
    • Added an API method or config option? Document in which version this will be introduced
    • I have made corresponding changes to the documentation
  • This is a bugfix
  • This is a new plugin
    • I have updated CHANGELOG.asciidoc
    • My code follows the style guidelines of this project
    • I have made corresponding changes to the documentation
    • I have added tests that prove my fix is effective or that my feature works
    • New and existing unit tests pass locally with my changes
    • I have updated supported-technologies.asciidoc
    • Added an API method or config option? Document in which version this will be introduced
    • Added an instrumentation plugin? Describe how you made sure that old, non-supported versions are not instrumented by accident.
  • This is something else
@eyalkoren eyalkoren requested a review from felixbarny August 5, 2020 05:59
@eyalkoren eyalkoren self-assigned this Aug 5, 2020
@ghost
Copy link

ghost commented Aug 5, 2020

💚 Build Succeeded

Pipeline View Test View Changes Artifacts preview

Expand to view the summary

Build stats

  • Build Cause: [Pull request #1326 updated]

  • Start Time: 2020-08-05T09:52:09.422+0000

  • Duration: 46 min 38 sec

Test stats 🧪

Test Results
Failed 0
Passed 1438
Skipped 11
Total 1449

Copy link
Member

@felixbarny felixbarny left a comment

Choose a reason for hiding this comment

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

Please describe the error scenario in a comment. Either in code or in the PR description. This makes it much easier to understand the context of a change compared to linking to a long thread.

Brilliant investigation BTW and sorry for introducing the bug 😬

public class ServletGlobalState {

public static final Set<String> nameInitialized = Collections.newSetFromMap(new ConcurrentHashMap<String, Boolean>());
public static final WeakConcurrentMap<ClassLoader, Boolean> nameInitialized = WeakMapSupplier.createMap();
Copy link
Member

Choose a reason for hiding this comment

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

Just FYT, in #1303, I've create a WeakMapSupplier.createSet() method.

Copy link
Contributor Author

Choose a reason for hiding this comment

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

If I remember that after merging #1303, I will switch to using it instead.

@eyalkoren eyalkoren merged commit 2fcf4b6 into elastic:master Aug 6, 2020
@eyalkoren eyalkoren deleted the webapp-redeployment-service-name-issue branch August 6, 2020 11:28
@SylvainJuge SylvainJuge added bug Bugs and removed type: bug labels Feb 2, 2021
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

bug Bugs

3 participants