|  | 
|  | 1 | +/* | 
|  | 2 | + * Copyright The OpenTelemetry Authors | 
|  | 3 | + * SPDX-License-Identifier: Apache-2.0 | 
|  | 4 | + */ | 
|  | 5 | + | 
|  | 6 | +package io.opentelemetry.opamp.client.internal; | 
|  | 7 | + | 
|  | 8 | +import io.opentelemetry.opamp.client.internal.response.MessageData; | 
|  | 9 | +import opamp.proto.AgentDescription; | 
|  | 10 | +import opamp.proto.RemoteConfigStatus; | 
|  | 11 | +import opamp.proto.ServerErrorResponse; | 
|  | 12 | + | 
|  | 13 | +public interface OpampClient { | 
|  | 14 | + | 
|  | 15 | +  /** | 
|  | 16 | +   * Starts the client and begin attempts to connect to the Server. Once connection is established | 
|  | 17 | +   * the client will attempt to maintain it by reconnecting if the connection is lost. All failed | 
|  | 18 | +   * connection attempts will be reported via {@link Callbacks#onConnectFailed(OpampClient, | 
|  | 19 | +   * Throwable)} callback. | 
|  | 20 | +   * | 
|  | 21 | +   * <p>This method does not wait until the connection to the Server is established and will likely | 
|  | 22 | +   * return before the connection attempts are even made. | 
|  | 23 | +   * | 
|  | 24 | +   * <p>This method may be called only once. | 
|  | 25 | +   * | 
|  | 26 | +   * @param callbacks The Callback to which the Client will notify about any Server requests and | 
|  | 27 | +   *     responses. | 
|  | 28 | +   */ | 
|  | 29 | +  void start(Callbacks callbacks); | 
|  | 30 | + | 
|  | 31 | +  /** | 
|  | 32 | +   * Stops the client. May be called only after {@link #start(Callbacks)}. May be called only once. | 
|  | 33 | +   * After this call returns successfully it is guaranteed that no callbacks will be called. Once | 
|  | 34 | +   * stopped, the client cannot be started again. | 
|  | 35 | +   */ | 
|  | 36 | +  void stop(); | 
|  | 37 | + | 
|  | 38 | +  /** | 
|  | 39 | +   * Sets attributes of the Agent. The attributes will be included in the next status report sent to | 
|  | 40 | +   * the Server. When called after {@link #start(Callbacks)}, the attributes will be included in the | 
|  | 41 | +   * next outgoing status report. This is typically used by Agents which allow their | 
|  | 42 | +   * AgentDescription to change dynamically while the OpAMPClient is started. May be also called | 
|  | 43 | +   * from {@link Callbacks#onMessage(OpampClient, MessageData)}. | 
|  | 44 | +   * | 
|  | 45 | +   * @param agentDescription The new agent description. | 
|  | 46 | +   */ | 
|  | 47 | +  void setAgentDescription(AgentDescription agentDescription); | 
|  | 48 | + | 
|  | 49 | +  /** | 
|  | 50 | +   * Sets the current remote config status which will be sent in the next agent to server request. | 
|  | 51 | +   * | 
|  | 52 | +   * @param remoteConfigStatus The new remote config status. | 
|  | 53 | +   */ | 
|  | 54 | +  void setRemoteConfigStatus(RemoteConfigStatus remoteConfigStatus); | 
|  | 55 | + | 
|  | 56 | +  interface Callbacks { | 
|  | 57 | +    /** | 
|  | 58 | +     * Called when the connection is successfully established to the Server. May be called after | 
|  | 59 | +     * {@link #start(Callbacks)} is called and every time a connection is established to the Server. | 
|  | 60 | +     * For WebSocket clients this is called after the handshake is completed without any error. For | 
|  | 61 | +     * HTTP clients this is called for any request if the response status is OK. | 
|  | 62 | +     * | 
|  | 63 | +     * @param client The relevant {@link OpampClient} instance. | 
|  | 64 | +     */ | 
|  | 65 | +    void onConnect(OpampClient client); | 
|  | 66 | + | 
|  | 67 | +    /** | 
|  | 68 | +     * Called when the connection to the Server cannot be established. May be called after {@link | 
|  | 69 | +     * #start(Callbacks)} is called and tries to connect to the Server. May also be called if the | 
|  | 70 | +     * connection is lost and reconnection attempt fails. | 
|  | 71 | +     * | 
|  | 72 | +     * @param client The relevant {@link OpampClient} instance. | 
|  | 73 | +     * @param throwable The exception. | 
|  | 74 | +     */ | 
|  | 75 | +    void onConnectFailed(OpampClient client, Throwable throwable); | 
|  | 76 | + | 
|  | 77 | +    /** | 
|  | 78 | +     * Called when the Server reports an error in response to some previously sent request. Useful | 
|  | 79 | +     * for logging purposes. The Agent should not attempt to process the error by reconnecting or | 
|  | 80 | +     * retrying previous operations. The client handles the ErrorResponse_UNAVAILABLE case | 
|  | 81 | +     * internally by performing retries as necessary. | 
|  | 82 | +     * | 
|  | 83 | +     * @param client The relevant {@link OpampClient} instance. | 
|  | 84 | +     * @param errorResponse The error returned by the Server. | 
|  | 85 | +     */ | 
|  | 86 | +    void onErrorResponse(OpampClient client, ServerErrorResponse errorResponse); | 
|  | 87 | + | 
|  | 88 | +    /** | 
|  | 89 | +     * Called when the Agent receives a message that needs processing. See {@link MessageData} | 
|  | 90 | +     * definition for the data that may be available for processing. During onMessage execution the | 
|  | 91 | +     * {@link OpampClient} functions that change the status of the client may be called, e.g. if | 
|  | 92 | +     * RemoteConfig is processed then {@link #setRemoteConfigStatus(opamp.proto.RemoteConfigStatus)} | 
|  | 93 | +     * should be called to reflect the processing result. These functions may also be called after | 
|  | 94 | +     * onMessage returns. This is advisable if processing can take a long time. In that case | 
|  | 95 | +     * returning quickly is preferable to avoid blocking the {@link OpampClient}. | 
|  | 96 | +     * | 
|  | 97 | +     * @param client The relevant {@link OpampClient} instance. | 
|  | 98 | +     * @param messageData The server response data that needs processing. | 
|  | 99 | +     */ | 
|  | 100 | +    void onMessage(OpampClient client, MessageData messageData); | 
|  | 101 | +  } | 
|  | 102 | +} | 
0 commit comments