0

以应用管理员身份登录时,我的 Google App Engine 应用是否可以调用 API,该 API 会返回有关我的应用返回的错误数(404、500 等)的信息?

我想在我的应用程序中设置一个简单的 cron 作业,以计算我的应用程序每隔几分钟返回的错误数,并在错误率变得异常高时向我发送电子邮件。我想避免不得不从 Appspot 仪表板中抓取信息或在我的应用程序之外运行另一个进程。

4

2 回答 2

1

最接近您需要的可能是LogService API

请注意,它不适用于 Java 运行时(我假设)。

于 2012-04-09T08:51:45.800 回答
0

这仅适用于,并且对 GAE 友好。

您将需要 jsoup。

package some.package


import java.util.HashMap;
import java.util.Map;

import org.jsoup.Connection;
import org.jsoup.Connection.Method;
import org.jsoup.Connection.Response;
import org.jsoup.Jsoup;
import org.jsoup.nodes.Document;
import org.jsoup.nodes.Element;
import org.jsoup.select.Elements;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;

public class AppEngineScraperUtil
{
    private static final Logger logger = LoggerFactory.getLogger( AppEngineScraperUtil.class );

    /**
     * @param appId
     *            in the form of {@code appId}
     * @return dashboard in html
     */
    public static String fetchDashboard( String appId )
    {
        return jsoupWay( appId );
    }

    private static String jsoupWay( String appId )
    {
        try
        {
            Connection conn = createGetConn( "https://appengine.google.com" );
            Response result = conn.execute();
            Document doc = result.parse();
            // parse inputs
            Elements elements = doc.select( "#dsh, [name=GALX], #service, #continue, #ltmpl" );
            Map<String, String> formFields = new HashMap<String, String>();
            // build form
            for ( Element element: elements )
                formFields.put( element.attr( "name" ), element.val() );
            formFields.put( "Email", "xxx" );
            formFields.put( "Passwd", "xxx" );
            String formAction = doc.select( "form" ).first().attr( "action" );
            // parse cookies
            Map<String, String> cookies = result.cookies();
            // build post
            conn = createPostConn( formAction );
            conn.cookies( cookies );
            conn.data( formFields );
            conn.header( "Content-Type", "application/x-www-form-urlencoded" );
            result = conn.execute();
            doc = result.parse();
            // get dashboard
            conn = createGetConn( "https://appengine.google.com/dashboard?&app_id=" + appId );
            conn.cookies( result.cookies() );
            result = conn.execute();
            // return html
            doc = result.parse();
            return doc.toString();
        }
        catch ( Exception e )
        {
            logger.error( "Error retrieving dashboard.", e );
        }
        return null;
    }

    private static Connection createPostConn( String url )
    {
        Connection conn = Jsoup.connect( url );
        conn.method( Method.POST );
        return conn;
    }

    private static Connection createGetConn( String url )
    {
        Connection conn = Jsoup.connect( url );
        conn.method( Method.GET );
        return conn;
    }
}
于 2013-01-29T07:41:40.230 回答