Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

PoC: Telemetry for Data.read #12385

Open
wants to merge 17 commits into
base: develop
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
11 changes: 9 additions & 2 deletions distribution/lib/Standard/Base/0.0.0-dev/src/Data.enso
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
import project.Any.Any
import project.Data.Download.Download_Mode.Download_Mode
import project.Data.Dictionary.Dictionary
import project.Data.Pair.Pair
import project.Data.Read.Many_Files_List.Many_Files_List
import project.Data.Read.Return_As.Return_As
Expand All @@ -16,6 +17,7 @@ import project.Errors.File_Error.File_Error
import project.Errors.Illegal_Argument.Illegal_Argument
import project.Errors.Problem_Behavior.Problem_Behavior
import project.Internal.Data_Read_Helpers
import project.Internal.Telemetry.Telemetry
import project.Meta
import project.Network.HTTP.Cache_Policy.Cache_Policy
import project.Network.HTTP.Header.Header
Expand Down Expand Up @@ -91,11 +93,16 @@ read : Text | URI | File -> File_Format -> Problem_Behavior -> Any ! File_Error
read path=(Missing_Argument.throw "path") format=Auto_Detect (on_problems : Problem_Behavior = ..Report_Warning) = case path of
_ : Text -> if Data_Read_Helpers.looks_like_uri path then Data_Read_Helpers.fetch_following_data_links path format=format else
read (File.new path) format on_problems
uri : URI -> fetch uri format=format
uri : URI ->
response = fetch uri format=format
Telemetry.log "Data.read" "fetching from URI" (Dictionary.from_vector [["content_length", response.content_length]])
response
_ ->
file_obj = File.new path
if file_obj.is_directory then Error.throw (Illegal_Argument.Error "Cannot `read` a directory, use `Data.list`.") else
file_obj.read format on_problems
file_content = file_obj.read format on_problems
Telemetry.log "Data.read" "read from file" (Dictionary.from_vector [["path", file_obj.path]])
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Sending home the file path might reveal customer internals we are not interested in. File size and mime type should be generic enough, however.

file_content

## ALIAS load, open
GROUP Input
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,41 @@
private

import project.Any.Any
import project.Data.Dictionary.Dictionary
import project.Data.Json.JS_Object
import project.Data.Text.Text
import project.Errors.Illegal_Argument.Illegal_Argument
import project.Panic.Panic

polyglot java import org.enso.base.enso_cloud.telemetry.TelemetryLog
polyglot java import org.enso.base.enso_cloud.telemetry.TelemetryLog.TelemetryLogError

## Heavily inspired by `Standard.Base.Enso_Cloud.Internal.Audit_Log`.
type Telemetry
logger_root = "org.enso.telemetry"

## Reports an event to the telemetry log.
The event is submitted asynchronously.

Arguments:
- name: Name of the telemetry logger
- message: The message associated with the event.
- metadata: Additional metadata to include with the event.
Note that there are some _restricted_ keys that are added automatically.
See `Standard.Base.Enso_Cloud.Internal.Audit_Log`.
log self name:Text message:Text (metadata:Dictionary Text Any = Dictionary.empty) =
logger_name = Telemetry.logger_root + "." + name
metadata_js = JS_Object.from_pairs <| metadata.to_vector
Illegal_Argument.handle_java_exception <|
Telemetry_Log_Error.handle_java_exception <|
TelemetryLog.logAsync logger_name message metadata_js.object_node


type Telemetry_Log_Error
private Error message:Text cause:Any

