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

Change Download JSON button to be a normal link with href. #3040

Merged
merged 3 commits into from
Mar 25, 2020
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
3 changes: 3 additions & 0 deletions zipkin-lens/README.md
Original file line number Diff line number Diff line change
Expand Up @@ -19,6 +19,9 @@ Open [http://localhost:3000](http://localhost:3000) to view it in the browser.
The page will reload if you make edits.<br />
You will also see any lint errors in the console.

By default, API requests are proxied to `http://localhost:9411`. You can change this target using
the `API_BASE` environment variable, e.g., `API_BASE=http://tracing.company.com npm start`.

### `npm test`

Launches the test runner in the interactive watch mode.<br />
Expand Down
4 changes: 2 additions & 2 deletions zipkin-lens/package.json
Original file line number Diff line number Diff line change
Expand Up @@ -51,6 +51,7 @@
"eslint-plugin-react-hooks": "^1.6.1",
"fetch-mock": "^7.3.0",
"history": "^4.10.1",
"http-proxy-middleware": "^1.0.3",
"lodash": "^4.17.14",
"moment": "^2.24.0",
"mvy": "0.2.1",
Expand Down Expand Up @@ -99,6 +100,5 @@
"last 1 firefox version",
"last 1 safari version"
]
},
"proxy": "http://localhost:9411"
}
}
26 changes: 7 additions & 19 deletions zipkin-lens/src/components/TracePage/TraceSummaryHeader.jsx
Original file line number Diff line number Diff line change
Expand Up @@ -106,6 +106,10 @@ const TraceSummaryHeader = React.memo(({ traceSummary, rootSpanIndex }) => {
? config.logsUrl.replace(/{traceId}/g, traceSummary.traceId)
: undefined;

const traceJsonUrl = traceSummary
? `${api.TRACE}/${traceSummary.traceId}`
: undefined;

const archivePostUrl =
config.archivePostUrl && traceSummary ? config.archivePostUrl : undefined;

Expand Down Expand Up @@ -180,24 +184,6 @@ const TraceSummaryHeader = React.memo(({ traceSummary, rootSpanIndex }) => {
});
}, [archivePostUrl, archiveUrl, traceSummary, enqueueSnackbar]);

const handleSaveButtonClick = useCallback(() => {
if (!traceSummary || !traceSummary.traceId) {
return;
}
fetch(`${api.TRACE}/${traceSummary.traceId}`)
.then((resp) => resp.blob())
.then((blob) => {
const a = document.createElement('a');
a.href = window.URL.createObjectURL(blob);
a.download = `${traceSummary.traceId}.json`;
a.click();
// See: https://stackoverflow.com/questions/30694453/blob-createobjecturl-download-not-working-in-firefox-but-works-when-debugging
setTimeout(() => {
window.URL.revokeObjectURL(a.href);
}, 100);
});
}, [traceSummary]);

const traceInfo = traceSummary ? (
<Box className={classes.traceInfo}>
{[
Expand Down Expand Up @@ -255,7 +241,9 @@ const TraceSummaryHeader = React.memo(({ traceSummary, rootSpanIndex }) => {
<Button
variant="outlined"
className={classes.actionButton}
onClick={handleSaveButtonClick}
href={traceJsonUrl}
download={traceSummary && `${traceSummary.traceId}.json`}
data-testid="download-json-link"
>
<FontAwesomeIcon
icon={faDownload}
Expand Down
22 changes: 22 additions & 0 deletions zipkin-lens/src/components/TracePage/TraceSummaryHeader.test.jsx
Original file line number Diff line number Diff line change
Expand Up @@ -19,6 +19,28 @@ import render from '../../test/util/render-with-default-settings';
import TraceSummaryHeader from './TraceSummaryHeader';

describe('<TraceSummaryHeader />', () => {
it('renders download JSON button', () => {
const { getByTestId } = render(
<TraceSummaryHeader
traceSummary={{
traceId: '1',
spans: [],
serviceNameAndSpanCounts: [],
duration: 1,
durationStr: '1μs',
rootSpan: {
serviceName: 'service-A',
spanName: 'span-A',
},
}}
/>,
);
const downloadJson = getByTestId('download-json-link');
expect(downloadJson).toBeInTheDocument();
expect(downloadJson).toHaveAttribute('href', '/zipkin/api/v2/trace/1');
expect(downloadJson).toHaveAttribute('download', '1.json');
});

it('does not render View Logs link with default config', () => {
const { queryByTestId } = render(
<TraceSummaryHeader
Expand Down
28 changes: 28 additions & 0 deletions zipkin-lens/src/setupProxy.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,28 @@
/*
* Copyright 2015-2020 The OpenZipkin Authors
*
* Licensed under the Apache License, Version 2.0 (the "License"); you may not use this file except
* in compliance with the License. You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software distributed under the License
* is distributed on an "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express
* or implied. See the License for the specific language governing permissions and limitations under
* the License.
*/

const { createProxyMiddleware } = require('http-proxy-middleware');

const API_BASE = process.env.API_BASE || 'http://localhost:9411';

// Default create-react-app proxy only proxies AJAX requests by looking at Accept headers. We want
// to proxy any request though, mainly to support the Download JSON button.
const proxy = createProxyMiddleware(['**/api/**', '**/config.json'], {
target: API_BASE,
changeOrigin: true,
});

module.exports = (app) => {
app.use('/', proxy);
};