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

io.datarouter.trace.conveyor.TraceBuffers Maven / Gradle / Ivy

/*
 * Copyright © 2009 HotPads ([email protected])
 *
 * 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 io.datarouter.trace.conveyor;

import java.util.Optional;

import org.slf4j.Logger;
import org.slf4j.LoggerFactory;

import io.datarouter.instrumentation.trace.TraceBundleAndHttpRequestRecordDto;
import io.datarouter.instrumentation.trace.Traceparent;
import io.datarouter.storage.setting.Setting;
import io.datarouter.trace.filter.TraceFilter;
import io.datarouter.trace.settings.DatarouterTracePublisherSettingRoot;
import io.datarouter.util.buffer.MemoryBuffer;
import jakarta.inject.Inject;
import jakarta.inject.Singleton;

@Singleton
public class TraceBuffers{
	private static final Logger logger = LoggerFactory.getLogger(TraceBuffers.class);

	private static final int MAX_TRACES = 1_000;

	public final MemoryBuffer buffer;
	private final Setting shouldRunSetting;

	@Inject
	public TraceBuffers(DatarouterTracePublisherSettingRoot settings){
		this.buffer = new MemoryBuffer<>("traceBuffer", MAX_TRACES);
		this.shouldRunSetting = settings.saveTracesToMemory;
	}

	public Optional offer(TraceBundleAndHttpRequestRecordDto dto){
		Traceparent traceparent = dto.traceBundleDto.traceDto.traceparent;
		if(TraceFilter.isTraceIdTimestampOutsideCutoffTimes(traceparent)){
			return Optional.empty();
		}
		if(!shouldRunSetting.get()){
			return Optional.empty();
		}
		if(!buffer.offer(dto)){
			logger.warn("error offering trace entity buffer={} traceparent={}",
					buffer.getName(),
					dto.traceBundleDto.traceDto.traceparent);
			return Optional.empty();
		}
		return Optional.of(buffer.getName());
	}

}




© 2015 - 2025 Weber Informatics LLC | Privacy Policy