-
Notifications
You must be signed in to change notification settings - Fork 8.3k
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
[Onboarding][OTel Host] Use MOTel collector on Serverless
- Loading branch information
1 parent
b26d85b
commit f38f31a
Showing
11 changed files
with
191 additions
and
22 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
65 changes: 65 additions & 0 deletions
65
...rvability_onboarding/public/application/quickstart_flows/otel_logs/use_encoded_api_key.ts
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,65 @@ | ||
/* | ||
* Copyright Elasticsearch B.V. and/or licensed to Elasticsearch B.V. under one | ||
* or more contributor license agreements. Licensed under the Elastic License | ||
* 2.0; you may not use this file except in compliance with the Elastic License | ||
* 2.0. | ||
*/ | ||
|
||
import { useEffect, useState } from 'react'; | ||
import { useKibana } from '@kbn/kibana-react-plugin/public'; | ||
import type { HttpSetup, IHttpFetchError, ResponseErrorBody } from '@kbn/core/public'; | ||
import type { ObservabilityOnboardingAppServices } from '../../..'; | ||
|
||
interface OnboardingApiKeyResponse { | ||
apiKeyEncoded: string; | ||
} | ||
interface APMApiKeyResponse { | ||
agentKey: { name: string; encoded: string }; | ||
} | ||
|
||
export function useEncodedApiKey() { | ||
const [encodedApiKey, setEncodedApiKey] = useState<string | null>(null); | ||
const [error, setError] = useState<IHttpFetchError<ResponseErrorBody> | null>(null); | ||
|
||
const { | ||
services: { | ||
http, | ||
context: { isServerless }, | ||
}, | ||
} = useKibana<ObservabilityOnboardingAppServices>(); | ||
|
||
useEffect(() => { | ||
requestEncodedApiKey(isServerless, http) | ||
.then((apiKey) => { | ||
setEncodedApiKey(apiKey); | ||
}) | ||
.catch((err) => { | ||
setError(err); | ||
}); | ||
}, [http, isServerless]); | ||
|
||
return { encodedApiKey, error }; | ||
} | ||
|
||
function requestEncodedApiKey(isServerless: boolean, http: HttpSetup): Promise<string> { | ||
let requestPromise: Promise<string>; | ||
|
||
if (isServerless) { | ||
const timestamp = new Date().toISOString(); | ||
|
||
requestPromise = http | ||
.post<APMApiKeyResponse>('/api/apm/agent_keys', { | ||
body: JSON.stringify({ | ||
name: `ingest-otel-host-${timestamp}`, | ||
privileges: ['event:write'], | ||
}), | ||
}) | ||
.then((res) => res.agentKey.encoded); | ||
} else { | ||
requestPromise = http | ||
.post<OnboardingApiKeyResponse>('/internal/observability_onboarding/otel/api_key') | ||
.then((res) => res.apiKeyEncoded); | ||
} | ||
|
||
return requestPromise; | ||
} |
57 changes: 57 additions & 0 deletions
57
...onboarding/public/application/quickstart_flows/otel_logs/use_ingest_endpoint_url.test.tsx
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,57 @@ | ||
/* | ||
* Copyright Elasticsearch B.V. and/or licensed to Elasticsearch B.V. under one | ||
* or more contributor license agreements. Licensed under the Elastic License | ||
* 2.0; you may not use this file except in compliance with the Elastic License | ||
* 2.0. | ||
*/ | ||
|
||
import React from 'react'; | ||
import { renderHook } from '@testing-library/react'; | ||
import { KibanaContextProvider } from '@kbn/kibana-react-plugin/public'; | ||
import { useIngestEndpointUrl } from './use_ingest_endpoint_url'; | ||
|
||
const createWrapper = ({ isServerless }: { isServerless: boolean }) => { | ||
return function Wrapper({ children }: React.PropsWithChildren) { | ||
return ( | ||
<KibanaContextProvider | ||
services={{ | ||
context: { isServerless }, | ||
}} | ||
> | ||
{children} | ||
</KibanaContextProvider> | ||
); | ||
}; | ||
}; | ||
|
||
describe('useIngestEndpointUrl', () => { | ||
it('returns ES endpoint when not on Serverless', () => { | ||
const { result } = renderHook( | ||
() => | ||
useIngestEndpointUrl({ | ||
elasticsearchUrl: 'http://elasticsearch', | ||
managedServiceUrl: 'https://e2e-tests-c045db.apm.us-east-1.aws.elastic.cloud:443', | ||
}), | ||
{ | ||
wrapper: createWrapper({ isServerless: false }), | ||
} | ||
); | ||
|
||
expect(result.current).toBe('http://elasticsearch'); | ||
}); | ||
|
||
it('returns APM endpoint with replaced sub-domain when on Serverless', () => { | ||
const { result } = renderHook( | ||
() => | ||
useIngestEndpointUrl({ | ||
elasticsearchUrl: 'http://elasticsearch', | ||
managedServiceUrl: 'https://e2e-tests-c045db.apm.us-east-1.aws.elastic.cloud:443', | ||
}), | ||
{ | ||
wrapper: createWrapper({ isServerless: true }), | ||
} | ||
); | ||
|
||
expect(result.current).toBe('https://e2e-tests-c045db.ingest.us-east-1.aws.elastic.cloud:443'); | ||
}); | ||
}); |
34 changes: 34 additions & 0 deletions
34
...ility_onboarding/public/application/quickstart_flows/otel_logs/use_ingest_endpoint_url.ts
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,34 @@ | ||
/* | ||
* Copyright Elasticsearch B.V. and/or licensed to Elasticsearch B.V. under one | ||
* or more contributor license agreements. Licensed under the Elastic License | ||
* 2.0; you may not use this file except in compliance with the Elastic License | ||
* 2.0. | ||
*/ | ||
|
||
import { useKibana } from '@kbn/kibana-react-plugin/public'; | ||
import { ObservabilityOnboardingAppServices } from '../../..'; | ||
|
||
interface Props { | ||
elasticsearchUrl?: string; | ||
managedServiceUrl?: string; | ||
} | ||
|
||
export function useIngestEndpointUrl({ elasticsearchUrl, managedServiceUrl }: Props): string { | ||
const { | ||
services: { | ||
context: { isServerless }, | ||
}, | ||
} = useKibana<ObservabilityOnboardingAppServices>(); | ||
|
||
if (!elasticsearchUrl || !managedServiceUrl) { | ||
return ''; | ||
} | ||
|
||
return isServerless ? convertApmUrlToOtelUrl(managedServiceUrl) : elasticsearchUrl; | ||
} | ||
|
||
function convertApmUrlToOtelUrl(apmUrl: string): string { | ||
const urlParts = apmUrl.split('.'); | ||
|
||
return `${urlParts[0]}.ingest.${urlParts.slice(2).join('.')}`; | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters