forked from apache/pulsar
-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
REST and CLI to get function metrics in json for monitoring (apache#2296
- Loading branch information
Showing
15 changed files
with
386 additions
and
25 deletions.
There are no files selected for viewing
47 changes: 47 additions & 0 deletions
47
pulsar-broker/src/main/java/org/apache/pulsar/broker/admin/v2/WorkerStats.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,47 @@ | ||
/** | ||
* Licensed to the Apache Software Foundation (ASF) under one | ||
* or more contributor license agreements. See the NOTICE file | ||
* distributed with this work for additional information | ||
* regarding copyright ownership. The ASF licenses this file | ||
* to you 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 org.apache.pulsar.broker.admin.v2; | ||
|
||
import java.io.IOException; | ||
|
||
import javax.ws.rs.GET; | ||
import javax.ws.rs.Path; | ||
import javax.ws.rs.core.Response; | ||
|
||
import org.apache.pulsar.functions.proto.InstanceCommunication.Metrics; | ||
import org.apache.pulsar.functions.worker.rest.FunctionApiResource; | ||
|
||
import io.swagger.annotations.ApiOperation; | ||
import io.swagger.annotations.ApiResponse; | ||
import io.swagger.annotations.ApiResponses; | ||
import lombok.extern.slf4j.Slf4j; | ||
|
||
@Slf4j | ||
@Path("/worker-stats") | ||
public class WorkerStats extends FunctionApiResource { | ||
|
||
@GET | ||
@Path("/functions") | ||
@ApiOperation(value = "Get metrics for all functions owned by worker", notes = "Requested should be executed by Monitoring agent on each worker to fetch the metrics", response = Metrics.class) | ||
@ApiResponses(value = { @ApiResponse(code = 403, message = "Don't have admin permission"), | ||
@ApiResponse(code = 503, message = "Worker service is not running") }) | ||
public Response getMetrics() throws IOException { | ||
return functions.getFunctionsMetrcis(clientAppId()); | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
35 changes: 35 additions & 0 deletions
35
pulsar-client-admin/src/main/java/org/apache/pulsar/client/admin/WorkerStats.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,35 @@ | ||
/** | ||
* Licensed to the Apache Software Foundation (ASF) under one | ||
* or more contributor license agreements. See the NOTICE file | ||
* distributed with this work for additional information | ||
* regarding copyright ownership. The ASF licenses this file | ||
* to you 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 org.apache.pulsar.client.admin; | ||
|
||
import org.apache.pulsar.functions.proto.InstanceCommunication.Metrics; | ||
|
||
/** | ||
* Admin interface for worker stats management. | ||
*/ | ||
public interface WorkerStats { | ||
|
||
|
||
/** | ||
* Get all functions stats on a worker | ||
* @return | ||
* @throws PulsarAdminException | ||
*/ | ||
Metrics getFunctionsStats() throws PulsarAdminException; | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
58 changes: 58 additions & 0 deletions
58
...r-client-admin/src/main/java/org/apache/pulsar/client/admin/internal/WorkerStatsImpl.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,58 @@ | ||
/** | ||
* Licensed to the Apache Software Foundation (ASF) under one | ||
* or more contributor license agreements. See the NOTICE file | ||
* distributed with this work for additional information | ||
* regarding copyright ownership. The ASF licenses this file | ||
* to you 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 org.apache.pulsar.client.admin.internal; | ||
|
||
import javax.ws.rs.ClientErrorException; | ||
import javax.ws.rs.client.WebTarget; | ||
import javax.ws.rs.core.Response; | ||
|
||
import org.apache.pulsar.client.admin.PulsarAdminException; | ||
import org.apache.pulsar.client.admin.WorkerStats; | ||
import org.apache.pulsar.client.api.Authentication; | ||
import org.apache.pulsar.functions.proto.InstanceCommunication.Metrics; | ||
import static org.apache.pulsar.client.admin.internal.FunctionsImpl.mergeJson; | ||
|
||
import lombok.extern.slf4j.Slf4j; | ||
|
||
@Slf4j | ||
public class WorkerStatsImpl extends BaseResource implements WorkerStats { | ||
|
||
private final WebTarget workerStats; | ||
|
||
public WorkerStatsImpl(WebTarget web, Authentication auth) { | ||
super(auth); | ||
this.workerStats = web.path("/admin/worker-stats"); | ||
} | ||
|
||
@Override | ||
public Metrics getFunctionsStats() throws PulsarAdminException { | ||
try { | ||
Response response = request(workerStats.path("functions")).get(); | ||
if (!response.getStatusInfo().equals(Response.Status.OK)) { | ||
throw new ClientErrorException(response); | ||
} | ||
String jsonResponse = response.readEntity(String.class); | ||
Metrics.Builder metricsBuilder = Metrics.newBuilder(); | ||
mergeJson(jsonResponse, metricsBuilder); | ||
return metricsBuilder.build(); | ||
} catch (Exception e) { | ||
throw getApiException(e); | ||
} | ||
} | ||
} |
79 changes: 79 additions & 0 deletions
79
pulsar-client-tools/src/main/java/org/apache/pulsar/admin/cli/CmdFunctionWorkerStats.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,79 @@ | ||
/** | ||
* Licensed to the Apache Software Foundation (ASF) under one | ||
* or more contributor license agreements. See the NOTICE file | ||
* distributed with this work for additional information | ||
* regarding copyright ownership. The ASF licenses this file | ||
* to you 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 org.apache.pulsar.admin.cli; | ||
|
||
import org.apache.pulsar.client.admin.PulsarAdmin; | ||
import org.apache.pulsar.client.api.PulsarClientException; | ||
import org.apache.pulsar.functions.utils.Utils; | ||
|
||
import com.beust.jcommander.Parameter; | ||
import com.beust.jcommander.Parameters; | ||
import com.google.gson.GsonBuilder; | ||
import com.google.gson.JsonParser; | ||
|
||
import lombok.Getter; | ||
import lombok.extern.slf4j.Slf4j; | ||
|
||
@Slf4j | ||
@Parameters(commandDescription = "Operations to collect function-worker statistics") | ||
public class CmdFunctionWorkerStats extends CmdBase { | ||
|
||
private final FunctionsStats functionsStats; | ||
|
||
/** | ||
* Base command | ||
*/ | ||
@Getter | ||
abstract class BaseCommand extends CliCommand { | ||
@Override | ||
void run() throws Exception { | ||
processArguments(); | ||
runCmd(); | ||
} | ||
|
||
void processArguments() throws Exception { | ||
} | ||
|
||
abstract void runCmd() throws Exception; | ||
} | ||
|
||
@Parameters(commandDescription = "dump all functions stats") | ||
class FunctionsStats extends BaseCommand { | ||
|
||
@Parameter(names = { "-i", "--indent" }, description = "Indent JSON output", required = false) | ||
boolean indent = false; | ||
|
||
@Override | ||
void runCmd() throws Exception { | ||
String json = Utils.printJson(admin.workerStats().getFunctionsStats()); | ||
GsonBuilder gsonBuilder = new GsonBuilder(); | ||
if (indent) { | ||
gsonBuilder.setPrettyPrinting(); | ||
} | ||
System.out.println(gsonBuilder.create().toJson(new JsonParser().parse(json))); | ||
} | ||
} | ||
|
||
public CmdFunctionWorkerStats(PulsarAdmin admin) throws PulsarClientException { | ||
super("functions", admin); | ||
functionsStats = new FunctionsStats(); | ||
jcommander.addCommand("functions", functionsStats); | ||
} | ||
|
||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.