Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Set Eureka InstanceInfo to endpoint attribute #6056

Open
minwoox opened this issue Jan 7, 2025 · 1 comment · May be fixed by #6069
Open

Set Eureka InstanceInfo to endpoint attribute #6056

minwoox opened this issue Jan 7, 2025 · 1 comment · May be fixed by #6069

Comments

@minwoox
Copy link
Contributor

minwoox commented Jan 7, 2025

Users might want to use the metadata from the Eureka InstanceInfo but currently, there's no way to retrieve it.

I think we can probably set the InstanceInfo to the Endpoint as an attribute so that users can retrieve it later.

public final class InstanceInfo {

    private static final AttributeKey<InstanceInfo> INSTANCE_INFO = AttributeKey.valueOf(
            InstanceInfo.class, "INSTANCE_INFO");

    public static InstanceInfo get(Endpoint endpoint) {
        return endpoint.attr(INSTANCE_INFO);
    }

    public static Endpoint with(Endpoint endpoint, InstanceInfo instanceInfo) {
        return endpoint.with(INSTANCE_INFO, instanceInfo);
    }
}

private static Endpoint endpoint(InstanceInfo instanceInfo, boolean secureVip) {
final String hostname = instanceInfo.getHostName();
final PortWrapper portWrapper = instanceInfo.getPort();
final int port;
if (secureVip || !portWrapper.isEnabled()) {
port = instanceInfo.getSecurePort().getPort();
} else {
port = portWrapper.getPort();
}
assert hostname != null;
Endpoint endpoint = Endpoint.of(hostname, port);
final String ipAddr = instanceInfo.getIpAddr();
if (ipAddr != null && hostname != ipAddr) {
endpoint = endpoint.withIpAddr(ipAddr);
}
return endpoint;
}

@Ivan-Montes
Copy link

Hi, I asked you about this matter on Discord some days ago. Maybe I can help with the resolution if possible.
I’ve reviewed some related old issues and pull requests but since this could be my very first contribution, I would like to share the approach before submitting a PR if you don't mind.

Modifications:

  • Add a helper class to hide the implementation detail.
  • Set the InstanceInfo to the Endpoint as an attribute.
private static final class EurekaInstanceInfoUtil {

        private static final AttributeKey<InstanceInfo> INSTANCE_INFO = AttributeKey.valueOf(
                EurekaInstanceInfoUtil.class, "INSTANCE_INFO");

        @Nullable
        static InstanceInfo get(Endpoint endpoint) {
            requireNonNull(endpoint, "endpoint");
            return endpoint.attr(INSTANCE_INFO);
        }

        static Endpoint with(Endpoint endpoint, InstanceInfo instanceInfo) {
            requireNonNull(endpoint, "endpoint");
            requireNonNull(instanceInfo, "instanceInfo");
            return endpoint.withAttr(INSTANCE_INFO, instanceInfo);
        }
    }

    private static Endpoint endpoint(InstanceInfo instanceInfo, boolean secureVip) {
        final String hostname = instanceInfo.getHostName();
        final PortWrapper portWrapper = instanceInfo.getPort();
        final int port;
        if (secureVip || !portWrapper.isEnabled()) {
            port = instanceInfo.getSecurePort().getPort();
        } else {
            port = portWrapper.getPort();
        }

        assert hostname != null;
        Endpoint endpoint = Endpoint.of(hostname, port);
        final String ipAddr = instanceInfo.getIpAddr();
        if (ipAddr != null && hostname != ipAddr) {
            endpoint = endpoint.withIpAddr(ipAddr);
        }
        return EurekaInstanceInfoUtil.with(endpoint, instanceInfo);
    }

Thanks.

Ivan-Montes added a commit to Ivan-Montes/armeria that referenced this issue Jan 13, 2025
Related: line#6056

Motivation:

Users might want to use the metadata from the Eureka `InstanceInfo` but currently, there's no way to retrieve it.

Modifications:

- Add a helper class to hide the implementation detail.
- Set the `InstanceInfo` to the `Endpoint` as an attribute.

Result:

- Closes line#6056
- Now users can retrieve it.
@Ivan-Montes Ivan-Montes linked a pull request Jan 13, 2025 that will close this issue
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Projects
None yet
Development

Successfully merging a pull request may close this issue.

2 participants