private static StringMap memento(final ReadOnlyStringMap data) { final StringMap result = ContextDataFactory.createContextData(); result.putAll(data); return result; }
static StringMap injectContextData(@Nullable List<Property> properties, StringMap reusable) { if (properties == null || properties.isEmpty()) { return getContextAndTracingData(); } // Context data has precedence over configuration properties. putProperties(properties, reusable); // TODO(sebright): The following line can be optimized. See // https://github.com/census-instrumentation/opencensus-java/pull/1422/files#r216425494. reusable.putAll(getContextAndTracingData()); return reusable; }
/** * Puts key-value pairs from both the specified list of properties as well as the thread context into the * specified reusable StringMap. * * @param props list of configuration properties, may be {@code null} * @param reusable a {@code StringMap} instance that may be reused to avoid creating temporary objects * @return a {@code StringMap} combining configuration properties with thread context data */ @Override public StringMap injectContextData(final List<Property> props, final StringMap reusable) { // When the ThreadContext is garbage-free, we must copy its key-value pairs into the specified reusable // StringMap. We cannot return the ThreadContext's internal data structure because it may be modified later // and such modifications should not be reflected in the log event. copyProperties(props, reusable); final ReadOnlyStringMap immutableCopy = ThreadContext.getThreadContextMap().getReadOnlyContextData(); reusable.putAll(immutableCopy); return reusable; }
/** * If there are no configuration properties, this injector will return the thread context's internal data * structure. Otherwise the configuration properties are combined with the thread context key-value pairs into the * specified reusable StringMap. * * @param props list of configuration properties, may be {@code null} * @param ignore a {@code StringMap} instance from the log event * @return a {@code StringMap} combining configuration properties with thread context data */ @Override public StringMap injectContextData(final List<Property> props, final StringMap ignore) { // If there are no configuration properties we want to just return the ThreadContext's StringMap: // it is a copy-on-write data structure so we are sure ThreadContext changes will not affect our copy. final StringMap immutableCopy = ThreadContext.getThreadContextMap().getReadOnlyContextData(); if (props == null || props.isEmpty()) { return immutableCopy; // this will replace the LogEvent's context data with the returned instance } // However, if the list of Properties is non-empty we need to combine the properties and the ThreadContext // data. Note that we cannot reuse the specified StringMap: some Loggers may have properties defined // and others not, so the LogEvent's context data may have been replaced with an immutable copy from // the ThreadContext - this will throw an UnsupportedOperationException if we try to modify it. final StringMap result = ContextDataFactory.createContextData(props.size() + immutableCopy.size()); copyProperties(props, result); result.putAll(immutableCopy); return result; }
if (contextData.isFrozen()) { final StringMap temp = ContextDataFactory.createContextData(); temp.putAll(contextData); contextData = temp;
this.contextData.clear(); this.contextData.putAll(other.getContextData());
this.contextData.putAll(event.getContextData());