# Java

{% hint style="warning" %}
This guide is for existing <mark style="color:red;">**JAVA applications that have been build**</mark> on Cocktail Cloud.
{% endhint %}

## 1. Log Appender Setting

Log Appender is an interface provided by a logging framework or library that provides the ability to collect and process log messages. OpenTelemetry interacts with Log Appender through the Log Bridge API to collect log messages and associate them with tracking data from OpenTelemetry. Therefore, log appenders can be used to collect and integrate log data from OpenTelemetry.

We introduce how to collect data using `logback` and `log4j`, which are representative loggers.

### 1) logback

#### 1-1) dependency addition

{% tabs %}
{% tab title="Maven" %}

```
<dependency>
    <groupId>io.opentelemetry.instrumentation</groupId>
    <artifactId>opentelemetry-logback-appender-1.0</artifactId>
    <version>2.0.0-alpha</version>
    <scope>runtime</scope>
</dependency>
```

{% endtab %}

{% tab title="Gradle" %}

```
runtimeOnly group: 'io.opentelemetry.instrumentation', name: 'opentelemetry-logback-appender-1.0', version: '2.0.0-alpha'
```

{% endtab %}
{% endtabs %}

#### 1-2) logback.xml setting

```
    ...

    <appender name="OpenTelemetry"
              class="io.opentelemetry.instrumentation.logback.appender.v1_0.OpenTelemetryAppender">
    </appender>

    ...
 
    <logger name="OTLP" additivity="false">
        <appender-ref ref="OpenTelemetry"/>
        <level value="INFO"/>
    </logger>
```

### 2) log4J

#### 2-1) dependency addition

{% tabs %}
{% tab title="Maven" %}

```
<dependency>
    <groupId>io.opentelemetry.instrumentation</groupId>
    <artifactId>opentelemetry-log4j-appender-2.17</artifactId>
    <version>OPENTELEMETRY_VERSION</version>
    <scope>runtime</scope>
  </dependency>
</dependencies>
```

{% endtab %}

{% tab title="Gradle" %}

```
runtimeOnly("io.opentelemetry.instrumentation:opentelemetry-log4j-appender-2.17:OPENTELEMETRY_VERSION")
```

{% endtab %}
{% endtabs %}

#### 2-2) log4j.xml setting

```java
<Configuration status="WARN" packages="io.opentelemetry.instrumentation.log4j.appender.v2_17">
  <Appenders>
    <Console name="Console" target="SYSTEM_OUT">
      <PatternLayout
          pattern="%d{HH:mm:ss.SSS} [%t] %-5level %logger{36} trace_id: %X{trace_id} span_id: %X{span_id} trace_flags: %X{trace_flags} - %msg%n"/>
    </Console>
    <OpenTelemetry name="OpenTelemetryAppender"/>
  </Appenders>
  <Loggers>
    <Root>
      <AppenderRef ref="OpenTelemetryAppender" level="All"/>
      <AppenderRef ref="Console" level="All"/>
    </Root>
  </Loggers>
</Configuration>
```

### 3) Logger setting

```java
private static final Logger logger = LoggerFactory.getLogger("OTLP");
// Logging with a logger called OTLP. This can be changed
// If you set it to 'ALL' in the above document, you can log with console and opentelemetry.
```

## 2. Image creation via SDK

### 1) Build/Pipeline - Build details of the Java application for which you want to collect logs from the build.

### 2) Click “Image Build Task” to edit

<figure><img src="https://4196990571-files.gitbook.io/~/files/v0/b/gitbook-x-prod.appspot.com/o/spaces%2FL5As9l8R3RbLkQd6XsfL%2Fuploads%2FQNvuWReVnElsxTpsWCFn%2Fimage.png?alt=media&#x26;token=3ae5e27b-eaf7-408c-b3f7-40614f41d855" alt=""><figcaption></figcaption></figure>

### 3) Setting environment variables and downloading SDK of Opentelemetry-java-instrumentation

<figure><img src="https://4196990571-files.gitbook.io/~/files/v0/b/gitbook-x-prod.appspot.com/o/spaces%2FL5As9l8R3RbLkQd6XsfL%2Fuploads%2FXfkSck6ExEAjencV6Odf%2Fimage.png?alt=media&#x26;token=7eb3a35b-ef48-4a13-a35b-1f190977560f" alt=""><figcaption></figcaption></figure>

