Skip to content
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

Refactor error budget to use factory (#1393) #1424

Merged
merged 1 commit into from
Feb 6, 2024
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

This file was deleted.

Original file line number Diff line number Diff line change
Expand Up @@ -32,8 +32,8 @@
import com.pinterest.deployservice.bean.HostAgentBean;
import com.pinterest.deployservice.bean.HostBean;
import com.pinterest.deployservice.bean.HostState;
import com.pinterest.deployservice.metrics.MeterConstants;
import com.pinterest.deployservice.rodimus.RodimusManager;
import com.pinterest.teletraan.universal.metrics.ErrorBudgetCounterFactory;

import io.micrometer.core.instrument.Counter;
import io.micrometer.core.instrument.Metrics;
Expand Down Expand Up @@ -67,16 +67,9 @@ public AgentJanitor(ServiceContext serviceContext, int minStaleHostThresholdSeco
maxLaunchLatencyThreshold = TimeUnit.SECONDS.toMillis(maxLaunchLatencyThresholdSeconds);
unreachableHostsCount = Metrics.gauge("unreachable_hosts", new AtomicInteger(0));
staleHostsCount = Metrics.gauge("stale_hosts", new AtomicInteger(0));

errorBudgetSuccess = Metrics.counter(MeterConstants.ERROR_BUDGET_METRIC_NAME,
MeterConstants.ERROR_BUDGET_TAG_NAME_RESPONSE_TYPE,
MeterConstants.ERROR_BUDGET_TAG_VALUE_RESPONSE_TYPE_SUCCESS,
MeterConstants.ERROR_BUDGET_TAG_NAME_METHOD_NAME, this.getClass().getSimpleName());

errorBudgetFailure = Metrics.counter(MeterConstants.ERROR_BUDGET_METRIC_NAME,
MeterConstants.ERROR_BUDGET_TAG_NAME_RESPONSE_TYPE,
MeterConstants.ERROR_BUDGET_TAG_VALUE_RESPONSE_TYPE_FAILURE,
MeterConstants.ERROR_BUDGET_TAG_NAME_METHOD_NAME, this.getClass().getSimpleName());
errorBudgetSuccess = ErrorBudgetCounterFactory.createSuccessCounter(this.getClass().getSimpleName());
errorBudgetFailure = ErrorBudgetCounterFactory.createFailureCounter(this.getClass().getSimpleName());
}

@Override
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -27,10 +27,9 @@
import com.pinterest.deployservice.dao.PromoteDAO;
import com.pinterest.deployservice.dao.UtilDAO;
import com.pinterest.deployservice.handler.DeployHandler;
import com.pinterest.deployservice.metrics.MeterConstants;
import com.pinterest.teletraan.universal.metrics.ErrorBudgetCounterFactory;

import io.micrometer.core.instrument.Counter;
import io.micrometer.core.instrument.Metrics;

import com.google.common.base.Preconditions;
import org.apache.commons.lang.StringUtils;
Expand Down Expand Up @@ -77,13 +76,8 @@ public AutoPromoter(ServiceContext serviceContext) {
deployHandler = new DeployHandler(serviceContext);
bufferTimeMinutes = DEFAULT_BUFFER_TIME_MINUTE;

errorBudgetSuccess = Metrics.counter(MeterConstants.ERROR_BUDGET_METRIC_NAME,
MeterConstants.ERROR_BUDGET_TAG_NAME_RESPONSE_TYPE, MeterConstants.ERROR_BUDGET_TAG_VALUE_RESPONSE_TYPE_SUCCESS,
MeterConstants.ERROR_BUDGET_TAG_NAME_METHOD_NAME, this.getClass().getSimpleName());

errorBudgetFailure = Metrics.counter(MeterConstants.ERROR_BUDGET_METRIC_NAME,
MeterConstants.ERROR_BUDGET_TAG_NAME_RESPONSE_TYPE, MeterConstants.ERROR_BUDGET_TAG_VALUE_RESPONSE_TYPE_FAILURE,
MeterConstants.ERROR_BUDGET_TAG_NAME_METHOD_NAME, this.getClass().getSimpleName());
errorBudgetSuccess = ErrorBudgetCounterFactory.createSuccessCounter(this.getClass().getSimpleName());
errorBudgetFailure = ErrorBudgetCounterFactory.createFailureCounter(this.getClass().getSimpleName());
}

public AutoPromoter withBufferTimeMinutes(int bufferTime) {
Expand Down Expand Up @@ -596,7 +590,7 @@ public void run() {
} catch (Throwable t) {
// Catch all throwable so that subsequent job not suppressed
LOG.error("Failed to call AutoPromoter.", t);

errorBudgetFailure.increment();
}
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -4,9 +4,9 @@
* 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.
Expand All @@ -17,8 +17,8 @@

import com.pinterest.deployservice.dao.BuildDAO;
import com.pinterest.deployservice.dao.UtilDAO;
import com.pinterest.deployservice.metrics.MeterConstants;
import com.pinterest.teletraan.TeletraanServiceContext;
import com.pinterest.teletraan.universal.metrics.ErrorBudgetCounterFactory;

import org.quartz.Job;
import org.quartz.JobExecutionContext;
Expand All @@ -32,7 +32,6 @@
import java.util.List;

import io.micrometer.core.instrument.Counter;
import io.micrometer.core.instrument.Metrics;

/**
* Remove unused and old builds.
Expand Down Expand Up @@ -94,14 +93,9 @@ public void execute(JobExecutionContext context) throws JobExecutionException {
LOG.info("Start build janitor process...");
SchedulerContext schedulerContext = context.getScheduler().getContext();
TeletraanServiceContext workerContext = (TeletraanServiceContext) schedulerContext.get("serviceContext");

errorBudgetSuccess = Metrics.counter(MeterConstants.ERROR_BUDGET_METRIC_NAME,
MeterConstants.ERROR_BUDGET_TAG_NAME_RESPONSE_TYPE, MeterConstants.ERROR_BUDGET_TAG_VALUE_RESPONSE_TYPE_SUCCESS,
MeterConstants.ERROR_BUDGET_TAG_NAME_METHOD_NAME, this.getClass().getSimpleName());

errorBudgetFailure = Metrics.counter(MeterConstants.ERROR_BUDGET_METRIC_NAME,
MeterConstants.ERROR_BUDGET_TAG_NAME_RESPONSE_TYPE, MeterConstants.ERROR_BUDGET_TAG_VALUE_RESPONSE_TYPE_FAILURE,
MeterConstants.ERROR_BUDGET_TAG_NAME_METHOD_NAME, this.getClass().getSimpleName());

errorBudgetSuccess = ErrorBudgetCounterFactory.createSuccessCounter(this.getClass().getSimpleName());
errorBudgetFailure = ErrorBudgetCounterFactory.createFailureCounter(this.getClass().getSimpleName());

processBuilds(workerContext);
LOG.info("Stop build janitor process...");
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -4,9 +4,9 @@
* 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.
Expand All @@ -20,7 +20,7 @@
import com.pinterest.deployservice.dao.DeployDAO;
import com.pinterest.deployservice.dao.EnvironDAO;
import com.pinterest.deployservice.dao.UtilDAO;
import com.pinterest.deployservice.metrics.MeterConstants;
import com.pinterest.teletraan.universal.metrics.ErrorBudgetCounterFactory;

import org.quartz.*;
import org.slf4j.Logger;
Expand All @@ -31,7 +31,6 @@
import java.util.List;

import io.micrometer.core.instrument.Counter;
import io.micrometer.core.instrument.Metrics;

/**
* Removed unused/old deploys.
Expand Down Expand Up @@ -90,13 +89,8 @@ void processDeploys() throws Exception {
public void execute(JobExecutionContext context) throws JobExecutionException {
SchedulerContext schedulerContext;

errorBudgetSuccess = Metrics.counter(MeterConstants.ERROR_BUDGET_METRIC_NAME,
MeterConstants.ERROR_BUDGET_TAG_NAME_RESPONSE_TYPE, MeterConstants.ERROR_BUDGET_TAG_VALUE_RESPONSE_TYPE_SUCCESS,
MeterConstants.ERROR_BUDGET_TAG_NAME_METHOD_NAME, this.getClass().getSimpleName());

errorBudgetFailure = Metrics.counter(MeterConstants.ERROR_BUDGET_METRIC_NAME,
MeterConstants.ERROR_BUDGET_TAG_NAME_RESPONSE_TYPE, MeterConstants.ERROR_BUDGET_TAG_VALUE_RESPONSE_TYPE_FAILURE,
MeterConstants.ERROR_BUDGET_TAG_NAME_METHOD_NAME, this.getClass().getSimpleName());
errorBudgetSuccess = ErrorBudgetCounterFactory.createSuccessCounter(this.getClass().getSimpleName());
errorBudgetFailure = ErrorBudgetCounterFactory.createFailureCounter(this.getClass().getSimpleName());

try {
schedulerContext = context.getScheduler().getContext();
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -4,8 +4,8 @@
import com.pinterest.deployservice.bean.*;
import com.pinterest.deployservice.dao.*;
import com.pinterest.deployservice.db.DatabaseUtil;
import com.pinterest.deployservice.metrics.MeterConstants;
import com.pinterest.deployservice.rodimus.RodimusManager;
import com.pinterest.teletraan.universal.metrics.ErrorBudgetCounterFactory;

import org.apache.commons.collections.CollectionUtils;
import org.apache.commons.collections.TransformerUtils;
Expand All @@ -19,7 +19,6 @@
import java.util.*;

import io.micrometer.core.instrument.Counter;
import io.micrometer.core.instrument.Metrics;

public class DeployTagWorker implements Runnable {
private static final Logger LOG = LoggerFactory.getLogger(DeployTagWorker.class);
Expand All @@ -44,13 +43,8 @@ public DeployTagWorker(ServiceContext serviceContext) {
dataSource = serviceContext.getDataSource();
utilDAO = serviceContext.getUtilDAO();

errorBudgetSuccess = Metrics.counter(MeterConstants.ERROR_BUDGET_METRIC_NAME,
MeterConstants.ERROR_BUDGET_TAG_NAME_RESPONSE_TYPE, MeterConstants.ERROR_BUDGET_TAG_VALUE_RESPONSE_TYPE_SUCCESS,
MeterConstants.ERROR_BUDGET_TAG_NAME_METHOD_NAME, this.getClass().getSimpleName());

errorBudgetFailure = Metrics.counter(MeterConstants.ERROR_BUDGET_METRIC_NAME,
MeterConstants.ERROR_BUDGET_TAG_NAME_RESPONSE_TYPE, MeterConstants.ERROR_BUDGET_TAG_VALUE_RESPONSE_TYPE_FAILURE,
MeterConstants.ERROR_BUDGET_TAG_NAME_METHOD_NAME, this.getClass().getSimpleName());
errorBudgetSuccess = ErrorBudgetCounterFactory.createSuccessCounter(this.getClass().getSimpleName());
errorBudgetFailure = ErrorBudgetCounterFactory.createFailureCounter(this.getClass().getSimpleName());
}


Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -27,9 +27,8 @@
import com.pinterest.deployservice.dao.HostDAO;
import com.pinterest.deployservice.dao.UtilDAO;
import com.pinterest.deployservice.rodimus.RodimusManager;

import com.pinterest.teletraan.universal.metrics.ErrorBudgetCounterFactory;
import com.pinterest.deployservice.handler.HostHandler;
import com.pinterest.deployservice.metrics.MeterConstants;

import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
Expand All @@ -38,7 +37,6 @@
import java.util.*;

import io.micrometer.core.instrument.Counter;
import io.micrometer.core.instrument.Metrics;

public class HostTerminator implements Runnable {
private static final Logger LOG = LoggerFactory.getLogger(HostTerminator.class);
Expand All @@ -59,13 +57,8 @@ public HostTerminator(ServiceContext serviceContext) {
hostAgentDAO = serviceContext.getHostAgentDAO();
hostHandler = new HostHandler(serviceContext);

errorBudgetSuccess = Metrics.counter(MeterConstants.ERROR_BUDGET_METRIC_NAME,
MeterConstants.ERROR_BUDGET_TAG_NAME_RESPONSE_TYPE, MeterConstants.ERROR_BUDGET_TAG_VALUE_RESPONSE_TYPE_SUCCESS,
MeterConstants.ERROR_BUDGET_TAG_NAME_METHOD_NAME, this.getClass().getSimpleName());

errorBudgetFailure = Metrics.counter(MeterConstants.ERROR_BUDGET_METRIC_NAME,
MeterConstants.ERROR_BUDGET_TAG_NAME_RESPONSE_TYPE, MeterConstants.ERROR_BUDGET_TAG_VALUE_RESPONSE_TYPE_FAILURE,
MeterConstants.ERROR_BUDGET_TAG_NAME_METHOD_NAME, this.getClass().getSimpleName());
errorBudgetSuccess = ErrorBudgetCounterFactory.createSuccessCounter(this.getClass().getSimpleName());
errorBudgetFailure = ErrorBudgetCounterFactory.createFailureCounter(this.getClass().getSimpleName());
}

private void terminateHost(HostBean host) throws Exception {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -22,7 +22,7 @@
import com.pinterest.deployservice.common.Jenkins;
import com.pinterest.deployservice.dao.*;
import com.pinterest.deployservice.handler.CommonHandler;
import com.pinterest.deployservice.metrics.MeterConstants;
import com.pinterest.teletraan.universal.metrics.ErrorBudgetCounterFactory;

import org.apache.commons.lang.StringUtils;
import org.slf4j.Logger;
Expand All @@ -35,7 +35,6 @@
import java.util.Set;

import io.micrometer.core.instrument.Counter;
import io.micrometer.core.instrument.Metrics;

/**
* Check active deploys and push them into their final states
Expand Down Expand Up @@ -66,13 +65,8 @@ public HotfixStateTransitioner(ServiceContext serviceContext) {
jenkinsUrl = serviceContext.getJenkinsUrl();
jenkinsRemoteToken = serviceContext.getJenkinsRemoteToken();

errorBudgetSuccess = Metrics.counter(MeterConstants.ERROR_BUDGET_METRIC_NAME,
MeterConstants.ERROR_BUDGET_TAG_NAME_RESPONSE_TYPE, MeterConstants.ERROR_BUDGET_TAG_VALUE_RESPONSE_TYPE_SUCCESS,
MeterConstants.ERROR_BUDGET_TAG_NAME_METHOD_NAME, this.getClass().getSimpleName());

errorBudgetFailure = Metrics.counter(MeterConstants.ERROR_BUDGET_METRIC_NAME,
MeterConstants.ERROR_BUDGET_TAG_NAME_RESPONSE_TYPE, MeterConstants.ERROR_BUDGET_TAG_VALUE_RESPONSE_TYPE_FAILURE,
MeterConstants.ERROR_BUDGET_TAG_NAME_METHOD_NAME, this.getClass().getSimpleName());
errorBudgetSuccess = ErrorBudgetCounterFactory.createSuccessCounter(this.getClass().getSimpleName());
errorBudgetFailure = ErrorBudgetCounterFactory.createFailureCounter(this.getClass().getSimpleName());
}

void processBatch() throws Exception {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -4,9 +4,9 @@
* 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.
Expand All @@ -18,7 +18,7 @@
import com.pinterest.deployservice.ServiceContext;
import com.pinterest.deployservice.dao.EnvironDAO;
import com.pinterest.deployservice.handler.CommonHandler;
import com.pinterest.deployservice.metrics.MeterConstants;
import com.pinterest.teletraan.universal.metrics.ErrorBudgetCounterFactory;

import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
Expand All @@ -27,7 +27,6 @@
import java.util.List;

import io.micrometer.core.instrument.Counter;
import io.micrometer.core.instrument.Metrics;

/**
* Check active deploys and transition them into final states
Expand All @@ -43,14 +42,8 @@ public class StateTransitioner implements Runnable {
public StateTransitioner(ServiceContext serviceContext) {
environDAO = serviceContext.getEnvironDAO();
commonHandler = new CommonHandler(serviceContext);

errorBudgetSuccess = Metrics.counter(MeterConstants.ERROR_BUDGET_METRIC_NAME,
MeterConstants.ERROR_BUDGET_TAG_NAME_RESPONSE_TYPE, MeterConstants.ERROR_BUDGET_TAG_VALUE_RESPONSE_TYPE_SUCCESS,
MeterConstants.ERROR_BUDGET_TAG_NAME_METHOD_NAME, this.getClass().getSimpleName());

errorBudgetFailure = Metrics.counter(MeterConstants.ERROR_BUDGET_METRIC_NAME,
MeterConstants.ERROR_BUDGET_TAG_NAME_RESPONSE_TYPE, MeterConstants.ERROR_BUDGET_TAG_VALUE_RESPONSE_TYPE_FAILURE,
MeterConstants.ERROR_BUDGET_TAG_NAME_METHOD_NAME, this.getClass().getSimpleName());
errorBudgetSuccess = ErrorBudgetCounterFactory.createSuccessCounter(this.getClass().getSimpleName());
errorBudgetFailure = ErrorBudgetCounterFactory.createFailureCounter(this.getClass().getSimpleName());
}

void processBatch() throws Exception {
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,36 @@
package com.pinterest.teletraan.universal.metrics;

import static com.pinterest.teletraan.universal.metrics.micrometer.PinStatsNamingConvention.CUSTOM_NAME_PREFIX;

import io.micrometer.core.instrument.Counter;
import io.micrometer.core.instrument.MeterRegistry;

public class ErrorBudgetCounterFactory {

public final static String ERROR_BUDGET_METRIC_NAME = CUSTOM_NAME_PREFIX + "error-budget.counters";
public final static String ERROR_BUDGET_TAG_NAME_METHOD_NAME = "method_name";
public final static String ERROR_BUDGET_TAG_NAME_RESPONSE_TYPE = "response_type";
public final static String ERROR_BUDGET_TAG_VALUE_RESPONSE_TYPE_SUCCESS = "success";
public final static String ERROR_BUDGET_TAG_VALUE_RESPONSE_TYPE_FAILURE = "failure";

private ErrorBudgetCounterFactory() {}

public static Counter createCounter(MeterRegistry registry, String methodName, boolean success) {
return Counter.builder(ERROR_BUDGET_METRIC_NAME)
.tag(
ERROR_BUDGET_TAG_NAME_RESPONSE_TYPE,
success
? ERROR_BUDGET_TAG_VALUE_RESPONSE_TYPE_SUCCESS
: ERROR_BUDGET_TAG_VALUE_RESPONSE_TYPE_FAILURE)
.tag(ERROR_BUDGET_TAG_NAME_METHOD_NAME, methodName)
.register(registry);
}

public static Counter createSuccessCounter(String methodName) {
return createCounter(io.micrometer.core.instrument.Metrics.globalRegistry, methodName, true);
}

public static Counter createFailureCounter(String methodName) {
return createCounter(io.micrometer.core.instrument.Metrics.globalRegistry, methodName, false);
}
}
Loading