<commit_before>/**
 * JBoss, Home of Professional Open Source.
 * Copyright 2014-2022 Red Hat, Inc., and individual contributors
 * as indicated by the @author tags.
 *
 * Licensed under the Apache License, Version 2.0 (the "License");
 * you may not use this file except in compliance with the License.
 * You may obtain a copy of the License at
 *
 * http://www.apache.org/licenses/LICENSE-2.0
 *
 * Unless required by applicable law or agreed to in writing, software
 * distributed under the License is distributed on an "AS IS" BASIS,
 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
 * See the License for the specific language governing permissions and
 * limitations under the License.
 */
package org.jboss.pnc.common.logging;

import org.jboss.pnc.api.constants.MDCKeys;
import org.jboss.pnc.common.otel.OtelUtils;
import org.jboss.pnc.common.util.StringUtils;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.slf4j.MDC;

import io.opentelemetry.api.trace.SpanContext;

import java.time.Instant;
import java.util.Objects;
import java.util.Optional;
import java.util.stream.Collectors;

import javax.ws.rs.container.ContainerRequestContext;

/**
 * @author <a href="mailto:<EMAIL>"><NAME></a>
 * @deprecated use MDCUtils from pnc-common
 */
public class MDCUtils extends org.jboss.pnc.common.log.MDCUtils {

    private static final Logger logger = LoggerFactory.getLogger(MDCUtils.class);

    public static void addBuildContext(BuildTaskContext buildTaskContext) {
        addBuildContext(
                buildTaskContext.getBuildContentId(),
                buildTaskContext.isTemporaryBuild(),
                buildTaskContext.getTemporaryBuildExpireDate(),
                buildTaskContext.getUserId());
    }

    public static void addBuildContext(
            String processContext,
            Boolean temporaryBuild,
            Instant temporaryBuildExpireDate,
            String userId) {
        addProcessContext(processContext);
        MDC.put(MDCKeys.USER_ID_KEY, userId);
        MDC.put(MDCKeys.TMP_KEY, temporaryBuild.toString());
        MDC.put(MDCKeys.EXP_KEY, temporaryBuildExpireDate.toString());
    }

    public static void removeBuildContext() {
        removeProcessContext();
        MDC.remove(MDCKeys.USER_ID_KEY);
        MDC.remove(MDCKeys.TMP_KEY);
        MDC.remove(MDCKeys.EXP_KEY);
    }

    public static void addProcessContext(String processContext) {
        String current = MDC.get(MDCKeys.PROCESS_CONTEXT_KEY);
        if (StringUtils.isEmpty(current)) {
            MDC.put(MDCKeys.PROCESS_CONTEXT_KEY, processContext);
        } else {
            logger.warn("Did not set new processContext [{}] as value already exists [{}].", processContext, current);
        }
    }

    public static void addUserId(String userId) {
        MDC.put(MDCKeys.USER_ID_KEY, userId);
    }

    public static void addTraceContext(ContainerRequestContext requestContext, SpanContext currentSpanContext) {

        SpanContext extractedSpanContext = OtelUtils.extractSpanContextFromHeaders(requestContext);

        if (extractedSpanContext != null && extractedSpanContext.isValid()) {
            MDC.put(MDCKeys.SLF4J_TRACE_ID_KEY, extractedSpanContext.getTraceId());
            MDC.put(MDCKeys.SLF4J_SPAN_ID_KEY, extractedSpanContext.getSpanId());
            MDC.put(MDCKeys.SLF4J_TRACE_FLAGS_KEY, extractedSpanContext.getTraceFlags().asHex());
            MDC.put(
                    MDCKeys.SLF4J_TRACE_STATE_KEY,
                    extractedSpanContext.getTraceState()
                            .asMap()
                            .entrySet()
                            .stream()
                            .map(Objects::toString)
                            .collect(Collectors.joining(",")));
        } else {
            MDC.put(MDCKeys.SLF4J_TRACE_ID_KEY, currentSpanContext.getTraceId());
            MDC.put(MDCKeys.SLF4J_SPAN_ID_KEY, currentSpanContext.getSpanId());
            MDC.put(MDCKeys.SLF4J_TRACE_FLAGS_KEY, currentSpanContext.getTraceFlags().asHex());
            MDC.put(
                    MDCKeys.SLF4J_TRACE_STATE_KEY,
                    currentSpanContext.getTraceState()
                            .asMap()
                            .entrySet()
                            .stream()
                            .map(Objects::toString)
                            .collect(Collectors.joining(",")));
        }

        logger.debug(
                "Trace ID: {}, Span ID: {}, TraceFlags ID: {}, TraceState: {}",
                MDC.get(MDCKeys.SLF4J_TRACE_ID_KEY),
                MDC.get(MDCKeys.SLF4J_SPAN_ID_KEY),
                MDC.get(MDCKeys.SLF4J_TRACE_FLAGS_KEY),
                MDC.get(MDCKeys.SLF4J_TRACE_STATE_KEY));
    }

    public static Optional<String> getRequestContext() {
        return Optional.ofNullable(MDC.get(MDCKeys.REQUEST_CONTEXT_KEY));
    }

    public static Optional<String> getProcessContext() {
        return Optional.ofNullable(MDC.get(MDCKeys.PROCESS_CONTEXT_KEY));
    }

