# WithOtelAgent

- Module: [Aspire.Hosting.Java](/reference/api/go/aspire.hosting.java.md)
- Version: `17.0.0-preview.1.26565.2`
- Kind: `method`
- Defined on: [`JavaAppResource`](/reference/api/go/aspire.hosting.java/javaappresource.md)
- Source: [GitHub](https://github.com/microsoft/aspire/tree/bcfa151cdca6d0a573f22d5d9c23f9c727cb3b2f)

Runs the application with the OpenTelemetry Java agent so it exports traces, metrics, and logs to Aspire.

## Remarks

The agent is not downloaded. Obtain it as a build dependency, or from
https://github.com/open-telemetry/opentelemetry-java-instrumentation/releases, and point this at the
resulting file. The OTLP exporter is configured by `AddJavaApp` regardless of whether an agent
is used, so call this only when you want the agent's automatic instrumentation.
A relative `agentPath` is resolved against the application directory and made absolute
when running locally. This is required, not cosmetic: `JAVA_TOOL_OPTIONS` is inherited by every JVM
started beneath the resource, and build tools start JVMs whose working directory is not the application
directory. The Gradle daemon, for example, starts from its own distribution directory, so a relative
`-javaagent:` path fails to resolve and the daemon dies during VM initialization with
"Error opening zip file or JAR manifest missing".
A relative path names a file the build produces, so Aspire runs that build as its own resource in run
mode and holds the application until it finishes. An absolute path names a file that exists
independently of the build, so no build resource is added.
In publish mode a relative path is rewritten to the location the generated Dockerfile copies the
agent to, because the path has to be interpreted inside the container rather than on the build
machine. An absolute path is emitted unchanged, since it cannot have come from the build context
and must be supplied by the base image or a mount.

## Definition

```go
func (JavaAppResource) WithOtelAgent(agentPath string) JavaAppResource
```

## Parameters

- `agentPath` (`string`) -- The path to the `opentelemetry-javaagent.jar` file.

## Returns

[`JavaAppResource`](/reference/api/go/aspire.hosting.java/javaappresource.md) -- A reference to the `IResourceBuilder`.

Returns a handle; failures surface later via `Err()`.

## Applies to

- [`JavaAppResource`](/reference/api/go/aspire.hosting.java/javaappresource.md)
