/*
 * Copyright 2019 NAVER Corp.
 *
 * 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 com.navercorp.pinpoint.profiler.sender.grpc;

import java.util.Objects;
import java.util.concurrent.atomic.AtomicLong;

import com.navercorp.pinpoint.grpc.StatusError;
import com.navercorp.pinpoint.grpc.StatusErrors;
import io.grpc.stub.ClientCallStreamObserver;
import io.grpc.stub.ClientResponseObserver;
import org.apache.logging.log4j.Logger;
import org.apache.logging.log4j.LogManager;

/**
 * @author Woonduk Kang(emeroad)
 */
public class ResponseStreamObserver<ReqT, ResT> implements ClientResponseObserver<ReqT, ResT> {

    private final Logger logger = LogManager.getLogger(this.getClass());

    private final StreamEventListener<ReqT> listener;

    public ResponseStreamObserver(StreamEventListener<ReqT> listener) {
        this.listener = Objects.requireNonNull(listener, "listener");
    }

    @Override
    public void beforeStart(final ClientCallStreamObserver<ReqT> requestStream) {
        logger.info("beforeStart {}", listener);
        requestStream.setOnReadyHandler(new Runnable() {
            private final AtomicLong isReadyCounter = new AtomicLong(0);

            @Override
            public void run() {
                final long isReadyCount = isReadyCounter.incrementAndGet();
                logger.info("onReadyHandler {} isReadyCount:{}", listener, isReadyCount);
                if (isReadyCount == 1) {
                    listener.start(requestStream);
                }
            }
        });
    }

    @Override
    public void onNext(ResT value) {
        if (logger.isDebugEnabled()) {
            logger.debug("{} onNext:{}", listener, value);
        }
    }

    @Override
    public void onError(Throwable t) {
        final StatusError statusError = StatusErrors.throwable(t);
        if (statusError.isSimpleError()) {
            logger.info("Failed to stream, name={}, cause={}", listener, statusError.getMessage());
        } else {
            logger.info("Failed to stream, name={}, cause={}", listener, statusError.getMessage(), statusError.getThrowable());
        }
        listener.onError(t);
    }

    @Override
    public void onCompleted() {
        logger.warn("{} onCompleted", listener);
        listener.onCompleted();

    }

    @Override
    public String toString() {
        return "ResponseStreamObserver{" +
                "name=" + listener +
                '}';
    }
}
@@ -2725,12 +2725,12 @@
 ger.
-warn
+info
 (%22%7B%7D
@@ -2787,17 +2787,16 @@
 eted();%0A
-%0A
     %7D%0A%0A 

Change agent's gRPC stream log level