-
Notifications
You must be signed in to change notification settings - Fork 2.8k
/
TelemetryUtils.java
49 lines (42 loc) · 1.85 KB
/
TelemetryUtils.java
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
package com.linkedin.gms.factory.telemetry;
import com.linkedin.common.AuditStamp;
import com.linkedin.common.urn.UrnUtils;
import com.linkedin.data.template.RecordTemplate;
import com.linkedin.metadata.Constants;
import com.linkedin.metadata.entity.EntityService;
import com.linkedin.telemetry.TelemetryClientId;
import io.datahubproject.metadata.context.OperationContext;
import jakarta.annotation.Nonnull;
import java.util.UUID;
import lombok.extern.slf4j.Slf4j;
@Slf4j
public final class TelemetryUtils {
public static final String CLIENT_ID_URN = "urn:li:telemetry:clientId";
public static final String CLIENT_ID_ASPECT = "telemetryClientId";
private static String _clientId;
public static String getClientId(
@Nonnull OperationContext opContext, EntityService<?> entityService) {
if (_clientId == null) {
createClientIdIfNotPresent(opContext, entityService);
RecordTemplate clientIdTemplate =
entityService.getLatestAspect(
opContext, UrnUtils.getUrn(CLIENT_ID_URN), CLIENT_ID_ASPECT);
// Should always be present here from above, so no need for null check
_clientId = ((TelemetryClientId) clientIdTemplate).getClientId();
}
return _clientId;
}
private static void createClientIdIfNotPresent(
@Nonnull OperationContext opContext, EntityService<?> entityService) {
String uuid = UUID.randomUUID().toString();
TelemetryClientId clientId = new TelemetryClientId().setClientId(uuid);
final AuditStamp clientIdStamp = new AuditStamp();
clientIdStamp.setActor(UrnUtils.getUrn(Constants.SYSTEM_ACTOR));
clientIdStamp.setTime(System.currentTimeMillis());
entityService.ingestAspectIfNotPresent(
opContext, UrnUtils.getUrn(CLIENT_ID_URN), CLIENT_ID_ASPECT, clientId, clientIdStamp, null);
}
private TelemetryUtils() {
throw new UnsupportedOperationException();
}
}