Skip to content

Commit

Permalink
[#10776] Create an ID to distinguish between metric definitions
Browse files Browse the repository at this point in the history
  • Loading branch information
minwoo-jung committed Jul 30, 2024
1 parent 10e349f commit b2f3b26
Show file tree
Hide file tree
Showing 2 changed files with 31 additions and 3 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -26,7 +26,7 @@ public class AppMetricDefinition {
public static final int SCHEMA_VERSION = 1;

private final String applicationName;
private final String id;
private String id;
private final String title;
private final String metricGroupName;
private final String metricName;
Expand All @@ -38,7 +38,7 @@ public class AppMetricDefinition {

public AppMetricDefinition(String applicationName, String id, String title, String metricGroupName, String metricName, String fieldName, String tags, String unit, String chartType) {
this.applicationName = StringPrecondition.requireHasLength(applicationName, "applicationName");
this.id = StringPrecondition.requireHasLength(id, "id");
this.id = id;
this.title = StringPrecondition.requireHasLength(title, "title");
this.metricGroupName = StringPrecondition.requireHasLength(metricGroupName, "metricGroupName");
this.metricName = StringPrecondition.requireHasLength(metricName, "metricName");
Expand All @@ -52,6 +52,10 @@ public String getApplicationName() {
return applicationName;
}

public void setId(String id) {
this.id = id;
}

public String getId() {
return id;
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -16,12 +16,16 @@

package com.navercorp.pinpoint.otlp.web.service;

import com.navercorp.pinpoint.common.util.StringUtils;
import com.navercorp.pinpoint.otlp.common.defined.AppMetricDefinition;
import com.navercorp.pinpoint.otlp.web.dao.AppMetricDefinitionDao;
import org.springframework.stereotype.Service;

import java.util.ArrayList;
import java.util.List;
import java.util.Set;
import java.util.UUID;
import java.util.stream.Collectors;

/**
* @author minwoo-jung
Expand All @@ -42,8 +46,9 @@ public void addUserDefinedMetric(AppMetricDefinition appMetricDefinition) {
if (appMetricDefinitionList == null) {
appMetricDefinitionList = new ArrayList<>();
}

appMetricDefinitionList.add(appMetricDefinition);

generateAndSetUniqueId(appMetricDefinitionList);
appMetricDefinitionDao.insertAppMetricDefinitionList(appMetricDefinitionList);
}

Expand All @@ -65,6 +70,25 @@ public List<AppMetricDefinition> getUserDefinedMetric(String applicationName) {

@Override
public void updateUserDefinedMetric(List<AppMetricDefinition> appMetricDefinitionList) {
generateAndSetUniqueId(appMetricDefinitionList);
appMetricDefinitionDao.updateAppMetricDefinitionList(appMetricDefinitionList);
}

private void generateAndSetUniqueId(List<AppMetricDefinition> appMetricDefinitionList) {
Set<String> existingIds = appMetricDefinitionList.stream()
.map(AppMetricDefinition::getId)
.filter(StringUtils::hasLength)
.collect(Collectors.toSet());

appMetricDefinitionList.stream().filter(appMetricDefinition -> StringUtils.isEmpty(appMetricDefinition.getId()))
.forEach(definition -> {
String newId;

do {
newId = UUID.randomUUID().toString().substring(0, 8);
} while (!existingIds.add(newId));

definition.setId(newId);
});
}
}

0 comments on commit b2f3b26

Please sign in to comment.