Skip to content
Merged
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
Original file line number Diff line number Diff line change
Expand Up @@ -253,7 +253,10 @@ private Completable logEvent(
parseFuture =
state
.getParser()
.parse(content)
.parse(
content,
traceIds.traceId(),
traceIds.spanId() != null ? traceIds.spanId() : "no_span")
.thenAccept(
parsedContent -> {
row.put(
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,94 @@
/*
* Copyright 2026 Google LLC
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/

package com.google.adk.plugins.agentanalytics;

import static java.nio.charset.StandardCharsets.UTF_8;

import com.google.auth.Credentials;
import com.google.cloud.storage.BlobId;
import com.google.cloud.storage.BlobInfo;
import com.google.cloud.storage.Storage;
import com.google.cloud.storage.StorageOptions;
import java.util.concurrent.CompletableFuture;
import java.util.concurrent.Executor;
import java.util.concurrent.RejectedExecutionException;
import org.jspecify.annotations.Nullable;

/** Offloads content to GCS. */
class GcsOffloader {
private final Storage storage;
private final String bucketName;
private final Executor executor;
private final boolean isStorageOverride;

GcsOffloader(
String projectId,
String bucketName,
Executor executor,
@Nullable Credentials credentials,
@Nullable Storage storageOverride) {
if (storageOverride != null) {
this.isStorageOverride = true;
this.storage = storageOverride;
} else {
this.isStorageOverride = false;
StorageOptions.Builder builder = StorageOptions.newBuilder().setProjectId(projectId);
if (credentials != null) {
builder.setCredentials(credentials);
}
this.storage = builder.build().getService();
}
this.bucketName = bucketName;
this.executor = executor;
}

/** Async wrapper around blocking GCS upload for binary data. */
CompletableFuture<String> uploadContent(byte[] data, String contentType, String path) {
try {
return CompletableFuture.supplyAsync(
() -> {
BlobId blobId = BlobId.of(bucketName, path);
BlobInfo blobInfo = BlobInfo.newBuilder(blobId).setContentType(contentType).build();
storage.create(blobInfo, data);
return String.format("gs://%s/%s", bucketName, path);
},
executor);
} catch (RejectedExecutionException e) {
return CompletableFuture.failedFuture(e);
}
}

/** Async wrapper around blocking GCS upload for text data. */
CompletableFuture<String> uploadContent(String data, String contentType, String path) {
try {
return CompletableFuture.supplyAsync(() -> data.getBytes(UTF_8), executor)
.thenCompose(bytes -> uploadContent(bytes, contentType, path));
} catch (RejectedExecutionException e) {
return CompletableFuture.failedFuture(e);
}
}

String getBucketName() {
return bucketName;
}

void close() throws Exception {
if (storage != null && !isStorageOverride) {
storage.close();
}
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,60 @@
/*
* Copyright 2026 Google LLC
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/

package com.google.adk.plugins.agentanalytics;

import com.google.common.collect.ImmutableMap;

/** Utility to map MIME types to file extensions. */
final class MimeTypeMapper {
private static final ImmutableMap<String, String> MIME_TO_EXT =
ImmutableMap.<String, String>builder()
// Images
.put("image/jpeg", ".jpg")
.put("image/png", ".png")
.put("image/gif", ".gif")
.put("image/webp", ".webp")
.put("image/bmp", ".bmp")
.put("image/tiff", ".tiff")
// Audio
.put("audio/mpeg", ".mp3")
.put("audio/ogg", ".ogg")
.put("audio/wav", ".wav")
.put("audio/x-wav", ".wav")
.put("audio/webm", ".webm")
.put("audio/aac", ".aac")
.put("audio/midi", ".mid")
.put("audio/x-m4a", ".m4a")
// Video
.put("video/mp4", ".mp4")
.put("video/mpeg", ".mpeg")
.put("video/ogg", ".ogv")
.put("video/webm", ".webm")
.put("video/avi", ".avi")
.put("video/x-msvideo", ".avi")
.put("video/quicktime", ".mov")
.buildOrThrow();

private MimeTypeMapper() {}

/**
* Returns the file extension (including the dot) for the given MIME type. Returns an empty string
* if the MIME type is unknown.
*/
static String getExtension(String mimeType) {
return MIME_TO_EXT.getOrDefault(mimeType, "");
}
}
Loading
Loading