```
...

# Required settings to export log
ENV OTEL_EXPORTER_OTLP_ENDPOINT=$OTEL_EXPORTER_OTLP_ENDPOINT
ENV OTEL_EXPORTER_OTLP_LOGS_ENDPOINT=$OTEL_EXPORTER_OTLP_LOGS_ENDPOINT
ENV OTEL_EXPORTER_OTLP_LOGS_HEADERS=$OTEL_EXPORTER_OTLP_LOGS_HEADERS
ENV OTEL_LOGS_EXPORTER=$OTEL_LOGS_EXPORTER
ENV OTEL_SERVICE_NAME=$OTEL_SERVICE_NAME

# None processing to not collect metrics and traces
ENV OTEL_METRICS_EXPORTER=none
ENV OTEL_TRACES_EXPORTER=none


# Download sdk
RUN wget -q https://github.com/open-telemetry/opentelemetry-java-instrumentation/releases/download/v2.0.0/opentelemetry-javaagent.jar
ENTRYPOINT ["java", "-javaagent:opentelemetry-javaagent.jar", "-jar", "app.jar"]

...
```

## 3. Container Setting

### 1) Logging - Copy the token of the application created in Application Management

<figure><img src="https://4196990571-files.gitbook.io/~/files/v0/b/gitbook-x-prod.appspot.com/o/spaces%2FL5As9l8R3RbLkQd6XsfL%2Fuploads%2FpQIkESEywJ9hUDvRTKxw%2Fimage.png?alt=media&#x26;token=af27edcc-55c4-4c64-b263-a2dc0a515d60" alt=""><figcaption></figcaption></figure>

### 2) In Container Details - Settings - Environment Variables tab, set environment variables as follows

<figure><img src="https://4196990571-files.gitbook.io/~/files/v0/b/gitbook-x-prod.appspot.com/o/spaces%2FL5As9l8R3RbLkQd6XsfL%2Fuploads%2FkVdoyIZmxXtNgUkdwrl0%2Fimage.png?alt=media&#x26;token=b4d5ba53-b3fa-4558-932f-726831ddbac5" alt=""><figcaption></figcaption></figure>

```yaml
apiVersion: apps/v1
kind: Deployment
...
spec:
...
  template:
    spec:
      containers:
      - env:
	# opentelemetry collector svc address (eg. http://log-agent-cocktail-log-agent.cocktail-addon:4318)
	- name: OTEL_EXPORTER_OTLP_ENDPOINT
          value: {log-agent Service address}:4318

	# Log service address of opentelemetry collector (eg. http://log-agent-cocktail-log-agent.cocktail-addon:4318/v1/logs)
        - name: OTEL_EXPORTER_OTLP_LOGS_ENDPOINT
          value: {log-agent Service address}:4318/v1/logs

	# Settings using opelemetry protocol
        - name: OTEL_LOGS_EXPORTER
          value: otlp
        
	# Application Name
	- name: OTEL_SERVICE_NAME
          value: {Application Name}
        
        # Settings for authentication
        - name: OTEL_EXPORTER_OTLP_LOGS_HEADERS
          value: app_token={Application Token},app_name={Application Name}        
        
        image: {java-application image}
        imagePullPolicy: Always
        ...
```

**`log-agent Service Address`** : Infrastructure - Cluster - Add-ons - Click 'log-agent' and check the service name.

( http port = 4318 , grpc port = 4317)

<figure><img src="https://4196990571-files.gitbook.io/~/files/v0/b/gitbook-x-prod.appspot.com/o/spaces%2FL5As9l8R3RbLkQd6XsfL%2Fuploads%2FtOuT0mIfjZM2O3QUBz26%2Fimage.png?alt=media&#x26;token=a8247216-073b-4985-900b-ba07776cfbc0" alt=""><figcaption></figcaption></figure>

## 4. Check Application Log Check

### 1) Logging - Application Log - Search for the application you set in the application list.

<figure><img src="https://4196990571-files.gitbook.io/~/files/v0/b/gitbook-x-prod.appspot.com/o/spaces%2FL5As9l8R3RbLkQd6XsfL%2Fuploads%2FNYoYzKFD2QC9WkV5SvTO%2Fimage.png?alt=media&#x26;token=e2fd39cf-9d25-4cfe-9fda-9ad125bf9ff7" alt=""><figcaption></figcaption></figure>
