001/*-
002 * #%L
003 * HAPI FHIR JPA Server - Batch2 Task Processor
004 * %%
005 * Copyright (C) 2014 - 2024 Smile CDR, Inc.
006 * %%
007 * Licensed under the Apache License, Version 2.0 (the "License");
008 * you may not use this file except in compliance with the License.
009 * You may obtain a copy of the License at
010 *
011 *      http://www.apache.org/licenses/LICENSE-2.0
012 *
013 * Unless required by applicable law or agreed to in writing, software
014 * distributed under the License is distributed on an "AS IS" BASIS,
015 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
016 * See the License for the specific language governing permissions and
017 * limitations under the License.
018 * #L%
019 */
020package ca.uhn.fhir.batch2.util;
021
022import io.opentelemetry.api.common.AttributeKey;
023import io.opentelemetry.api.common.Attributes;
024import io.opentelemetry.api.common.AttributesBuilder;
025import io.opentelemetry.api.trace.Span;
026import jakarta.annotation.Nullable;
027
028public class BatchJobOpenTelemetryUtils {
029
030        public static final String JOB_STEP_EXECUTION_SPAN_NAME = "hapifhir.batch_job.execute";
031        private static final AttributeKey<String> OTEL_JOB_DEF_ID_ATT_KEY =
032                        AttributeKey.stringKey("hapifhir.batch_job.definition_id");
033        private static final AttributeKey<String> OTEL_JOB_DEF_VER_ATT_KEY =
034                        AttributeKey.stringKey("hapifhir.batch_job.definition_version");
035        private static final AttributeKey<String> OTEL_JOB_STEP_ID_ATT_KEY =
036                        AttributeKey.stringKey("hapifhir.batch_job.step_id");
037        private static final AttributeKey<String> OTEL_JOB_INSTANCE_ID_ATT_KEY =
038                        AttributeKey.stringKey("hapifhir.batch_job.instance_id");
039        private static final AttributeKey<String> OTEL_JOB_CHUNK_ID_ATT_KEY =
040                        AttributeKey.stringKey("hapifhir.batch_job.chunk_id");
041
042        private BatchJobOpenTelemetryUtils() {}
043
044        public static void addAttributesToCurrentSpan(
045                        String theJobDefinitionId,
046                        int theJobDefinitionVersion,
047                        String theJobInstanceId,
048                        String theStepId,
049                        @Nullable String theChunkId) {
050
051                Span currentSpan = Span.current();
052                AttributesBuilder attBuilder = Attributes.builder()
053                                .put(OTEL_JOB_DEF_ID_ATT_KEY, theJobDefinitionId)
054                                .put(OTEL_JOB_DEF_VER_ATT_KEY, Integer.toString(theJobDefinitionVersion))
055                                .put(OTEL_JOB_STEP_ID_ATT_KEY, theStepId)
056                                .put(OTEL_JOB_INSTANCE_ID_ATT_KEY, theJobInstanceId)
057                                // it is ok to put null values, builder ignores them
058                                .put(OTEL_JOB_CHUNK_ID_ATT_KEY, theChunkId);
059
060                currentSpan.setAllAttributes(attBuilder.build());
061        }
062}