Skip to content

Telemetry for Data.read #12385

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

Merged
merged 24 commits into from
Mar 11, 2025
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
Show all changes
24 commits
Select commit Hold shift + click to select a range
58bbd85
Generalize enso_cloud log API access
Akirathan Feb 28, 2025
5e98e7b
No point in making uri abstract.
Akirathan Feb 28, 2025
0e2f2e7
Move most functionality from AuditLogMessage to the base class
Akirathan Feb 28, 2025
e150c32
Introduce TelemetryLog
Akirathan Feb 28, 2025
296b926
Add Telemetry_Log_Spec test
Akirathan Feb 28, 2025
d485a52
Handle null EnsoProject
Akirathan Feb 28, 2025
0d7ce6b
Add some telemetry to Data.read
Akirathan Feb 28, 2025
5300f35
Remove IO.println from Logging
Akirathan Feb 28, 2025
51db71e
[WIP] Add more logging
Akirathan Mar 4, 2025
c540239
Revert "[WIP] Add more logging"
Akirathan Mar 5, 2025
6ecdd77
LogMessage may have extraM<eta and extraPayload
Akirathan Mar 6, 2025
0f3b719
Merge branch 'develop' into wip/akirathan/12090-telemetry
Akirathan Mar 6, 2025
963f088
Move Telemetry to private module
Akirathan Mar 6, 2025
19212cf
Move Telemetry_Log_Spec to Base_Internal_Tests
Akirathan Mar 6, 2025
039b279
Make classes package-private
Akirathan Mar 6, 2025
1874832
Mock PostLogHandler treats projectId as optional field
Akirathan Mar 6, 2025
965443f
Update project name in Telemetry_Log_Spec
Akirathan Mar 6, 2025
01e6121
Do not collect file path in telemetry
Akirathan Mar 7, 2025
2bce996
Failed to send telemetry event is logged as a soft error.
Akirathan Mar 7, 2025
c0e771c
Make stuff final
Akirathan Mar 7, 2025
b9ca239
Add file extension to telemetry
Akirathan Mar 7, 2025
a5559e2
Remove telemetry from Data.read with URI parameter.
Akirathan Mar 7, 2025
00d9002
Do not mention OpenSearch
Akirathan Mar 7, 2025
20cada8
Ignore dataflow errors from telemetry sending
Akirathan Mar 11, 2025
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
12 changes: 10 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,17 @@ 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 ->
fetch uri format=format
_ ->
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
metadata = Dictionary.empty
. insert "size" file_obj.size
. insert "extension" file_obj.extension
logged = Telemetry.log "Data.read" "read from file" metadata
Copy link
Member

Choose a reason for hiding this comment

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

Suggested change
logged = Telemetry.log "Data.read" "read from file" metadata
_ = Telemetry.log "Data.read" "read from file" metadata

if logged.is_error then file_content else file_content

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

Choose a reason for hiding this comment

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

Thanks for making this type internal.


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

from project.Logging import all
from project.Data.Boolean import Boolean, True, False

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
handler = err ->
msg = "Telemetry event: {"
+ "logger_name="
+ logger_name
+ ", metadata="
+ metadata.to_text
+ "} was dropped because of: "
+ err.to_text
Telemetry.log_message msg ..Fine
Panic.catch Any handler=handler <|
TelemetryLog.logAsync logger_name message metadata_js.object_node
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