Skip to content

Commit

Permalink
select logs by app id in v2 (#3760)
Browse files Browse the repository at this point in the history
  • Loading branch information
Feroze Mohideen authored Oct 9, 2023
1 parent e4ae73b commit 7f337b6
Show file tree
Hide file tree
Showing 12 changed files with 77 additions and 67 deletions.
20 changes: 15 additions & 5 deletions api/server/handlers/porter_app/logs_apply_v2.go
Original file line number Diff line number Diff line change
Expand Up @@ -14,6 +14,7 @@ import (
"github.com/porter-dev/porter/api/server/shared"
"github.com/porter-dev/porter/api/server/shared/apierrors"
"github.com/porter-dev/porter/api/server/shared/config"
"github.com/porter-dev/porter/api/server/shared/requestutils"
"github.com/porter-dev/porter/api/types"
porter_agent "github.com/porter-dev/porter/internal/kubernetes/porter_agent/v2"
"github.com/porter-dev/porter/internal/models"
Expand Down Expand Up @@ -42,7 +43,7 @@ func NewAppLogsHandler(
type AppLogsRequest struct {
DeploymentTargetID string `schema:"deployment_target_id"`
ServiceName string `schema:"service_name"`
AppName string `schema:"app_name"`
AppID uint `schema:"app_id"`
Limit uint `schema:"limit"`
StartRange time.Time `schema:"start_range,omitempty"`
EndRange time.Time `schema:"end_range,omitempty"`
Expand All @@ -53,6 +54,7 @@ type AppLogsRequest struct {

const (
lokiLabel_PorterAppName = "porter_run_app_name"
lokiLabel_PorterAppID = "porter_run_app_id"
lokiLabel_PorterServiceName = "porter_run_service_name"
lokiLabel_PorterAppRevisionID = "porter_run_app_revision_id"
lokiLabel_DeploymentTargetId = "porter_run_deployment_target_id"
Expand All @@ -74,12 +76,19 @@ func (c *AppLogsHandler) ServeHTTP(w http.ResponseWriter, r *http.Request) {
return
}

if request.AppName == "" {
err := telemetry.Error(ctx, span, nil, "must provide app name")
appName, reqErr := requestutils.GetURLParamString(r, types.URLParamPorterAppName)
if reqErr != nil {
err := telemetry.Error(ctx, span, reqErr, "porter app name not found in request")
c.HandleAPIError(w, r, apierrors.NewErrPassThroughToClient(err, http.StatusBadRequest))
return
}
telemetry.WithAttributes(span, telemetry.AttributeKV{Key: "app-name", Value: appName})

if request.AppID == 0 {
err := telemetry.Error(ctx, span, nil, "must provide app id")
c.HandleAPIError(w, r, apierrors.NewErrPassThroughToClient(err, http.StatusBadRequest))
return
}
telemetry.WithAttributes(span, telemetry.AttributeKV{Key: "app-name", Value: request.AppName})

if request.ServiceName == "" {
err := telemetry.Error(ctx, span, nil, "must provide service name")
Expand Down Expand Up @@ -148,7 +157,8 @@ func (c *AppLogsHandler) ServeHTTP(w http.ResponseWriter, r *http.Request) {

matchLabels := map[string]string{
lokiLabel_Namespace: namespace,
lokiLabel_PorterAppName: request.AppName,
lokiLabel_PorterAppName: appName,
lokiLabel_PorterAppID: fmt.Sprintf("%d", request.AppID),
}

if request.ServiceName != "all" {
Expand Down
11 changes: 7 additions & 4 deletions api/server/handlers/porter_app/stream_logs.go
Original file line number Diff line number Diff line change
Expand Up @@ -16,6 +16,7 @@ import (
"github.com/porter-dev/porter/api/server/shared"
"github.com/porter-dev/porter/api/server/shared/apierrors"
"github.com/porter-dev/porter/api/server/shared/config"
"github.com/porter-dev/porter/api/server/shared/requestutils"
"github.com/porter-dev/porter/api/server/shared/websocket"
"github.com/porter-dev/porter/api/types"
"github.com/porter-dev/porter/internal/models"
Expand Down Expand Up @@ -54,12 +55,13 @@ func (c *StreamLogsLokiHandler) ServeHTTP(w http.ResponseWriter, r *http.Request
return
}

if request.AppName == "" {
err := telemetry.Error(ctx, span, nil, "must provide app name")
appName, reqErr := requestutils.GetURLParamString(r, types.URLParamPorterAppName)
if reqErr != nil {
err := telemetry.Error(ctx, span, reqErr, "porter app name not found in request")
c.HandleAPIError(w, r, apierrors.NewErrPassThroughToClient(err, http.StatusBadRequest))
return
}
telemetry.WithAttributes(span, telemetry.AttributeKV{Key: "app-name", Value: request.AppName})
telemetry.WithAttributes(span, telemetry.AttributeKV{Key: "app-name", Value: appName})

if request.ServiceName == "" {
err := telemetry.Error(ctx, span, nil, "must provide service name")
Expand Down Expand Up @@ -126,8 +128,9 @@ func (c *StreamLogsLokiHandler) ServeHTTP(w http.ResponseWriter, r *http.Request

labels := []string{
fmt.Sprintf("%s=%s", lokiLabel_Namespace, namespace),
fmt.Sprintf("%s=%s", lokiLabel_PorterAppName, request.AppName),
fmt.Sprintf("%s=%s", lokiLabel_PorterAppName, appName),
fmt.Sprintf("%s=%s", lokiLabel_DeploymentTargetId, request.DeploymentTargetID),
fmt.Sprintf("%s=%s", lokiLabel_PorterAppID, fmt.Sprintf("%d", request.AppID)),
}

if request.ServiceName != "all" {
Expand Down
4 changes: 2 additions & 2 deletions api/server/router/porter_app.go
Original file line number Diff line number Diff line change
Expand Up @@ -891,7 +891,7 @@ func getPorterAppRoutes(
Router: r,
})

// GET /api/projects/{project_id}/clusters/{cluster_id}/apps/logs -> cluster.NewAppLogsHandler
// GET /api/projects/{project_id}/clusters/{cluster_id}/apps/{porter_app_name}/logs -> cluster.NewAppLogsHandler
appLogsEndpoint := factory.NewAPIEndpoint(
&types.APIRequestMetadata{
Verb: types.APIVerbGet,
Expand Down Expand Up @@ -920,7 +920,7 @@ func getPorterAppRoutes(
Router: r,
})

// GET /api/projects/{project_id}/clusters/{cluster_id}/apps/logs/loki -> namespace.NewStreamLogsLokiHandler
// GET /api/projects/{project_id}/clusters/{cluster_id}/apps/{porter_app_name}/logs/loki -> namespace.NewStreamLogsLokiHandler
streamLogsLokiEndpoint := factory.NewAPIEndpoint(
&types.APIRequestMetadata{
Verb: types.APIVerbGet,
Expand Down
1 change: 1 addition & 0 deletions dashboard/src/lib/hooks/useJobs.ts
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,7 @@ const jobRunValidator = z.object({
labels: z.object({
"porter.run/app-revision-id": z.string(),
"porter.run/service-name": z.string(),
"porter.run/app-id": z.string(),
}),
creationTimestamp: z.string(),
uid: z.string(),
Expand Down
1 change: 1 addition & 0 deletions dashboard/src/main/home/app-dashboard/app-view/AppView.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -12,6 +12,7 @@ import AppHeader from "./AppHeader";
import { LatestRevisionProvider } from "./LatestRevisionContext";

export const porterAppValidator = z.object({
id: z.number(),
name: z.string(),
git_branch: z.string().optional(),
git_repo_id: z.number().optional(),
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,7 @@ import Logs from "../../validate-apply/logs/Logs"
import { useLatestRevision } from "../LatestRevisionContext";

const LogsTab: React.FC = () => {
const { projectId, clusterId, latestProto, deploymentTarget } = useLatestRevision();
const { projectId, clusterId, latestProto, deploymentTarget, porterApp } = useLatestRevision();

const appName = latestProto.name
const serviceNames = Object.keys(latestProto.services)
Expand All @@ -16,6 +16,7 @@ const LogsTab: React.FC = () => {
serviceNames={serviceNames}
deploymentTargetId={deploymentTarget.id}
filterPredeploy={true}
appId={porterApp.id}
/>
);
};
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -36,7 +36,7 @@ const AppEventCard: React.FC<Props> = ({ event, deploymentTargetId, projectId, c
{
start_range: dayjs(event.created_at).subtract(1, 'minute').toISOString(),
end_range: dayjs(event.updated_at).add(1, 'minute').toISOString(),
app_name: event.metadata.app_name,
app_id: event.porter_app_id,
service_name: event.metadata.service_name,
deployment_target_id: deploymentTargetId,
limit: 1000,
Expand All @@ -45,7 +45,7 @@ const AppEventCard: React.FC<Props> = ({ event, deploymentTargetId, projectId, c
{
project_id: projectId,
cluster_id: clusterId,
app_name: appName,
porter_app_name: appName,
}
)

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -19,7 +19,7 @@ type Props = {
const PreDeployEventFocusView: React.FC<Props> = ({
event,
}) => {
const { projectId, clusterId, latestProto, deploymentTarget } = useLatestRevision();
const { projectId, clusterId, latestProto, deploymentTarget, porterApp } = useLatestRevision();

const appName = latestProto.name
const serviceNames = [`${latestProto.name}-predeploy`]
Expand Down Expand Up @@ -66,6 +66,7 @@ const PreDeployEventFocusView: React.FC<Props> = ({
deploymentTargetId={deploymentTarget.id}
appRevisionId={event.metadata.app_revision_id}
logFilterNames={["service_name"]}
appId={porterApp.id}
/>
</>
);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -74,6 +74,7 @@ const JobRunDetails: React.FC<Props> = ({
startTime: dayjs(jobRun.status.startTime ?? jobRun.metadata.creationTimestamp).subtract(30, 'second'),
endTime: jobRun.status.completionTime != null ? dayjs(jobRun.status.completionTime).add(30, 'second') : undefined,
}}
appId={parseInt(jobRun.metadata.labels["porter.run/app-id"])}
/>
</>
);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -42,6 +42,7 @@ type Props = {
endTime?: Dayjs;
};
filterPredeploy?: boolean;
appId: number;
};

const Logs: React.FC<Props> = ({
Expand All @@ -54,6 +55,7 @@ const Logs: React.FC<Props> = ({
timeRange,
logFilterNames = ["service_name", "revision", "output_stream"],
filterPredeploy = false,
appId,
}) => {
const { search } = useLocation();
const queryParams = new URLSearchParams(search);
Expand Down Expand Up @@ -194,6 +196,7 @@ const Logs: React.FC<Props> = ({
appRevisionId,
filterPredeploy,
timeRange,
appID: appId,
});

useEffect(() => {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -56,6 +56,7 @@ export const useLogs = ({
appRevisionId = "",
timeRange,
filterPredeploy,
appID,
}: {
projectID: number,
clusterID: number,
Expand All @@ -75,6 +76,7 @@ export const useLogs = ({
endTime?: Dayjs,
},
filterPredeploy: boolean,
appID: number,
}
) => {
const [isLive, setIsLive] = useState<boolean>(!setDate && (timeRange?.startTime == null && timeRange?.endTime == null));
Expand Down Expand Up @@ -180,11 +182,11 @@ export const useLogs = ({
const websocketBaseURL = `/api/projects/${projectID}/clusters/${clusterID}/apps/${appName}/logs/loki`;

const searchParams = {
app_name: appName,
service_name: serviceName,
deployment_target_id: deploymentTargetId,
search_param: searchParam,
app_revision_id: appRevisionId,
app_id: appID.toString(),
}

const q = new URLSearchParams(searchParams).toString();
Expand Down Expand Up @@ -272,7 +274,7 @@ export const useLogs = ({
}> => {
try {
const getLogsReq = {
app_name: appName,
app_id: appID,
service_name: serviceName,
deployment_target_id: deploymentTargetId,
search_param: searchParam,
Expand Down
Loading

0 comments on commit 7f337b6

Please sign in to comment.