|
| 1 | +# OpenTelemetry Java Agent Extension for SAP Cloud Logging |
| 2 | + |
| 3 | +This module provides an extension for the [OpenTelemetry Java Agent](https://opentelemetry.io/docs/instrumentation/java/automatic/). |
| 4 | +The extension scans the service bindings of an application for SAP Cloud Logging. |
| 5 | +If such a binding is found, the OpenTelemetry Java Agent is configured to ship observability data to that service. |
| 6 | +Thus, this extension provides a convenient auto-instrumentation for Java applications running on SAP BTP. |
| 7 | + |
| 8 | +The extension provides two main features: |
| 9 | + |
| 10 | +* auto-configuration of the OpenTelemetry connection to SAP Cloud Logging |
| 11 | +* adding resource attributes to describe the CF application |
| 12 | + |
| 13 | +See the section on [configuration](#configuration) for further details. |
| 14 | + |
| 15 | +## Quickstart Guide |
| 16 | + |
| 17 | +Any Java application can be instrumented with the OpenTelemetry Java Agent and this extension by adding the following arguments to the java command: |
| 18 | + |
| 19 | +```sh |
| 20 | +java -javaagent:/path/to/opentelemetry-javaagent-<version>.jar \ |
| 21 | + -Dotel.javaagent-extensions=/path/to/cf-java-logging-support-opentelemetry-agent-extension-<versions>.jar \ |
| 22 | + # your Java application command |
| 23 | +``` |
| 24 | + |
| 25 | +If you are using Spring Boot, you can bundle both dependencies with the application. |
| 26 | +See the Maven pom of the [Spring Boot sample application](../sample-spring-boot/pom.xml) for details. |
| 27 | +When deployed to a Cloud Foundry runtime environment, the Spring Boot jar is expanded, so that the agent and extension jar are available during application start. |
| 28 | +In that case, the following Java arguments are required: |
| 29 | + |
| 30 | +```sh |
| 31 | +java -javaagent:BOOT-INF/lib/opentelemetry-javaagent-<version>.jar \ |
| 32 | + -Dotel.javaagent.extensions=BOOT-INF/lib/cf-java-logging-support-opentelemetry-agent-extension-<version>.jar \ |
| 33 | + # your Java application command |
| 34 | +``` |
| 35 | + |
| 36 | +See the [example manifest](../sample-spring-boot/manifest-otel-javaagent.yml), how this translates into a deployment description. |
| 37 | + |
| 38 | +For the instrumentation to send observability data to SAP Cloud Logging, the application needs to be bound to a corresponding service instance. |
| 39 | +The service instance can be either managed or [user-provided](#using-user-provided-service-instances). |
| 40 | + |
| 41 | +Note, that the OpenTelemetry Java Agent currently only sends traces and metrics by default. |
| 42 | +To enable logs, the additional property `-Dotel.logs.exporter=otlp` is required. |
| 43 | + |
| 44 | +## Configuration |
| 45 | + |
| 46 | +The OpenTelemetry Java Agent supports a wide variety of [configuration options](https://opentelemetry.io/docs/instrumentation/java/automatic/agent-config/). |
| 47 | +As the extension provides configuration via SPI, all its configuration takes lower precedence than other configuration options for OpenTelemetry. |
| 48 | +Users can easily overwrite any setting using environment variables or system properties. |
| 49 | + |
| 50 | +### Configuring the Extension |
| 51 | + |
| 52 | +The extension itself can be configured by specifying the following system properties: |
| 53 | + |
| 54 | +| Property | Default Value | Comment | |
| 55 | +|----------|---------------|---------| |
| 56 | +| `com.sap.otel.extension.cloud-logging.label` | `cloud-logging` | The label of the managed service binding to bind to. | |
| 57 | +| `com.sap.otel.extension.cloud-logging.tag` | `Cloud Logging` | The tag of any service binding (managed or user-provided) to bind to. | |
| 58 | +| `otel.javaagent.extension.sap.cf.resource.enabled` or `env(OTEL_JAVAAGENT_EXTENSION_SAP_CF_RESOURCE_ENABLED)` | `true` | Whether to add CF resource attributes to all events. | |
| 59 | + |
| 60 | +The extension will scan the environment variable `VCAP_SERVICES` for CF service bindings. |
| 61 | +User-provided bindings will take precedence over managed bindings of the configured label ("cloud-logging" by default). |
| 62 | +All matching bindings are filtered for the configured tag ("Cloud Logging" by default). |
| 63 | +The first binding will be taken for configuration for the OpenTelemetry exporter. |
| 64 | +Preferring user-provided services over managed service instances allows better control of the binding properties, e.g. syslog drains. |
| 65 | + |
| 66 | +### Recommended Agent Configuration |
| 67 | + |
| 68 | +The OpenTelemetry Java Agent offers a lot of configuration options. |
| 69 | +The following set of properties is recommended to be used with the extension: |
| 70 | + |
| 71 | +```sh |
| 72 | +java -javaagent:/path/to/opentelemetry-javaagent-<version>.jar \ |
| 73 | + -Dotel.javaagent-extensions=/path/to/cf-java-logging-support-opentelemetry-agent-extension-<versions>.jar \ |
| 74 | + # enable logs \ |
| 75 | + -Dotel.logs.exporter=otlp \ |
| 76 | + # reroute agent logs to otlp \ |
| 77 | + -Dotel.javaagent.logging=application |
| 78 | + # configure logback context \ |
| 79 | + -Dotel.instrumentation.logback-appender.experimental.capture-mdc-attributes=* \ |
| 80 | + -Dotel.instrumentation.logback-appender.experimental.capture-key-value-pair-attributes=true \ |
| 81 | + -Dotel.instrumentation.logback-appender.experimental.capture-code-attributes=true \ |
| 82 | + -Dotel.instrumentation.logback-appender.experimental-log-attributes=true \ |
| 83 | + # Disable large resource attributes |
| 84 | + -Dotel.experimental.resource.disabled-keys=process.command_line,process.command_args,process.executable.path |
| 85 | +``` |
| 86 | + |
| 87 | +The [OpenTelemetry Java Instrumentation project](https://github.com/open-telemetry/opentelemetry-java-instrumentation) provides detailed documentation on the configuration properties for [Logback](https://github.com/open-telemetry/opentelemetry-java-instrumentation/tree/main/instrumentation/logback/logback-appender-1.0/javaagent) and [Log4j](https://github.com/open-telemetry/opentelemetry-java-instrumentation/tree/main/instrumentation/log4j/log4j-appender-2.17/javaagent). |
| 88 | + |
| 89 | +## Using User-Provided Service Instances |
| 90 | + |
| 91 | +The extension provides support not only for managed service instance of SAP Cloud Logging but also for user-provided service instances. |
| 92 | +This helps to fine-tune the configuration, e.g. leave out or reconfigure the syslog drain. |
| 93 | +Furthermore, this helps on sharing service instances across CF orgs or landscapes. |
| 94 | + |
| 95 | +The extension requires four fields in the user-provided service credentials and needs to be tagged with the `com.sap.otel.extension.cloud-logging.tag` (default: `Cloud Logging`) documented in section [Configuration](#configuration). |
| 96 | + |
| 97 | +| Field name | Contents | |
| 98 | +|------------|---------| |
| 99 | +| `ingest-otlp-endpoint` | The OTLP endpoint including port. It will be prefixed with `https://`. | |
| 100 | +| `ingest-otlp-key` | The mTLS client key in PCKS#8 format. Line breaks as `\n`. | |
| 101 | +| `ingest-otlp-cert`| The mTLS client certificate in PEM format matching the client key. Line breaks as `\n`. | |
| 102 | +| `server-ca` | The trusted mTLS server certificate in PEM format. Line breaks as `\n`. | |
| 103 | + |
| 104 | +If you have a SAP Cloud Logging service key, you can generate the required JSON file with jq: |
| 105 | + |
| 106 | +```bash |
| 107 | +cf service-key cls test \ |
| 108 | +| tail -n +2 \ |
| 109 | +| jq '.credentials | {"ingest-otlp-endpoint":."ingest-otlp-endpoint", "ingest-otlp-cert":."ingest-otlp-cert", "ingest-otlp-key":."ingest-otlp-key", "server-ca":."server-ca"}' \ |
| 110 | +> ups.json |
| 111 | +``` |
| 112 | + |
| 113 | +Using this file, you can create the required user-provided service: |
| 114 | + |
| 115 | +```bash |
| 116 | + cf cups <your-service-name> -p ups.json -t "Cloud Logging" |
| 117 | +``` |
| 118 | + |
| 119 | +Note, that you can easily feed arbitrary credentials to the extension. |
| 120 | +It does not need to be SAP Cloud Logging. |
| 121 | +You can even change the tag using the configuration parameters of the extension. |
0 commit comments