Skip to content

Commit

Permalink
Onboarding Docs - Copy to clipboard (SigNoz#3634)
Browse files Browse the repository at this point in the history
* feat: enable copy-to-clipboard to onboarding docs snippets

* feat: remove commented code & <br></br> from md docs

* feat: remove react-copy-to-clipboard lib and fix type issues

* feat: markdown renderer - pre - remove any with reactnode
  • Loading branch information
YounixM authored and manishm committed Sep 27, 2023
1 parent 67ae23a commit 620e2c3
Show file tree
Hide file tree
Showing 34 changed files with 563 additions and 282 deletions.
4 changes: 4 additions & 0 deletions frontend/package.json
Original file line number Diff line number Diff line change
Expand Up @@ -84,9 +84,11 @@
"react-helmet-async": "1.3.0",
"react-i18next": "^11.16.1",
"react-intersection-observer": "9.4.1",
"react-markdown": "8.0.7",
"react-query": "^3.34.19",
"react-redux": "^7.2.2",
"react-router-dom": "^5.2.0",
"react-syntax-highlighter": "15.5.0",
"react-use": "^17.3.2",
"react-virtuoso": "4.0.3",
"redux": "^4.0.5",
Expand Down Expand Up @@ -150,6 +152,7 @@
"@types/react-redux": "^7.1.11",
"@types/react-resizable": "3.0.3",
"@types/react-router-dom": "^5.1.6",
"@types/react-syntax-highlighter": "15.5.7",
"@types/styled-components": "^5.1.4",
"@types/uuid": "^8.3.1",
"@types/webpack": "^5.28.0",
Expand Down Expand Up @@ -183,6 +186,7 @@
"lint-staged": "^12.5.0",
"portfinder-sync": "^0.0.2",
"prettier": "2.2.1",
"raw-loader": "4.0.2",
"react-hooks-testing-library": "0.6.0",
"react-hot-loader": "^4.13.0",
"react-resizable": "3.0.4",
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,34 @@
.code-snippet-container {
position: relative;
background-color: rgb(43, 43, 43);
}

.code-copy-btn {
position: absolute;
top: 8px;
right: 8px;
display: flex;
justify-content: flex-end;
align-items: center;

button {
cursor: pointer;

background-color: rgba($color: #1d1d1d, $alpha: 0.7);
color: white;
border: none;
padding: 8px;
border-radius: 3px;
transition: all 0.1s;

&:hover {
background-color: rgba($color: #1d1d1d, $alpha: 1);
}
}

&.copied {
button {
background-color: rgba($color: #52c41a, $alpha: 1);
}
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,32 @@
import './CodeCopyBtn.scss';

import { CheckOutlined, CopyOutlined } from '@ant-design/icons';
import cx from 'classnames';
import { useState } from 'react';

export default function CodeCopyBtn({
children,
}: {
children: React.ReactNode;
}): JSX.Element {
const [isSnippetCopied, setIsSnippetCopied] = useState(false);

const handleClick = (): void => {
if (children && Array.isArray(children)) {
setIsSnippetCopied(true);
navigator.clipboard.writeText(children[0].props.children[0]).finally(() => {
setTimeout(() => {
setIsSnippetCopied(false);
}, 1000);
});
}
};

return (
<div className={cx('code-copy-btn', isSnippetCopied ? 'copied' : '')}>
<button type="button" onClick={handleClick}>
{!isSnippetCopied ? <CopyOutlined /> : <CheckOutlined />}
</button>
</div>
);
}
43 changes: 43 additions & 0 deletions frontend/src/components/MarkdownRenderer/MarkdownRenderer.tsx
Original file line number Diff line number Diff line change
@@ -0,0 +1,43 @@
/* eslint-disable react/jsx-props-no-spreading */
import { CodeProps } from 'react-markdown/lib/ast-to-react';
import { Prism as SyntaxHighlighter } from 'react-syntax-highlighter';
import { a11yDark } from 'react-syntax-highlighter/dist/cjs/styles/prism';

import CodeCopyBtn from './CodeCopyBtn/CodeCopyBtn';

function Pre({ children }: { children: React.ReactNode }): JSX.Element {
return (
<pre className="code-snippet-container">
<CodeCopyBtn>{children}</CodeCopyBtn>
{children}
</pre>
);
}

function Code({
node,
inline,
className = 'blog-code',
children,
...props
}: CodeProps): JSX.Element {
const match = /language-(\w+)/.exec(className || '');
return !inline && match ? (
<SyntaxHighlighter
// eslint-disable-next-line @typescript-eslint/ban-ts-comment
// @ts-ignore
style={a11yDark}
language={match[1]}
PreTag="div"
{...props}
>
{String(children).replace(/\n$/, '')}
</SyntaxHighlighter>
) : (
<code className={className} {...props}>
{children}
</code>
);
}

export { Code, Pre };
14 changes: 10 additions & 4 deletions frontend/src/container/OnboardingContainer/APM/GoLang/GoLang.tsx
Original file line number Diff line number Diff line change
@@ -1,8 +1,9 @@
import './GoLang.styles.scss';

import { MDXProvider } from '@mdx-js/react';
import { Form, Input } from 'antd';
import { Code, Pre } from 'components/MarkdownRenderer/MarkdownRenderer';
import Header from 'container/OnboardingContainer/common/Header/Header';
import ReactMarkdown from 'react-markdown';

import ConnectionStatus from '../common/ConnectionStatus/ConnectionStatus';
import GoLangDocs from './goLang.md';
Expand Down Expand Up @@ -44,9 +45,14 @@ export default function GoLang({
</div>

<div className="content-container">
<MDXProvider>
<GoLangDocs />
</MDXProvider>
<ReactMarkdown
components={{
pre: Pre,
code: Code,
}}
>
{GoLangDocs}
</ReactMarkdown>
</div>
</div>
)}
Expand Down
46 changes: 23 additions & 23 deletions frontend/src/container/OnboardingContainer/APM/GoLang/goLang.md
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,7 @@ From VMs, there are two ways to send data to SigNoz Cloud.

#### **Send traces directly to SigNoz Cloud**

1. **Install Dependencies**<br></br>
1. **Install Dependencies**

Dependencies related to OpenTelemetry exporter and SDK have to be installed first. Note that we are assuming you are using `gin` request router. If you are using other request routers, check out the [corresponding package](#request-routers).

Expand All @@ -24,7 +24,7 @@ From VMs, there are two ways to send data to SigNoz Cloud.
go.opentelemetry.io/otel/exporters/otlp/otlptrace/otlptracegrpc
```

2. **Declare environment variables for configuring OpenTelemetry**<br></br>
2. **Declare environment variables for configuring OpenTelemetry**

Declare the following global variables in `main.go` which we will use to configure OpenTelemetry:

Expand All @@ -36,7 +36,7 @@ From VMs, there are two ways to send data to SigNoz Cloud.
)
```

3. **Instrument your Go application with OpenTelemetry**<br></br>
3. **Instrument your Go application with OpenTelemetry**

To configure your application to send data we will need a function to initialize OpenTelemetry. Add the following snippet of code in your `main.go` file.

Expand Down Expand Up @@ -98,7 +98,7 @@ From VMs, there are two ways to send data to SigNoz Cloud.
}
```
4. **Initialize the tracer in main.go**<br></br>
4. **Initialize the tracer in main.go**
Modify the main function to initialise the tracer in `main.go`. Initiate the tracer at the very beginning of our main function.
Expand All @@ -111,7 +111,7 @@ From VMs, there are two ways to send data to SigNoz Cloud.
}
```
5. **Add the OpenTelemetry Gin middleware**<br></br>
5. **Add the OpenTelemetry Gin middleware**
Configure Gin to use the middleware by adding the following lines in `main.go`.
Expand All @@ -129,7 +129,7 @@ From VMs, there are two ways to send data to SigNoz Cloud.
}
```
6. **Set environment variables and run your Go Gin application**<br></br>
6. **Set environment variables and run your Go Gin application**
The run command must have some environment variables to send data to SigNoz cloud. The run command:
Expand All @@ -146,11 +146,11 @@ From VMs, there are two ways to send data to SigNoz Cloud.
`OTEL_EXPORTER_OTLP_ENDPOINT`: ingest.{region}.signoz.cloud:443. Depending on the choice of your region for SigNoz cloud, the ingest endpoint will vary accordingly.
US - ingest.us.signoz.cloud:443 <br></br>
US - ingest.us.signoz.cloud:443
IN - ingest.in.signoz.cloud:443 <br></br>
IN - ingest.in.signoz.cloud:443
EU - ingest.eu.signoz.cloud:443 <br></br>
EU - ingest.eu.signoz.cloud:443
---
Expand All @@ -161,7 +161,7 @@ OTel Collector binary helps to collect logs, hostmetrics, resource and infra att
You can find instructions to install OTel Collector binary [here](https://signoz.io/docs/tutorial/opentelemetry-binary-usage-in-virtual-machine/) in your VM. Once you are done setting up your OTel Collector binary, you can follow the below steps for instrumenting your Golang application.
1. **Install Dependencies**<br></br>
1. **Install Dependencies**
Dependencies related to OpenTelemetry exporter and SDK have to be installed first. Note that we are assuming you are using `gin` request router. If you are using other request routers, check out the [corresponding package](#request-routers).
Expand All @@ -176,7 +176,7 @@ You can find instructions to install OTel Collector binary [here](https://signoz
go.opentelemetry.io/otel/exporters/otlp/otlptrace/otlptracegrpc
```
2. **Declare environment variables for configuring OpenTelemetry**<br></br>
2. **Declare environment variables for configuring OpenTelemetry**
Declare the following global variables in `main.go` which we will use to configure OpenTelemetry:
Expand All @@ -188,7 +188,7 @@ You can find instructions to install OTel Collector binary [here](https://signoz
)
```
3. **Instrument your Go application with OpenTelemetry**<br></br>
3. **Instrument your Go application with OpenTelemetry**
To configure your application to send data we will need a function to initialize OpenTelemetry. Add the following snippet of code in your `main.go` file.
Expand Down Expand Up @@ -249,7 +249,7 @@ You can find instructions to install OTel Collector binary [here](https://signoz
return exporter.Shutdown
}
4. **Initialize the tracer in main.go**<br></br>
4. **Initialize the tracer in main.go**
Modify the main function to initialise the tracer in `main.go`. Initiate the tracer at the very beginning of our main function.
Expand All @@ -262,7 +262,7 @@ You can find instructions to install OTel Collector binary [here](https://signoz
}
```
5. **Add the OpenTelemetry Gin middleware**<br></br>
5. **Add the OpenTelemetry Gin middleware**
Configure Gin to use the middleware by adding the following lines in `main.go`.
Expand All @@ -280,15 +280,15 @@ You can find instructions to install OTel Collector binary [here](https://signoz
}
```
6. **Set environment variables and run your Go Gin application**<br></br>
6. **Set environment variables and run your Go Gin application**
The run command must have some environment variables to send data to SigNoz. The run command:
```bash
SERVICE_NAME=goGinApp INSECURE_MODE=true OTEL_EXPORTER_OTLP_ENDPOINT=localhost:4317 go run main.go
```
If you want to update your `service_name`, you can modify the `SERVICE_NAME` variable.<br></br>
If you want to update your `service_name`, you can modify the `SERVICE_NAME` variable.
`SERVICE_NAME`: goGinApp (you can name it whatever you want)
---
Expand All @@ -299,7 +299,7 @@ For Golang application deployed on Kubernetes, you need to install OTel Collecto
Once you have set up OTel Collector agent, you can proceed with OpenTelemetry Golang instrumentation by following the below steps:
1. **Install Dependencies**<br></br>
1. **Install Dependencies**
Dependencies related to OpenTelemetry exporter and SDK have to be installed first. Note that we are assuming you are using `gin` request router. If you are using other request routers, check out the [corresponding package](#request-routers).
Expand All @@ -314,7 +314,7 @@ Once you have set up OTel Collector agent, you can proceed with OpenTelemetry Go
go.opentelemetry.io/otel/exporters/otlp/otlptrace/otlptracegrpc
```
2. **Declare environment variables for configuring OpenTelemetry**<br></br>
2. **Declare environment variables for configuring OpenTelemetry**
Declare the following global variables in `main.go` which we will use to configure OpenTelemetry:
Expand All @@ -326,7 +326,7 @@ Once you have set up OTel Collector agent, you can proceed with OpenTelemetry Go
)
```
3. **Instrument your Go application with OpenTelemetry**<br></br>
3. **Instrument your Go application with OpenTelemetry**
To configure your application to send data we will need a function to initialize OpenTelemetry. Add the following snippet of code in your `main.go` file.
Expand Down Expand Up @@ -387,7 +387,7 @@ Once you have set up OTel Collector agent, you can proceed with OpenTelemetry Go
return exporter.Shutdown
}
4. **Initialize the tracer in main.go**<br></br>
4. **Initialize the tracer in main.go**
Modify the main function to initialise the tracer in `main.go`. Initiate the tracer at the very beginning of our main function.
Expand All @@ -400,7 +400,7 @@ Once you have set up OTel Collector agent, you can proceed with OpenTelemetry Go
}
```
5. **Add the OpenTelemetry Gin middleware**<br></br>
5. **Add the OpenTelemetry Gin middleware**
Configure Gin to use the middleware by adding the following lines in `main.go`.
Expand All @@ -418,13 +418,13 @@ Once you have set up OTel Collector agent, you can proceed with OpenTelemetry Go
}
```
6. **Set environment variables and run your Go Gin application**<br></br>
6. **Set environment variables and run your Go Gin application**
The run command must have some environment variables to send data to SigNoz. The run command:
```bash
SERVICE_NAME=goGinApp INSECURE_MODE=true OTEL_EXPORTER_OTLP_ENDPOINT=localhost:4317 go run main.go
```
If you want to update your `service_name`, you can modify the `SERVICE_NAME` variable.<br></br>
If you want to update your `service_name`, you can modify the `SERVICE_NAME` variable.
`SERVICE_NAME`: goGinApp (you can name it whatever you want)
Loading

0 comments on commit 620e2c3

Please sign in to comment.