Skip to content

Commit

Permalink
Change OTEL user attribute (#3321)
Browse files Browse the repository at this point in the history
* Change OTEL user attribute

standardise other attributes

* address comments
  • Loading branch information
p29876 authored Oct 18, 2024
1 parent 2b89f94 commit 9bf6f1b
Show file tree
Hide file tree
Showing 5 changed files with 28 additions and 8 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -21,6 +21,12 @@

public final class OtelUtil {

public static final String USER_ATTRIBUTE = "enduser.id";
public static final String JOB_ID_ATTRIBUTE = "gaffer.jobId";
public static final String GRAPH_ID_ATTRIBUTE = "gaffer.graphId";
public static final String VIEW_ATTRIBUTE = "gaffer.view";
public static final String GREMLIN_QUERY_ATTRIBUTE = "gaffer.gremlin.query";

private static boolean openTelemetryActive = false;

private OtelUtil() {
Expand Down
6 changes: 3 additions & 3 deletions core/graph/src/main/java/uk/gov/gchq/gaffer/graph/Graph.java
Original file line number Diff line number Diff line change
Expand Up @@ -321,9 +321,9 @@ private <O> GraphResult<O> _execute(final StoreExecuter<O> storeExecuter, final
Span span = OtelUtil.startSpan(
this.getClass().getName(),
"Graph Request: " + clonedOpChain.toOverviewString());
span.setAttribute("gaffer.graphId", getGraphId());
span.setAttribute("gaffer.jobId", clonedContext.getJobId());
span.setAttribute("gaffer.user", clonedContext.getUser().getUserId());
span.setAttribute(OtelUtil.GRAPH_ID_ATTRIBUTE, getGraphId());
span.setAttribute(OtelUtil.JOB_ID_ATTRIBUTE, clonedContext.getJobId());
span.setAttribute(OtelUtil.USER_ATTRIBUTE, clonedContext.getUser().getUserId());

O result = null;
// Sets the span to current so parent child spans are auto linked
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -52,9 +52,9 @@ public OUT doOperation(final OperationChain<OUT> operationChain, final Context c
for (final Operation op : preparedOperationChain.getOperations()) {
// OpenTelemetry hooks
Span span = OtelUtil.startSpan(this.getClass().getName(), op.getClass().getName());
span.setAttribute("jobId", context.getJobId());
span.setAttribute(OtelUtil.JOB_ID_ATTRIBUTE, context.getJobId());
if (op instanceof OperationView && ((OperationView) op).getView() != null) {
span.setAttribute("view", ((OperationView) op).getView().toString());
span.setAttribute(OtelUtil.VIEW_ATTRIBUTE, ((OperationView) op).getView().toString());
}

// Sets the span to current so parent child spans are auto linked
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -57,6 +57,7 @@
import uk.gov.gchq.gaffer.rest.factory.spring.AbstractUserFactory;
import uk.gov.gchq.gaffer.tinkerpop.GafferPopGraph;
import uk.gov.gchq.gaffer.tinkerpop.GafferPopGraphVariables;
import uk.gov.gchq.gaffer.user.User;
import uk.gov.gchq.koryphe.tuple.n.Tuple2;

import java.io.IOException;
Expand Down Expand Up @@ -309,7 +310,17 @@ private Tuple2<Object, JSONObject> runGremlinQuery(final String gremlinQuery) {
// OpenTelemetry hooks
Span span = OtelUtil.startSpan(
this.getClass().getName(), "Gremlin Request: " + UUID.nameUUIDFromBytes(gremlinQuery.getBytes(StandardCharsets.UTF_8)));
span.setAttribute("gaffer.gremlin.query", gremlinQuery);
span.setAttribute(OtelUtil.GREMLIN_QUERY_ATTRIBUTE, gremlinQuery);

User user = ((GafferPopGraphVariables) gafferPopGraph.variables()).getUser();
String userId;
if (user != null) {
userId = user.getUserId();
} else {
LOGGER.warn("Could not find Gaffer user for OTEL. Using default.");
userId = "unknownGremlinUser";
}
span.setAttribute(OtelUtil.USER_ATTRIBUTE, userId);

// tuple to hold the result and explain
Tuple2<Object, JSONObject> pair = new Tuple2<>();
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -52,6 +52,7 @@
import uk.gov.gchq.gaffer.rest.factory.spring.AbstractUserFactory;
import uk.gov.gchq.gaffer.tinkerpop.GafferPopGraph;
import uk.gov.gchq.gaffer.tinkerpop.GafferPopGraphVariables;
import uk.gov.gchq.gaffer.user.User;

import java.io.IOException;
import java.nio.ByteBuffer;
Expand Down Expand Up @@ -145,14 +146,16 @@ private ResponseMessage handleGremlinRequest(final WebSocketSession session, fin

// OpenTelemetry hooks
Span span = OtelUtil.startSpan(this.getClass().getName(), "Gremlin Request: " + requestId.toString());
span.setAttribute("gaffer.gremlin.query", request.getArgs().get(Tokens.ARGS_GREMLIN).toString());
span.setAttribute(OtelUtil.GREMLIN_QUERY_ATTRIBUTE, request.getArgs().get(Tokens.ARGS_GREMLIN).toString());

// Execute the query
try (Scope scope = span.makeCurrent();
GremlinExecutor gremlinExecutor = getGremlinExecutor()) {
// Set current headers for potential authorisation then set the user
userFactory.setHttpHeaders(session.getHandshakeHeaders());
graph.variables().set(GafferPopGraphVariables.USER, userFactory.createUser());
User user = userFactory.createUser();
graph.variables().set(GafferPopGraphVariables.USER, user);
span.setAttribute(OtelUtil.USER_ATTRIBUTE, user.getUserId());

// Run the query using the gremlin executor service
Object result = gremlinExecutor.eval(
Expand Down

0 comments on commit 9bf6f1b

Please sign in to comment.