All Downloads are FREE. Search and download functionalities are using the official Maven repository.

io.honeybadger.reporter.dto.Details Maven / Gradle / Ivy

There is a newer version: 2.1.2
Show newest version
package io.honeybadger.reporter.dto;

import com.fasterxml.jackson.annotation.JacksonInject;
import com.fasterxml.jackson.annotation.JsonCreator;
import com.fasterxml.jackson.annotation.JsonInclude;
import io.honeybadger.reporter.config.ConfigContext;
import org.slf4j.MDC;

import java.io.Serializable;
import java.util.LinkedHashMap;
import java.util.Map;
import java.util.Objects;
import java.util.Set;
import java.util.TreeMap;

/**
 * Class representing metadata and run-time state.
 * @author Elijah Zupancic
 * @since 1.0.9
 */
@JsonInclude(JsonInclude.Include.NON_NULL)
public class Details extends LinkedHashMap>
        implements Serializable {
    private static final long serialVersionUID = -6238693264237448645L;

    private final ConfigContext config;

    @JsonCreator
    public Details(@JacksonInject("config") final ConfigContext config) {
        this.config = config;
    }

    void addDefaultDetails() {
        put("System Properties", systemProperties());
        put("MDC Properties", mdcProperties());
    }

    protected static LinkedHashMap mdcProperties() {
        LinkedHashMap map = new LinkedHashMap<>();

        @SuppressWarnings("unchecked")
        Map mdc = MDC.getCopyOfContextMap();

        if (mdc != null) {
            for (Map.Entry entry : mdc.entrySet()) {
                map.put(entry.getKey(), entry.getValue());
            }
        }

        return map;
    }

    protected Map systemProperties() {
        TreeMap map = new TreeMap<>();
        Set excludedSysProps = config.getExcludedSysProps();

        for (Map.Entry entry: System.getProperties().entrySet()) {
            final Object key = entry.getKey();

            if (key == null) {
                continue;
            }

            final String stringKey = Objects.toString(key);

            if (stringKey.isEmpty()) {
                continue;
            }

            // We skip all excluded properties
            if (excludedSysProps.contains(stringKey)) {
                continue;
            }

            map.put(stringKey, entry.getValue().toString());
        }

        return map;
    }
}




© 2015 - 2024 Weber Informatics LLC | Privacy Policy