private handle_java_exception =
on_error caught_panic =
cause = caught_panic.payload
Panic.throw (Telemetry_Log_Error.Error cause.getMessage cause)
Panic.catch TelemetryLogError handler=on_error
Original file line number Diff line number Diff line change
Expand Up @@ -24,7 +24,8 @@ public static CurrentEnsoProject get() {
EnsoMeta.callStaticModuleMethod("Standard.Base.Meta.Enso_Project", "enso_project");
Value namespace = invokeMember("namespace", ensoProject);
Value name = invokeMember("name", ensoProject);
Value rootPath = invokeMember("path", invokeMember("root", ensoProject));
Value root = invokeMember("root", ensoProject);
Value rootPath = root != null ? invokeMember("path", root) : null;
if (namespace == null || name == null || rootPath == null) {
cached = null;
} else {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@
import com.fasterxml.jackson.databind.node.ObjectNode;
import java.util.concurrent.ExecutionException;
import java.util.concurrent.Future;
import org.enso.base.enso_cloud.logging.LogApiAccess;

/**
* The high-level API for logging audit events.
Expand All @@ -13,16 +14,18 @@
* the meantime, all waiting messages (up to some limit) will be sent in a single request.
*/
public final class AuditLog {
private AuditLog() {}

/** Schedules the log message to be sent in the next batch, and returns immediately. */
public static void logAsync(String type, String message, ObjectNode metadata) {
var event = new AuditLogMessage(type, message, metadata);
AuditLogApiAccess.INSTANCE.logWithoutConfirmation(event);
var event = AuditLogMessage.create(type, message, metadata);
LogApiAccess.INSTANCE.logWithoutConfirmation(event);
}

/** Schedules the log message to be sent in the next batch, and waits until it has been sent. */
public static void logSynchronously(String type, String message, ObjectNode metadata) {
var event = new AuditLogMessage(type, message, metadata);
Future<Void> future = AuditLogApiAccess.INSTANCE.logWithConfirmation(event);
var event = AuditLogMessage.create(type, message, metadata);
Future<Void> future = LogApiAccess.INSTANCE.logWithConfirmation(event);
try {
future.get();
} catch (ExecutionException | InterruptedException e) {
Expand All @@ -37,6 +40,6 @@ public AuditLogError(String message, Throwable cause) {
}

public static void resetCache() {
AuditLogApiAccess.INSTANCE.resetCache();
LogApiAccess.INSTANCE.resetCache();
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -5,77 +5,56 @@
import com.fasterxml.jackson.databind.node.ObjectNode;
import com.fasterxml.jackson.databind.node.TextNode;
import java.util.Objects;
import org.enso.base.CurrentEnsoProject;
import org.enso.base.enso_cloud.CloudAPI;
import org.enso.base.enso_cloud.logging.LogMessage;

class AuditLogMessage implements AuditLogApiAccess.LogMessage {

/**
* A reserved field that is currently added by the cloud backend. Duplicating it will lead to
* internal server errors and log messages being discarded.
*/
private static final String RESERVED_TYPE = "type";
final class AuditLogMessage extends LogMessage {

private static final String OPERATION = "operation";
private static final String PROJECT_NAME = "projectName";
private static final String PROJECT_ID = "projectId";
private static final String PROJECT_SESSION_ID = "projectSessionId";
private static final String LOCAL_TIMESTAMP = "localTimestamp";

private final String projectId;
private final String projectName;
private final String operation;
private final String message;
private final ObjectNode metadata;
private final String projectId;

public AuditLogMessage(String operation, String message, ObjectNode metadata) {
this.operation = Objects.requireNonNull(operation);
this.message = Objects.requireNonNull(message);
this.metadata = Objects.requireNonNull(metadata);
checkNoRestrictedField(metadata, RESERVED_TYPE);
checkNoRestrictedField(metadata, OPERATION);
checkNoRestrictedField(metadata, PROJECT_NAME);
checkNoRestrictedField(metadata, PROJECT_SESSION_ID);
checkNoRestrictedField(metadata, LOCAL_TIMESTAMP);

this.projectId = CloudAPI.getCloudProjectId();

var currentProject = CurrentEnsoProject.get();
this.projectName = currentProject == null ? null : currentProject.fullName();
private AuditLogMessage(String message, String operation, ObjectNode metadata, String projectId) {
super(message);
this.operation = operation;
this.metadata = metadata;
this.projectId = projectId;
}

private static void checkNoRestrictedField(ObjectNode metadata, String fieldName) {
if (metadata.has(fieldName)) {
throw new IllegalArgumentException(
"Metadata cannot contain a field named '" + fieldName + "'");
}
public static AuditLogMessage create(String operation, String message, ObjectNode metadata) {
Objects.requireNonNull(operation);
Objects.requireNonNull(message);
Objects.requireNonNull(metadata);
checkNoRestrictedFields(metadata);
var projectId = CloudAPI.getCloudProjectId();
return new AuditLogMessage(message, operation, metadata, projectId);
}

private ObjectNode computedMetadata() {
var copy = metadata.deepCopy();
copy.set(OPERATION, TextNode.valueOf(operation));

// The project name may be null if a script is run outside a project.
if (projectName != null) {
copy.set(PROJECT_NAME, TextNode.valueOf(projectName));
}

String projectSessionId = CloudAPI.getCloudSessionId();
if (projectSessionId != null) {
copy.set(PROJECT_SESSION_ID, TextNode.valueOf(projectSessionId));
}

return copy;
@Override
protected String kind() {
return "Lib";
}

@Override
public String payload() {
protected ObjectNode extraPayload() {
var payload = new ObjectNode(JsonNodeFactory.instance);
payload.set("message", TextNode.valueOf(message));
payload.set(
PROJECT_ID, projectId == null ? NullNode.getInstance() : TextNode.valueOf(projectId));
payload.set("metadata", computedMetadata());
payload.set("kind", TextNode.valueOf("Lib"));
return payload.toString();
return payload;
}

@Override
protected ObjectNode extraMetadata() {
var meta = new ObjectNode(JsonNodeFactory.instance);
meta.set(OPERATION, TextNode.valueOf(operation));
metadata
.fields()
.forEachRemaining(
entry -> {
meta.set(entry.getKey(), entry.getValue());
});
return meta;
}
}
Loading
Loading