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

Using MDC for labels #71

Open
stefnats opened this issue Aug 20, 2021 · 5 comments
Open

Using MDC for labels #71

stefnats opened this issue Aug 20, 2021 · 5 comments

Comments

@stefnats
Copy link

Hey there,

i want to migrate from ELK Stack to Loki.
Is it possible to send the MDC Parameters (set dynamically in ThreadContext.put(key, value)) as Labels to Loki? And how would i achieve this with log4j2?

Thanks in advance

@tkowalcz
Copy link
Owner

tkowalcz commented Aug 20, 2021

Hello,

yes, you can do that. In log4j you have variable ctx that can be used in Label definition, you can use mdc or X:

  <appenders>
        <Loki name="Loki">
...
            <Label name="tid1" pattern="%X{tid}"/>
            <Label name="tid2" value="${ctx:tid}"/>
            <Label name="tid3" pattern="%mdc{tid}"/>
       </Loki>
    </appenders>

The ${ctx} syntax also allows for specifying default values if context is empty:

            <Label name="default_value_test" value="${ctx:missing:-use_this_if_missing}"/>

You might find out this documentation helpful:

@tkowalcz
Copy link
Owner

tkowalcz commented Sep 5, 2021

@stefnats Please let me know or reopen the issue if you need more info.

@tkowalcz tkowalcz closed this as completed Sep 5, 2021
@lucianocallero
Copy link

Is there any way to do the same but without knowing previously the name of the MDC vars? I have dynamic context variables depending on the code and i don't want to re-write my xml every time i add a new one.

By instance, i have 2 methods with 3 different value, can i do something at XML to show 3 without telling the name up front?

// Method 1..
ThreadContext.put("foo1", "value1");
ThreadContext.put("foo2", "value2");

// Method 2
ThreadContext.put("foo1", "value1");
ThreadContext.put("foo3", "OtherValue3");

@tkowalcz
Copy link
Owner

Is there any way to do the same but without knowing previously the name of the MDC vars? I have dynamic context variables depending on the code and i don't want to re-write my xml every time i add a new one.

By instance, i have 2 methods with 3 different value, can i do something at XML to show 3 without telling the name up front?

// Method 1.. ThreadContext.put("foo1", "value1"); ThreadContext.put("foo2", "value2");

// Method 2 ThreadContext.put("foo1", "value1"); ThreadContext.put("foo3", "OtherValue3");

@lucianocallero I don't think it is possible. Is it possible in other appenders?

I guess we could add a switch that will convert the whole thread context into loki labels.

@tkowalcz tkowalcz reopened this Jul 18, 2024
@lucianocallero
Copy link

Thanks. That would be great. In me case more than one appender does not fit because labels are dynamic depending on the context of the appliances.

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

No branches or pull requests

3 participants