    public static Optional<String> getUserId() {
        return Optional.ofNullable(MDC.get(MDCKeys.USER_ID_KEY));
    }

    public static Optional<String> getTraceId() {
        return Optional.ofNullable(MDC.get(MDCKeys.SLF4J_TRACE_ID_KEY));
    }

    public static Optional<String> getSpanId() {
        return Optional.ofNullable(MDC.get(MDCKeys.SLF4J_SPAN_ID_KEY));
    }

    public static Optional<String> getTraceFlag() {
        return Optional.ofNullable(MDC.get(MDCKeys.SLF4J_TRACE_FLAGS_KEY));
    }

    public static Optional<String> getTraceState() {
        return Optional.ofNullable(MDC.get(MDCKeys.SLF4J_TRACE_STATE_KEY));
    }

    public static void removeProcessContext() {<commit_msg>Remove unused method<commit_after>/**
 * JBoss, Home of Professional Open Source.
 * Copyright 2014-2022 Red Hat, Inc., and individual contributors
 * as indicated by the @author tags.
 *
 * Licensed under the Apache License, Version 2.0 (the "License");
 * you may not use this file except in compliance with the License.
 * You may obtain a copy of the License at
 *
 * http://www.apache.org/licenses/LICENSE-2.0
 *
 * Unless required by applicable law or agreed to in writing, software
 * distributed under the License is distributed on an "AS IS" BASIS,
 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
 * See the License for the specific language governing permissions and
 * limitations under the License.
 */
package org.jboss.pnc.common.logging;

import org.jboss.pnc.api.constants.MDCKeys;
import org.jboss.pnc.common.util.StringUtils;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.slf4j.MDC;

import java.time.Instant;
import java.util.Optional;

/**
 * @author <a href="mailto:<EMAIL>"><NAME></a>
 * @deprecated use MDCUtils from pnc-common
 */
public class MDCUtils extends org.jboss.pnc.common.log.MDCUtils {

    private static final Logger logger = LoggerFactory.getLogger(MDCUtils.class);

    public static void addBuildContext(BuildTaskContext buildTaskContext) {
        addBuildContext(
                buildTaskContext.getBuildContentId(),
                buildTaskContext.isTemporaryBuild(),
                buildTaskContext.getTemporaryBuildExpireDate(),
                buildTaskContext.getUserId());
    }

    public static void addBuildContext(
            String processContext,
            Boolean temporaryBuild,
            Instant temporaryBuildExpireDate,
            String userId) {
        addProcessContext(processContext);
        MDC.put(MDCKeys.USER_ID_KEY, userId);
        MDC.put(MDCKeys.TMP_KEY, temporaryBuild.toString());
        MDC.put(MDCKeys.EXP_KEY, temporaryBuildExpireDate.toString());
    }

    public static void removeBuildContext() {
        removeProcessContext();
        MDC.remove(MDCKeys.USER_ID_KEY);
        MDC.remove(MDCKeys.TMP_KEY);
        MDC.remove(MDCKeys.EXP_KEY);
    }

    public static void addProcessContext(String processContext) {
        String current = MDC.get(MDCKeys.PROCESS_CONTEXT_KEY);
        if (StringUtils.isEmpty(current)) {
            MDC.put(MDCKeys.PROCESS_CONTEXT_KEY, processContext);
        } else {
            logger.warn("Did not set new processContext [{}] as value already exists [{}].", processContext, current);
        }
    }

    public static void addUserId(String userId) {
        MDC.put(MDCKeys.USER_ID_KEY, userId);
    }

    public static Optional<String> getRequestContext() {
        return Optional.ofNullable(MDC.get(MDCKeys.REQUEST_CONTEXT_KEY));
    }

    public static Optional<String> getProcessContext() {
        return Optional.ofNullable(MDC.get(MDCKeys.PROCESS_CONTEXT_KEY));
    }

    public static Optional<String> getUserId() {
        return Optional.ofNullable(MDC.get(MDCKeys.USER_ID_KEY));
    }

    public static Optional<String> getTraceId() {
        return Optional.ofNullable(MDC.get(MDCKeys.SLF4J_TRACE_ID_KEY));
    }

    public static Optional<String> getSpanId() {
        return Optional.ofNullable(MDC.get(MDCKeys.SLF4J_SPAN_ID_KEY));
    }

    public static Optional<String> getTraceFlag() {
        return Optional.ofNullable(MDC.get(MDCKeys.SLF4J_TRACE_FLAGS_KEY));
    }

    public static Optional<String> getTraceState() {
        return Optional.ofNullable(MDC.get(MDCKeys.SLF4J_TRACE_STATE_KEY));
    }

    public static void removeProcessContext() {
{'fraction_non_alphanumeric': 0.09736674453174772, 'fraction_numerical': 0.005308982798895732, 'mean_word_length': 2.876131687242798, 'pattern_counts': {'":': 0, '<': 25, '<?xml version=': 0, '>': 25, 'https://': 0, 'lorem ipsum': 0, 'www.': 2, 'xml': 0}, 'pii_count': 0, 'substrings_counts': 0, 'word_list_counts': {'cursed_substrings.json': 0, 'profanity_word_list.json': 0, 'sexual_word_list.json': 0, 'zh_pornsignals.json': 0}}
{'dir': 'git-commits-cleaned', 'id': '827693', 'n_tokens_mistral': 2831, 'n_tokens_neox': 2790, 'n_words': 563}