Publish custom events (Event Grid)

Create custom topic for AI events

Un custom topic tiene un user-defined endpoint donde tu app postea eventos.

Antes de poder publicar eventos, tienes que crear y configurar el topic y el schema de tus eventos.

Usa el input schema a cloudeventsschemav1_0 al crear el topic para que los eventos usen el formato standard CloudEvents.

az eventgrid topic create \
	--name ai-pipeline-events \
	--resource-group ai-platform-rg \
	--location eastus \
	--input-schema cloudeventschemav1_0

Después de crear el topic, necesitas credenciales para publicar eventos. Puedes obtener el topic endpoint y access key con Azure CLI

az eventgrid topic show \
	--name ai-pipeline-events \
	--resource-group ai-platform-rg \
	--query "endpoint" \
	--output tsv

az eventgrid topic key list \
	--name ai-pipeline-events \
	--resource-group ai-platform-rg \
	--query "key1" \
	--output tsv

Event Grid soporta 3 metodos de auth:

  • access key auth (header aeg-sas-key)
  • SAS token auth
  • Microsoft Entra ID

Construct events for AI ops

Cada custom event describe e implica un meaningful state change en tu sistema. Un evento bien construido incluye suficiente contexto como para que el consumer comience a procesar, pero sin llegar a meter todos los datos dentro. Los subscribers sacan los datos detallados de un data store usando IDs del event data.

CloudEvents event. type categoriza la op, source la fuente donde se origina, subject provee un path filtrable, data payload incluye metadatos sobre la op y una referencia donde el evento puede encontrar todos los detalles

{
    "specversion": "1.0",
    "type": "com.contoso.ai.InferenceCompleted",
    "source": "/services/content-moderation",
    "id": "evt-20250915-143000-001",
    "time": "2025-09-15T14:30:00Z",
    "subject": "/pipelines/moderation/image-classifier",
    "datacontenttype": "application/json",
    "data": {
        "requestId": "req-78901",
        "modelName": "content-classifier-v3",
        "modelVersion": "3.2.1",
        "processingDurationMs": 1250,
        "resultLocation": "https://results.blob.core.windows.net/output/req-78901.json",
        "status": "completed",
        "itemsProcessed": 1,
        "summary": {
            "classification": "safe",
            "confidence": 0.97
        }
    }
}

Al diseñar eventos:

  • Incluye IDs para correlacion: requestId o pipe run ID para tracear entre servicios distribuidos
  • Referencia resultados en vez de meterlos: Los resultados detallados van a un storage y se manda solo la referencia
  • Operational metadata: Campos como processingDurationMs o modelVersion permiten monitorizar trends
  • Provee un summary: asi los consumidores pueden tomar routing decisions sin tener que descargar el texto completo

Publish events using Event Grid SDK

El EventGridPublisherClient de la libreria azure-eventgrid maneja serializacion, atuh y retries.

crea un client y publica CloudEvent to custom topic

# Code fragment - focus on creating and sending a CloudEvent
from azure.core.credentials import AzureKeyCredential
from azure.core.messaging import CloudEvent
from azure.eventgrid import EventGridPublisherClient

endpoint = os.environ["EVENTGRID_TOPIC_ENDPOINT"]
key = os.environ["EVENTGRID_TOPIC_KEY"]

credential = AzureKeyCredential(key)
client = EventGridPublisherClient(endpoint, credential)

event = CloudEvent(
    type="com.contoso.ai.InferenceCompleted",
    source="/services/content-moderation",
    data={
        "requestId": "req-78901",
        "modelName": "content-classifier-v3",
        "processingDurationMs": 1250,
        "resultLocation": "https://results.blob.core.windows.net/output/req-78901.json",
        "status": "completed"
    },
    subject="/pipelines/moderation/image-classifier"
)

client.send(event)

Para PROD usar DefaultAzureCredential para autenticar con manage identity - no usar keys

# Code fragment - focus on managed identity authentication
from azure.identity import DefaultAzureCredential
from azure.core.messaging import CloudEvent
from azure.eventgrid import EventGridPublisherClient

endpoint = os.environ["EVENTGRID_TOPIC_ENDPOINT"]

credential = DefaultAzureCredential()
client = EventGridPublisherClient(endpoint, credential)

Podemos publicar eventos en batches también

# Code fragment - focus on batch publishing
events = [
    CloudEvent(
        type="com.contoso.ai.StageCompleted",
        source="/services/embeddings",
        data={"pipelineRunId": "run-42", "stage": "embeddings", "status": "completed"},
        subject="/pipelines/rag/run-42"
    ),
    CloudEvent(
        type="com.contoso.ai.StageCompleted",
        source="/services/indexing",
        data={"pipelineRunId": "run-42", "stage": "indexing", "status": "completed"},
        subject="/pipelines/rag/run-42"
    )
]

client.send(events)

Publish events using REST API

Podemos publicar eventos enviando un HTTP POST directos al custom topic endpoint.

curl -X POST \
    -H "Content-Type: application/cloudevents+json; charset=utf-8" \
    -H "aeg-sas-key: $EVENTGRID_TOPIC_KEY" \
    -d '{
        "specversion": "1.0",
        "type": "com.contoso.ai.ModelRetrained",
        "source": "/services/training",
        "id": "evt-20250915-160000-001",
        "time": "2025-09-15T16:00:00Z",
        "subject": "/models/sentiment-v2",
        "datacontenttype": "application/json",
        "data": {
            "modelName": "sentiment-v2",
            "modelVersion": "2.1.0",
            "accuracy": 0.94,
            "trainingDurationMinutes": 45,
            "artifactLocation": "https://models.blob.core.windows.net/trained/sentiment-v2.1.0.tar.gz"
        }
    }' \
    "$EVENTGRID_TOPIC_ENDPOINT"