/**
 * Copyright (C) 2011-2020 Red Hat, Inc. (https://github.com/Commonjava/indy)
 *
 * 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.
 * See the License for the specific language governing permissions and
 * limitations under the License.
 */
package org.commonjava.indy.subsys.newrelic;

import com.newrelic.telemetry.Attributes;
import com.newrelic.telemetry.spans.Span;
import org.commonjava.indy.subsys.newrelic.config.NewRelicConfiguration;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;

import javax.enterprise.context.ApplicationScoped;
import javax.inject.Inject;
import javax.servlet.Filter;
import javax.servlet.FilterChain;
import javax.servlet.FilterConfig;
import javax.servlet.ServletException;
import javax.servlet.ServletRequest;
import javax.servlet.ServletResponse;
import javax.servlet.http.HttpServletRequest;
import java.io.IOException;
import java.util.stream.Stream;

import static org.apache.commons.lang3.StringUtils.isBlank;
import static org.commonjava.indy.metrics.RequestContextHelper.getContext;

@ApplicationScoped
public class NewRelicFilter
                implements Filter
{
    @Inject
    private NewRelicManager manager;

    @Inject
    private NewRelicConfiguration config;

    private final Logger logger = LoggerFactory.getLogger( getClass() );

    @Override
    public void init( final FilterConfig filterConfig ) throws ServletException
    {
    }

    @Override
    public void doFilter( final ServletRequest request, final ServletResponse response, final FilterChain chain )
                    throws IOException, ServletException
    {
        logger.trace( "START: {}", getClass().getSimpleName() );

        HttpServletRequest hsr = (HttpServletRequest) request;
        Span rootSpan = null;
        Attributes attrs = new Attributes();
        try
        {
            rootSpan = manager.startRootTracer( getEndpointName( hsr.getMethod(), hsr.getPathInfo() ) );
            if ( rootSpan != null )
            {
                attrs.put( "path_info", hsr.getPathInfo() );
            }

            chain.doFilter( request, response );
        }
        finally
        {
            if ( rootSpan != null )
            {
                Stream.of( config.getFields()).forEach( field->{
                    Object value = getContext( field );
                    if ( value != null )
                    {
                        logger.trace( "NEW RELIC FIELD: {} = {}", field, value );
                        if ( value instanceof Number)
                        {
                            attrs.put( field, (Number) value );
                        }
                        else
                        {
                            attrs.put( field, String.valueOf( value ) );
                        }
                    }
                });

                manager.closeSpans( attrs );
            }

            logger.trace( "END: {}", getClass().getSimpleName() );
        }
    }

    private String getEndpointName( String method, String pathInfo )
    {
        StringBuilder sb = new StringBuilder( method + "_" );
        String[] toks = pathInfo.split( "/" );
        for ( String s : toks )
        {
            if ( isBlank( s ) || "api".equals( s ) )
            {
                continue;
            }
            sb.append( s );
            if ( "admin".equals( s ) )
            {
                sb.append( "_" );
            }
            else
            {
                break;
            }
        }
        return sb.toString();
    }

    @Override
    public void destroy()
    {
    }

}

@@ -2592,641 +2592,8 @@
   %7B%0A
-                Stream.of( config.getFields()).forEach( field-%3E%7B%0A                    Object value = getContext( field );%0A                    if ( value != null )%0A                    %7B%0A                        logger.trace( %22NEW RELIC FIELD: %7B%7D = %7B%7D%22, field, value );%0A                        if ( value instanceof Number)%0A                        %7B%0A                            attrs.put( field, (Number) value );%0A                        %7D%0A                        else%0A                        %7B%0A                            attrs.put( field, String.valueOf( value ) );%0A                        %7D%0A                    %7D%0A                %7D);%0A%0A
     

check before sending batch, push field extraction to manager, use Dequeue instead of List