Skip to content

Commit

Permalink
Merge pull request #28444 from geoand/otel-lambda
Browse files Browse the repository at this point in the history
Remove lambdas for OpenTelemetry runtime code
  • Loading branch information
geoand committed Oct 7, 2022
2 parents 28ae4fe + e35f87f commit 7d551fd
Show file tree
Hide file tree
Showing 4 changed files with 60 additions and 40 deletions.
Original file line number Diff line number Diff line change
@@ -1,14 +1,13 @@
package io.quarkus.opentelemetry.runtime;

import java.util.AbstractMap;
import java.util.Collections;
import java.util.HashMap;
import java.util.HashSet;
import java.util.LinkedHashMap;
import java.util.List;
import java.util.Map;
import java.util.ServiceLoader;
import java.util.Set;
import java.util.stream.Collectors;
import java.util.stream.StreamSupport;

import io.opentelemetry.api.baggage.propagation.W3CBaggagePropagator;
import io.opentelemetry.api.trace.Span;
Expand Down Expand Up @@ -49,18 +48,18 @@ private static TextMapPropagator getPropagator(
}

public static ContextPropagators mapPropagators(List<String> propagators) {
Map<String, TextMapPropagator> spiPropagators = StreamSupport.stream(
ServiceLoader.load(ConfigurablePropagatorProvider.class).spliterator(), false)
.collect(
Collectors.toMap(ConfigurablePropagatorProvider::getName,
// Even though this param was added, propagators currently don't use any config properties
// when the time arrives, and they do use it, we will need to implement a Quarkus
// backed `ConfigProperties` class
o -> o.getPropagator(null)));
Map<String, TextMapPropagator> spiPropagators = new HashMap<>();
for (var provider : ServiceLoader.load(ConfigurablePropagatorProvider.class)) {
// Even though this param was added, propagators currently don't use any config properties
// when the time arrives, and they do use it, we will need to implement a Quarkus
// backed `ConfigProperties` class
spiPropagators.put(provider.getName(), provider.getPropagator(null));
}

Set<TextMapPropagator> selectedPropagators = propagators.stream()
.map(propagator -> getPropagator(propagator.trim(), spiPropagators))
.collect(Collectors.toSet());
Set<TextMapPropagator> selectedPropagators = new HashSet<>(propagators.size());
for (String propagator : propagators) {
selectedPropagators.add(getPropagator(propagator.trim(), spiPropagators));
}

return ContextPropagators.create(TextMapPropagator.composite(selectedPropagators));
}
Expand All @@ -78,11 +77,18 @@ public static Map<String, String> convertKeyValueListToMap(List<String> headers)
return Collections.emptyMap();
}

return headers.stream()
.filter(header -> !header.isEmpty())
.map(keyValuePair -> keyValuePair.split("=", 2))
.map(keyValuePair -> new AbstractMap.SimpleImmutableEntry<>(keyValuePair[0].trim(), keyValuePair[1].trim()))
.collect(Collectors.toMap(Map.Entry::getKey, Map.Entry::getValue, (first, next) -> next, LinkedHashMap::new));
Map<String, String> result = new LinkedHashMap<>();
for (String header : headers) {
if (header.isEmpty()) {
continue;
}
String[] parts = header.split("=", 2);
String key = parts[0].trim();
String value = parts[1].trim();
result.put(key, value);
}

return result;
}

/**
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -67,17 +67,21 @@ public Scope attach(io.vertx.core.Context vertxContext, Context toAttach) {
vertxContext.putLocal(OTEL_CONTEXT, toAttach);
OpenTelemetryUtil.setMDCData(toAttach, vertxContext);

return () -> {
if (getContext(vertxContext) != toAttach) {
log.warn("Context in storage not the expected context, Scope.close was not called correctly");
}

if (beforeAttach == null) {
vertxContext.removeLocal(OTEL_CONTEXT);
OpenTelemetryUtil.clearMDCData(vertxContext);
} else {
vertxContext.putLocal(OTEL_CONTEXT, beforeAttach);
OpenTelemetryUtil.setMDCData(beforeAttach, vertxContext);
return new Scope() {

@Override
public void close() {
if (getContext(vertxContext) != toAttach) {
log.warn("Context in storage not the expected context, Scope.close was not called correctly");
}

if (beforeAttach == null) {
vertxContext.removeLocal(OTEL_CONTEXT);
OpenTelemetryUtil.clearMDCData(vertxContext);
} else {
vertxContext.putLocal(OTEL_CONTEXT, beforeAttach);
OpenTelemetryUtil.setMDCData(beforeAttach, vertxContext);
}
}
};
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -115,11 +115,13 @@ public void setupResources(TracerRuntimeConfig config) {
.select(Resource.class, Any.Literal.INSTANCE);

// Merge resource instances with env attributes
Resource resource = allResources.stream()
.reduce(Resource.empty(), Resource::merge)
.merge(config.resourceAttributes
.map(TracerUtil::mapResourceAttributes)
.orElseGet(Resource::empty));
Resource resource = Resource.empty();
for (Resource r : allResources) {
resource = resource.merge(r);
}
if (config.resourceAttributes.isPresent()) {
resource = resource.merge(TracerUtil.mapResourceAttributes(config.resourceAttributes.get()));
}

// Update Delayed attributes to contain new runtime attributes if necessary
if (resource.getAttributes().size() > 0) {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@
import java.util.Optional;

import javax.enterprise.inject.Any;
import javax.enterprise.inject.Instance;
import javax.enterprise.inject.spi.CDI;

import io.opentelemetry.api.common.Attributes;
Expand Down Expand Up @@ -40,11 +41,18 @@ private static Sampler getBaseSampler(String samplerName, Optional<Double> ratio

public static Sampler mapSampler(TracerRuntimeConfig.SamplerConfig samplerConfig,
List<String> dropTargets) {
Sampler sampler = CDI.current()
.select(Sampler.class, Any.Literal.INSTANCE)
.stream()
.filter(o -> !(o instanceof LateBoundSampler))
.findFirst().orElseGet(() -> getBaseSampler(samplerConfig.samplerName, samplerConfig.ratio));
Sampler sampler = null;
Instance<Sampler> samplerInstance = CDI.current().select(Sampler.class, Any.Literal.INSTANCE);
for (Sampler s : samplerInstance) {
if (s instanceof LateBoundSampler) {
continue;
}
sampler = s;
break;
}
if (sampler == null) {
sampler = getBaseSampler(samplerConfig.samplerName, samplerConfig.ratio);
}

if (!dropTargets.isEmpty()) {
sampler = new DropTargetsSampler(sampler, dropTargets);
Expand Down

0 comments on commit 7d551fd

Please sign in to comment.