All Downloads are FREE. Search and download functionalities are using the official Maven repository.

io.camunda.exporter.handlers.MetricFromProcessInstanceHandler Maven / Gradle / Ivy

There is a newer version: 8.7.0-alpha2
Show newest version
/*
 * Copyright Camunda Services GmbH and/or licensed to Camunda Services GmbH under
 * one or more contributor license agreements. See the NOTICE file distributed
 * with this work for additional information regarding copyright ownership.
 * Licensed under the Camunda License 1.0. You may not use this file
 * except in compliance with the Camunda License 1.0.
 */
package io.camunda.exporter.handlers;

import static io.camunda.exporter.utils.ExporterUtil.tenantOrDefault;
import static io.camunda.zeebe.protocol.record.intent.ProcessInstanceIntent.ELEMENT_ACTIVATING;

import io.camunda.exporter.store.BatchRequest;
import io.camunda.webapps.schema.entities.MetricEntity;
import io.camunda.zeebe.protocol.record.Record;
import io.camunda.zeebe.protocol.record.ValueType;
import io.camunda.zeebe.protocol.record.value.ProcessInstanceRecordValue;
import java.time.Instant;
import java.time.OffsetDateTime;
import java.time.ZoneOffset;
import java.util.List;

public class MetricFromProcessInstanceHandler
    implements ExportHandler {
  protected static final int EMPTY_PARENT_PROCESS_INSTANCE_ID = -1;
  protected static final String EVENT_PROCESS_INSTANCE_STARTED = "EVENT_PROCESS_INSTANCE_STARTED";

  private final String indexName;

  public MetricFromProcessInstanceHandler(final String indexName) {
    this.indexName = indexName;
  }

  @Override
  public ValueType getHandledValueType() {
    return ValueType.PROCESS_INSTANCE;
  }

  @Override
  public Class getEntityType() {
    return MetricEntity.class;
  }

  @Override
  public boolean handlesRecord(final Record record) {
    final var recordValue = record.getValue();
    final boolean isRootProcessInstance =
        recordValue.getParentProcessInstanceKey() == EMPTY_PARENT_PROCESS_INSTANCE_ID;
    return isRootProcessInstance && record.getIntent().equals(ELEMENT_ACTIVATING);
  }

  @Override
  public List generateIds(final Record record) {
    return List.of(String.valueOf(record.getValue().getProcessInstanceKey()));
  }

  @Override
  public MetricEntity createNewEntity(final String id) {
    return new MetricEntity().setId(id);
  }

  @Override
  public void updateEntity(
      final Record record, final MetricEntity entity) {
    final ProcessInstanceRecordValue recordValue = record.getValue();
    final String processInstanceKey = String.valueOf(recordValue.getProcessInstanceKey());
    final OffsetDateTime timestamp =
        OffsetDateTime.ofInstant(Instant.ofEpochMilli(record.getTimestamp()), ZoneOffset.UTC);
    final String tenantId = tenantOrDefault(recordValue.getTenantId());
    entity
        .setEvent(EVENT_PROCESS_INSTANCE_STARTED)
        .setValue(processInstanceKey)
        .setEventTime(timestamp)
        .setTenantId(tenantId);
  }

  @Override
  public void flush(final MetricEntity entity, final BatchRequest batchRequest) {
    batchRequest.add(indexName, entity);
  }

  @Override
  public String getIndexName() {
    return indexName;
  }
}




© 2015 - 2025 Weber Informatics LLC | Privacy Policy