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

Improve/mdc #179

Merged
merged 13 commits into from
Sep 9, 2024
Merged

Improve/mdc #179

merged 13 commits into from
Sep 9, 2024

Conversation

testower
Copy link
Collaborator

@testower testower commented Jul 31, 2024

Related to entur/lamassu#250

This adds possibility to provide interceptor for subscription updates. Example implementation from application code:

public class LoggingSubscriptionUpdateInterceptor implements SubscriptionUpdateInterceptor {
    private final FeedProvider feedProvider;

    public LoggingSubscriptionUpdateInterceptor(
            FeedProvider feedProvider
    ) {
        this.feedProvider = feedProvider;
    }
    @Override
    public void beforeUpdate() {
        MDC.put("systemId", feedProvider.getSystemId());
    }

    @Override
    public void afterUpdate() {
        MDC.remove("systemId");
    }
}

Which in turn enables systemId to be logged from log statements within this library by configuring logback as this example shows:

            <appender name="console" class="ch.qos.logback.core.ConsoleAppender">
                <encoder class="net.logstash.logback.encoder.LoggingEventCompositeJsonEncoder">
                    <providers>
                        <pattern>
                            <pattern>
                                {
                                    "serviceContext": {
                                        "service": "lamassu"
                                    },
                                    "mdc": {
                                        "systemId": "%X{systemId}"
                                    },
                                    "message": "%message\n%ex{full}",
                                    "severity": "%level",
                                    "reportLocation": {
                                        "filePath": "%logger",
                                        "lineNumber": "%line",
                                        "functionName": "%method"
                                    }
                                }
                            </pattern>
                        </pattern>
                    </providers>
                </encoder>
            </appender>

@testower testower requested a review from hbruch July 31, 2024 11:41
@testower
Copy link
Collaborator Author

I wonder if there's some sort of best practice for adding stuff to MDC from libraries. Maybe namespacing?

@testower
Copy link
Collaborator Author

One way to improve this is to add some lifecycle hooks or interceptors to the subscription, so that the application can make the MDC calls in stead of the library code.

Copy link
Collaborator

@hbruch hbruch left a comment

Choose a reason for hiding this comment

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

Calling the updateInterceptor in the update method does not support the intended use case to have a the system_id logged in case of exceptions, as exception logging occurs after update() was called and hence the externally set MDC is cleared already.

Possible solution could be to add beforeUpdate/afterUpdate as lifecycle methods to the GBFSSubscription classes and let them be called by the GBFSSubscriptionManager before/after calling update, e.g. like this

public void update() {
    subscriptions
      .values()
      .parallelStream()
      .forEach(subscription ->
        Optional
          .ofNullable(customThreadPool)
          .orElse(ForkJoinPool.commonPool())
          .execute(() -> this.update(subscription))
      );
  }

  public void update(GbfsSubscription subscription) {
    try {
      subscription.beforeUpdate();
      subscription.update();
    } catch (RuntimeException e) {
      LOG.error("Error updating subscription", e);
    } finally {
      subscription.afterUpdate();
    }
  }

Copy link

sonarcloud bot commented Sep 9, 2024

@testower testower merged commit 7235e48 into master Sep 9, 2024
3 checks passed
@testower testower deleted the improve/mdc branch September 9, 2024 12:09
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.

2 participants