func()

in cli/azd/cmd/monitor.go [99:174]


func (m *monitorAction) Run(ctx context.Context) (*actions.ActionResult, error) {
	if !m.flags.monitorLive && !m.flags.monitorLogs && !m.flags.monitorOverview {
		m.flags.monitorOverview = true
	}

	if m.env.GetSubscriptionId() == "" {
		return nil, errors.New(
			"infrastructure has not been provisioned. Run `azd provision`",
		)
	}

	aspireDashboard := apphost.AspireDashboardUrl(ctx, m.env, m.alphaFeaturesManager)
	if aspireDashboard != nil {
		openWithDefaultBrowser(ctx, m.console, aspireDashboard.Link)
		return nil, nil
	}

	resourceGroups, err := m.resourceManager.GetResourceGroupsForEnvironment(ctx, m.env.GetSubscriptionId(), m.env.Name())
	if err != nil {
		return nil, fmt.Errorf("discovering resource groups from deployment: %w", err)
	}

	var insightsResources []*azapi.ResourceExtended
	var portalResources []*azapi.ResourceExtended

	for _, resourceGroup := range resourceGroups {
		resources, err := m.resourceService.ListResourceGroupResources(
			ctx, azure.SubscriptionFromRID(resourceGroup.Id), resourceGroup.Name, nil)
		if err != nil {
			return nil, fmt.Errorf("listing resources: %w", err)
		}

		for _, resource := range resources {
			switch resource.Type {
			case string(azapi.AzureResourceTypePortalDashboard):
				portalResources = append(portalResources, resource)
			case string(azapi.AzureResourceTypeAppInsightComponent):
				insightsResources = append(insightsResources, resource)
			}
		}
	}

	if len(insightsResources) == 0 && (m.flags.monitorLive || m.flags.monitorLogs) {
		return nil, fmt.Errorf("application does not contain an Application Insights resource")
	}

	if len(portalResources) == 0 && m.flags.monitorOverview {
		return nil, fmt.Errorf("application does not contain an Application Insights dashboard")
	}

	tenantId, err := m.subResolver.LookupTenant(ctx, m.env.GetSubscriptionId())
	if err != nil {
		return nil, err
	}

	for _, insightsResource := range insightsResources {
		if m.flags.monitorLive {
			openWithDefaultBrowser(ctx, m.console,
				fmt.Sprintf("%s/#@%s/resource%s/quickPulse", m.portalUrlBase, tenantId, insightsResource.Id))
		}

		if m.flags.monitorLogs {
			openWithDefaultBrowser(ctx, m.console,
				fmt.Sprintf("%s/#@%s/resource%s/logs", m.portalUrlBase, tenantId, insightsResource.Id))
		}
	}

	for _, portalResource := range portalResources {
		if m.flags.monitorOverview {
			openWithDefaultBrowser(ctx, m.console,
				fmt.Sprintf("%s/#@%s/dashboard/arm%s", m.portalUrlBase, tenantId, portalResource.Id))
		}
	}

	return nil, nil
}