Skip to content
This repository has been archived by the owner on Nov 14, 2023. It is now read-only.

Commit

Permalink
Merge pull request #118 from razorpay/OBSV-1191-ht-onboarding
Browse files Browse the repository at this point in the history
Add deployment attribute in resource enricher
  • Loading branch information
sakshi-goyal-razorpay authored Dec 22, 2022
2 parents 562c937 + 791ef83 commit 3f21d29
Show file tree
Hide file tree
Showing 5 changed files with 67 additions and 11 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -95,3 +95,10 @@ enum ErrorMetrics {
ERROR_METRICS_TOTAL_SPANS_WITH_ERRORS = 3 [(string_value) = "span_count_with_errors"];
ERROR_METRICS_TOTAL_SPANS_WITH_EXCEPTIONS = 4 [(string_value) = "span_count_with_exceptions"];
}

enum Deployment {
DEPLOYMENT_CANARY = 0 [(string_value) = "canary"];
DEPLOYMENT_BASELINE = 1 [(string_value) = "baseline"];
DEPLOYMENT_WORKER = 2 [(string_value) = "worker"];
DEPLOYMENT_WEB = 3 [(string_value) = "web"];
}
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,9 @@
import org.hypertrace.core.datamodel.AttributeValue;
import org.hypertrace.core.datamodel.Event;
import org.hypertrace.core.datamodel.StructuredTrace;
import org.hypertrace.core.datamodel.shared.trace.AttributeValueCreator;
import org.hypertrace.traceenricher.enrichedspan.constants.EnrichedSpanConstants;
import org.hypertrace.traceenricher.enrichedspan.constants.v1.Deployment;
import org.hypertrace.traceenricher.enrichment.AbstractTraceEnricher;
import org.hypertrace.traceenricher.enrichment.clients.ClientRegistry;
import org.slf4j.Logger;
Expand All @@ -23,6 +26,9 @@ public class ResourceAttributeEnricher extends AbstractTraceEnricher {
private static final Logger LOGGER = LoggerFactory.getLogger(ResourceAttributeEnricher.class);
private static final String RESOURCE_ATTRIBUTES_CONFIG_KEY = "attributes";
private static final String NODE_SELECTOR_KEY = "node.selector";

private static final String DEPLOYMENT_TYPE_KEY = "deployment.type";

private static final String ATTRIBUTES_TO_MATCH_CONFIG_KEY = "attributesToMatch";
private List<String> resourceAttributesToAdd = new ArrayList<>();
private Map<String, String> resourceAttributeKeysToMatch = new HashMap<>();
Expand Down Expand Up @@ -55,13 +61,18 @@ public void enrichEvent(StructuredTrace trace, Event event) {
attributeMap.computeIfAbsent(
resourceAttributeKey,
key -> {
if (resourceAttributeKey.equals(NODE_SELECTOR_KEY)) {
attributeValue.setValue(
attributeValue
.getValue()
.substring(attributeValue.getValue().lastIndexOf('/') + 1));
switch (resourceAttributeKey) {
case DEPLOYMENT_TYPE_KEY:
return AttributeValueCreator.create(
getDeploymentType(attributeValue.getValue()));
case NODE_SELECTOR_KEY:
attributeValue.setValue(
attributeValue
.getValue()
.substring(attributeValue.getValue().lastIndexOf('/') + 1));
default:
return attributeValue;
}
return attributeValue;
}));
}
} catch (Exception e) {
Expand All @@ -77,4 +88,19 @@ private boolean isValidEvent(Event event) {
&& (event.getAttributes() != null)
&& (event.getAttributes().getAttributeMap() != null);
}

private String getDeploymentType(String hostName) {
/*
There can be applications which have canary/baseline workers. (eg: worker-canary, worker-baseline)
These rare cases are not handled for now.
*/
for (Deployment d : Deployment.values()) {
if (d == Deployment.UNRECOGNIZED) {
break;
} else if (hostName.contains(EnrichedSpanConstants.getValue(d))) {
return EnrichedSpanConstants.getValue(d);
}
}
return EnrichedSpanConstants.getValue(Deployment.DEPLOYMENT_WEB);
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -83,7 +83,10 @@ public void traceWithResource() {
assertEquals(
resourceAttributesToAddList.size() - 2, event.getAttributes().getAttributeMap().size());
assertEquals(
"test-56f5d554c-5swkj", event.getAttributes().getAttributeMap().get("pod.name").getValue());
"test-canary-56f5d554c-5swkj",
event.getAttributes().getAttributeMap().get("pod.name").getValue());
assertEquals(
"canary", event.getAttributes().getAttributeMap().get("deployment.type").getValue());
assertEquals(
"01188498a468b5fef1eb4accd63533297c195a73",
event.getAttributes().getAttributeMap().get("service.version").getValue());
Expand Down Expand Up @@ -111,6 +114,11 @@ public void traceWithResource() {
"worker-generic", event2.getAttributes().getAttributeMap().get("node.name").getValue());
assertEquals(
"worker-generic", event2.getAttributes().getAttributeMap().get("node.selector").getValue());
assertEquals(
"test1-baseline-56f5d554c-5swkj",
event2.getAttributes().getAttributeMap().get("pod.name").getValue());
assertEquals(
"baseline", event2.getAttributes().getAttributeMap().get("deployment.type").getValue());

Event event3 =
Event.newBuilder()
Expand All @@ -121,6 +129,11 @@ public void traceWithResource() {
event3.setResourceIndex(2);
resourceAttributeEnricher.enrichEvent(structuredTrace, event3);
assertEquals("", event3.getAttributes().getAttributeMap().get("node.selector").getValue());
assertEquals(
"test1-worker-56f5d554c-5swkj",
event3.getAttributes().getAttributeMap().get("pod.name").getValue());
assertEquals(
"worker", event3.getAttributes().getAttributeMap().get("deployment.type").getValue());

Event event4 =
Event.newBuilder()
Expand All @@ -133,6 +146,7 @@ public void traceWithResource() {
assertEquals(
"worker-generic", event4.getAttributes().getAttributeMap().get("node.selector").getValue());
assertEquals("pod1", event4.getAttributes().getAttributeMap().get("pod.name").getValue());
assertEquals("web", event4.getAttributes().getAttributeMap().get("deployment.type").getValue());
}

private Resource getResource4() {
Expand All @@ -157,6 +171,9 @@ private Resource getResource3() {
AttributeValue.newBuilder()
.setValue("node-role.kubernetes.io/worker-generic/")
.build());
put(
"host.name",
AttributeValue.newBuilder().setValue("test1-worker-56f5d554c-5swkj").build());
}
};
return Resource.newBuilder()
Expand All @@ -177,7 +194,9 @@ private Resource getResource2() {
put(
"opencensus.exporterversion",
AttributeValue.newBuilder().setValue("Jaeger-Go-2.23.1").build());
put("host.name", AttributeValue.newBuilder().setValue("test1-56f5d554c-5swkj").build());
put(
"host.name",
AttributeValue.newBuilder().setValue("test1-baseline-56f5d554c-5swkj").build());
put("ip", AttributeValue.newBuilder().setValue("10.21.18.1712").build());
put("client-uuid", AttributeValue.newBuilder().setValue("53a112a715bdf86").build());
put("node.name", AttributeValue.newBuilder().setValue("worker-generic").build());
Expand Down Expand Up @@ -210,7 +229,9 @@ private Resource getResource1() {
put(
"opencensus.exporterversion",
AttributeValue.newBuilder().setValue("Jaeger-Go-2.23.1").build());
put("host.name", AttributeValue.newBuilder().setValue("test-56f5d554c-5swkj").build());
put(
"host.name",
AttributeValue.newBuilder().setValue("test-canary-56f5d554c-5swkj").build());
put("ip", AttributeValue.newBuilder().setValue("10.21.18.171").build());
put("client-uuid", AttributeValue.newBuilder().setValue("53a112a715bda986").build());
put(
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -69,9 +69,10 @@ enricher {

ResourceAttributeEnricher {
class = "org.hypertrace.traceenricher.enrichment.enrichers.ResourceAttributeEnricher"
attributes = ["pod.name","node.name","cluster.name","ip","service.version","node.selector"]
attributes = ["pod.name","node.name","cluster.name","ip","service.version","node.selector","deployment.type"]
attributesToMatch {
pod.name = "host.name"
deployment.type = "host.name"
}
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -123,9 +123,10 @@ enricher {

ResourceAttributeEnricher {
class = "org.hypertrace.traceenricher.enrichment.enrichers.ResourceAttributeEnricher"
attributes = ["pod.name","node.name","cluster.name","ip","service.version","node.selector"]
attributes = ["pod.name","node.name","cluster.name","ip","service.version","node.selector","deployment.type"]
attributesToMatch {
pod.name = "host.name"
deployment.type = "host.name"
}
}

Expand Down

0 comments on commit 3f21d29

Please sign in to comment.