@dynova/newrelic-sb-log-forwarder 0.0.1
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- package/LICENSE +201 -0
- package/README.md +228 -0
- package/package.json +45 -0
- package/src/newrelic-sb-log-forwarder/extractors/apim.js +45 -0
- package/src/newrelic-sb-log-forwarder/extractors/containerapps.js +18 -0
- package/src/newrelic-sb-log-forwarder/extractors/datafactory.js +15 -0
- package/src/newrelic-sb-log-forwarder/extractors/default.js +7 -0
- package/src/newrelic-sb-log-forwarder/extractors/functionapps.js +14 -0
- package/src/newrelic-sb-log-forwarder/extractors/index.js +13 -0
- package/src/newrelic-sb-log-forwarder/index.js +8 -0
- package/src/newrelic-sb-log-forwarder/middlewares/AzureExtractorMiddleware.js +28 -0
- package/src/newrelic-sb-log-forwarder/middlewares/NewRelicEnrichmentMiddleware.js +94 -0
- package/src/newrelic-sb-log-forwarder/middlewares/OTLPFormatterMiddleware.js +127 -0
- package/src/newrelic-sb-log-forwarder/parsers/EventHubParser.js +54 -0
- package/src/newrelic-sb-log-forwarder/pipeline/ForwarderPipeline.js +47 -0
- package/src/newrelic-sb-log-forwarder/pipeline/LogContext.js +19 -0
- package/src/newrelic-sb-log-forwarder/schemas/azure/apim.js +15 -0
- package/src/newrelic-sb-log-forwarder/schemas/azure/containerapps.js +31 -0
- package/src/newrelic-sb-log-forwarder/schemas/azure/datafactory.js +13 -0
- package/src/newrelic-sb-log-forwarder/schemas/azure/functionapps.js +7 -0
- package/src/newrelic-sb-log-forwarder/schemas/eventhub.js +17 -0
- package/src/newrelic-sb-log-forwarder/schemas/otlp.js +13 -0
- package/src/newrelic-sb-log-forwarder/senders/OTLPSender.js +32 -0
- package/src/newrelic-sb-log-forwarder/utils/camelCase.js +24 -0
- package/src/newrelic-sb-log-forwarder/utils/logger.js +67 -0
- package/src/newrelic-sb-log-forwarder/utils/pkg.js +22 -0
package/LICENSE
ADDED
|
@@ -0,0 +1,201 @@
|
|
|
1
|
+
Apache License
|
|
2
|
+
Version 2.0, January 2004
|
|
3
|
+
http://www.apache.org/licenses/
|
|
4
|
+
|
|
5
|
+
TERMS AND CONDITIONS FOR USE, REPRODUCTION, AND DISTRIBUTION
|
|
6
|
+
|
|
7
|
+
1. Definitions.
|
|
8
|
+
|
|
9
|
+
"License" shall mean the terms and conditions for use, reproduction,
|
|
10
|
+
and distribution as defined by Sections 1 through 9 of this document.
|
|
11
|
+
|
|
12
|
+
"Licensor" shall mean the copyright owner or entity authorized by
|
|
13
|
+
the copyright owner that is granting the License.
|
|
14
|
+
|
|
15
|
+
"Legal Entity" shall mean the union of the acting entity and all
|
|
16
|
+
other entities that control, are controlled by, or are under common
|
|
17
|
+
control with that entity. For the purposes of this definition,
|
|
18
|
+
"control" means (i) the power, direct or indirect, to cause the
|
|
19
|
+
direction or management of such entity, whether by contract or
|
|
20
|
+
otherwise, or (ii) ownership of fifty percent (50%) or more of the
|
|
21
|
+
outstanding shares, or (iii) beneficial ownership of such entity.
|
|
22
|
+
|
|
23
|
+
"You" (or "Your") shall mean an individual or Legal Entity
|
|
24
|
+
exercising permissions granted by this License.
|
|
25
|
+
|
|
26
|
+
"Source" form shall mean the preferred form for making modifications,
|
|
27
|
+
including but not limited to software source code, documentation
|
|
28
|
+
source, and configuration files.
|
|
29
|
+
|
|
30
|
+
"Object" form shall mean any form resulting from mechanical
|
|
31
|
+
transformation or translation of a Source form, including but
|
|
32
|
+
not limited to compiled object code, generated documentation,
|
|
33
|
+
and conversions to other media types.
|
|
34
|
+
|
|
35
|
+
"Work" shall mean the work of authorship, whether in Source or
|
|
36
|
+
Object form, made available under the License, as indicated by a
|
|
37
|
+
copyright notice that is included in or attached to the work
|
|
38
|
+
(an example is provided in the Appendix below).
|
|
39
|
+
|
|
40
|
+
"Derivative Works" shall mean any work, whether in Source or Object
|
|
41
|
+
form, that is based on (or derived from) the Work and for which the
|
|
42
|
+
editorial revisions, annotations, elaborations, or other modifications
|
|
43
|
+
represent, as a whole, an original work of authorship. For the purposes
|
|
44
|
+
of this License, Derivative Works shall not include works that remain
|
|
45
|
+
separable from, or merely link (or bind by name) to the interfaces of,
|
|
46
|
+
the Work and Derivative Works thereof.
|
|
47
|
+
|
|
48
|
+
"Contribution" shall mean any work of authorship, including
|
|
49
|
+
the original version of the Work and any modifications or additions
|
|
50
|
+
to that Work or Derivative Works thereof, that is intentionally
|
|
51
|
+
submitted to Licensor for inclusion in the Work by the copyright owner
|
|
52
|
+
or by an individual or Legal Entity authorized to submit on behalf of
|
|
53
|
+
the copyright owner. For the purposes of this definition, "submitted"
|
|
54
|
+
means any form of electronic, verbal, or written communication sent
|
|
55
|
+
to the Licensor or its representatives, including but not limited to
|
|
56
|
+
communication on electronic mailing lists, source code control systems,
|
|
57
|
+
and issue tracking systems that are managed by, or on behalf of, the
|
|
58
|
+
Licensor for the purpose of discussing and improving the Work, but
|
|
59
|
+
excluding communication that is conspicuously marked or otherwise
|
|
60
|
+
designated in writing by the copyright owner as "Not a Contribution."
|
|
61
|
+
|
|
62
|
+
"Contributor" shall mean Licensor and any individual or Legal Entity
|
|
63
|
+
on behalf of whom a Contribution has been received by Licensor and
|
|
64
|
+
subsequently incorporated within the Work.
|
|
65
|
+
|
|
66
|
+
2. Grant of Copyright License. Subject to the terms and conditions of
|
|
67
|
+
this License, each Contributor hereby grants to You a perpetual,
|
|
68
|
+
worldwide, non-exclusive, no-charge, royalty-free, irrevocable
|
|
69
|
+
copyright license to reproduce, prepare Derivative Works of,
|
|
70
|
+
publicly display, publicly perform, sublicense, and distribute the
|
|
71
|
+
Work and such Derivative Works in Source or Object form.
|
|
72
|
+
|
|
73
|
+
3. Grant of Patent License. Subject to the terms and conditions of
|
|
74
|
+
this License, each Contributor hereby grants to You a perpetual,
|
|
75
|
+
worldwide, non-exclusive, no-charge, royalty-free, irrevocable
|
|
76
|
+
(except as stated in this section) patent license to make, have made,
|
|
77
|
+
use, offer to sell, sell, import, and otherwise transfer the Work,
|
|
78
|
+
where such license applies only to those patent claims licensable
|
|
79
|
+
by such Contributor that are necessarily infringed by their
|
|
80
|
+
Contribution(s) alone or by combination of their Contribution(s)
|
|
81
|
+
with the Work to which such Contribution(s) was submitted. If You
|
|
82
|
+
institute patent litigation against any entity (including a
|
|
83
|
+
cross-claim or counterclaim in a lawsuit) alleging that the Work
|
|
84
|
+
or a Contribution incorporated within the Work constitutes direct
|
|
85
|
+
or contributory patent infringement, then any patent licenses
|
|
86
|
+
granted to You under this License for that Work shall terminate
|
|
87
|
+
as of the date such litigation is filed.
|
|
88
|
+
|
|
89
|
+
4. Redistribution. You may reproduce and distribute copies of the
|
|
90
|
+
Work or Derivative Works thereof in any medium, with or without
|
|
91
|
+
modifications, and in Source or Object form, provided that You
|
|
92
|
+
meet the following conditions:
|
|
93
|
+
|
|
94
|
+
(a) You must give any other recipients of the Work or
|
|
95
|
+
Derivative Works a copy of this License; and
|
|
96
|
+
|
|
97
|
+
(b) You must cause any modified files to carry prominent notices
|
|
98
|
+
stating that You changed the files; and
|
|
99
|
+
|
|
100
|
+
(c) You must retain, in the Source form of any Derivative Works
|
|
101
|
+
that You distribute, all copyright, patent, trademark, and
|
|
102
|
+
attribution notices from the Source form of the Work,
|
|
103
|
+
excluding those notices that do not pertain to any part of
|
|
104
|
+
the Derivative Works; and
|
|
105
|
+
|
|
106
|
+
(d) If the Work includes a "NOTICE" text file as part of its
|
|
107
|
+
distribution, then any Derivative Works that You distribute must
|
|
108
|
+
include a readable copy of the attribution notices contained
|
|
109
|
+
within such NOTICE file, excluding those notices that do not
|
|
110
|
+
pertain to any part of the Derivative Works, in at least one
|
|
111
|
+
of the following places: within a NOTICE text file distributed
|
|
112
|
+
as part of the Derivative Works; within the Source form or
|
|
113
|
+
documentation, if provided along with the Derivative Works; or,
|
|
114
|
+
within a display generated by the Derivative Works, if and
|
|
115
|
+
wherever such third-party notices normally appear. The contents
|
|
116
|
+
of the NOTICE file are for informational purposes only and
|
|
117
|
+
do not modify the License. You may add Your own attribution
|
|
118
|
+
notices within Derivative Works that You distribute, alongside
|
|
119
|
+
or as an addendum to the NOTICE text from the Work, provided
|
|
120
|
+
that such additional attribution notices cannot be construed
|
|
121
|
+
as modifying the License.
|
|
122
|
+
|
|
123
|
+
You may add Your own copyright statement to Your modifications and
|
|
124
|
+
may provide additional or different license terms and conditions
|
|
125
|
+
for use, reproduction, or distribution of Your modifications, or
|
|
126
|
+
for any such Derivative Works as a whole, provided Your use,
|
|
127
|
+
reproduction, and distribution of the Work otherwise complies with
|
|
128
|
+
the conditions stated in this License.
|
|
129
|
+
|
|
130
|
+
5. Submission of Contributions. Unless You explicitly state otherwise,
|
|
131
|
+
any Contribution intentionally submitted for inclusion in the Work
|
|
132
|
+
by You to the Licensor shall be under the terms and conditions of
|
|
133
|
+
this License, without any additional terms or conditions.
|
|
134
|
+
Notwithstanding the above, nothing herein shall supersede or modify
|
|
135
|
+
the terms of any separate license agreement you may have executed
|
|
136
|
+
with Licensor regarding such Contributions.
|
|
137
|
+
|
|
138
|
+
6. Trademarks. This License does not grant permission to use the trade
|
|
139
|
+
names, trademarks, service marks, or product names of the Licensor,
|
|
140
|
+
except as required for reasonable and customary use in describing the
|
|
141
|
+
origin of the Work and reproducing the content of the NOTICE file.
|
|
142
|
+
|
|
143
|
+
7. Disclaimer of Warranty. Unless required by applicable law or
|
|
144
|
+
agreed to in writing, Licensor provides the Work (and each
|
|
145
|
+
Contributor provides its Contributions) on an "AS IS" BASIS,
|
|
146
|
+
WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or
|
|
147
|
+
implied, including, without limitation, any warranties or conditions
|
|
148
|
+
of TITLE, NON-INFRINGEMENT, MERCHANTABILITY, or FITNESS FOR A
|
|
149
|
+
PARTICULAR PURPOSE. You are solely responsible for determining the
|
|
150
|
+
appropriateness of using or redistributing the Work and assume any
|
|
151
|
+
risks associated with Your exercise of permissions under this License.
|
|
152
|
+
|
|
153
|
+
8. Limitation of Liability. In no event and under no legal theory,
|
|
154
|
+
whether in tort (including negligence), contract, or otherwise,
|
|
155
|
+
unless required by applicable law (such as deliberate and grossly
|
|
156
|
+
negligent acts) or agreed to in writing, shall any Contributor be
|
|
157
|
+
liable to You for damages, including any direct, indirect, special,
|
|
158
|
+
incidental, or consequential damages of any character arising as a
|
|
159
|
+
result of this License or out of the use or inability to use the
|
|
160
|
+
Work (including but not limited to damages for loss of goodwill,
|
|
161
|
+
work stoppage, computer failure or malfunction, or any and all
|
|
162
|
+
other commercial damages or losses), even if such Contributor
|
|
163
|
+
has been advised of the possibility of such damages.
|
|
164
|
+
|
|
165
|
+
9. Accepting Warranty or Additional Liability. While redistributing
|
|
166
|
+
the Work or Derivative Works thereof, You may choose to offer,
|
|
167
|
+
and charge a fee for, acceptance of support, warranty, indemnity,
|
|
168
|
+
or other liability obligations and/or rights consistent with this
|
|
169
|
+
License. However, in accepting such obligations, You may act only
|
|
170
|
+
on Your own behalf and on Your sole responsibility, not on behalf
|
|
171
|
+
of any other Contributor, and only if You agree to indemnify,
|
|
172
|
+
defend, and hold each Contributor harmless for any liability
|
|
173
|
+
incurred by, or claims asserted against, such Contributor by reason
|
|
174
|
+
of your accepting any such warranty or additional liability.
|
|
175
|
+
|
|
176
|
+
END OF TERMS AND CONDITIONS
|
|
177
|
+
|
|
178
|
+
APPENDIX: How to apply the Apache License to your work.
|
|
179
|
+
|
|
180
|
+
To apply the Apache License to your work, attach the following
|
|
181
|
+
boilerplate notice, with the fields enclosed by brackets "[]"
|
|
182
|
+
replaced with your own identifying information. (Don't include
|
|
183
|
+
the brackets!) The text should be enclosed in the appropriate
|
|
184
|
+
comment syntax for the file format. We also recommend that a
|
|
185
|
+
file or class name and description of purpose be included on the
|
|
186
|
+
same "printed page" as the copyright notice for easier
|
|
187
|
+
identification within third-party archives.
|
|
188
|
+
|
|
189
|
+
Copyright 2025 Dynova
|
|
190
|
+
|
|
191
|
+
Licensed under the Apache License, Version 2.0 (the "License");
|
|
192
|
+
you may not use this file except in compliance with the License.
|
|
193
|
+
You may obtain a copy of the License at
|
|
194
|
+
|
|
195
|
+
http://www.apache.org/licenses/LICENSE-2.0
|
|
196
|
+
|
|
197
|
+
Unless required by applicable law or agreed to in writing, software
|
|
198
|
+
distributed under the License is distributed on an "AS IS" BASIS,
|
|
199
|
+
WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
|
|
200
|
+
See the License for the specific language governing permissions and
|
|
201
|
+
limitations under the License.
|
package/README.md
ADDED
|
@@ -0,0 +1,228 @@
|
|
|
1
|
+
# New Relic Azure Log Forwarder
|
|
2
|
+
|
|
3
|
+
![Community-Project][repository:banner]
|
|
4
|
+
|
|
5
|
+
[](code_of_conduct.md)
|
|
6
|
+
[](LICENSE)
|
|
7
|
+
|
|
8
|
+
A powerful, extensible pipeline-driven Node.js library for ingesting, parsing, and forwarding Azure telemetry logs to New Relic with full environmental context.
|
|
9
|
+
|
|
10
|
+
This project evolved from a simple script into a robust, decoupled architecture capable of natively interpreting various Azure service logs (Container Apps, Function Apps, API Management, Data Factory) and standardizing them into strict OpenTelemetry formats.
|
|
11
|
+
|
|
12
|
+
## ⚡ Quick Usage Guide
|
|
13
|
+
|
|
14
|
+
To integrate this library directly into your Azure Functions:
|
|
15
|
+
|
|
16
|
+
1. **Install the package:**
|
|
17
|
+
|
|
18
|
+
```bash
|
|
19
|
+
npm install @dynova/newrelic-sb-azure-forwarder
|
|
20
|
+
```
|
|
21
|
+
|
|
22
|
+
2. **Configure your Pipeline:**
|
|
23
|
+
Inside your Azure Function handler, instantiate the pipeline and send your `InvocationContext` logs directly:
|
|
24
|
+
|
|
25
|
+
```javascript
|
|
26
|
+
import {
|
|
27
|
+
LogContext,
|
|
28
|
+
ForwarderPipeline,
|
|
29
|
+
NewRelicEnrichmentMiddleware,
|
|
30
|
+
OTLPFormatterMiddleware,
|
|
31
|
+
AzureExtractorMiddleware,
|
|
32
|
+
NewRelicLogSender
|
|
33
|
+
} from '@dynova/newrelic-sb-azure-forwarder'
|
|
34
|
+
|
|
35
|
+
// Create the sender
|
|
36
|
+
const sender = new NewRelicLogSender({
|
|
37
|
+
licenseKey: process.env.NR_LICENSE_KEY,
|
|
38
|
+
metricsUrl: 'https://log-api.newrelic.com/log/v1'
|
|
39
|
+
})
|
|
40
|
+
|
|
41
|
+
// Create the pipeline
|
|
42
|
+
const pipeline = new ForwarderPipeline(sender)
|
|
43
|
+
.use(AzureExtractorMiddleware)
|
|
44
|
+
.use(NewRelicEnrichmentMiddleware)
|
|
45
|
+
.use(OTLPFormatterMiddleware)
|
|
46
|
+
|
|
47
|
+
// Pass raw logs alongside your Azure context (for debugging)
|
|
48
|
+
await pipeline.processLog(rawAzureLog, invocationContext)
|
|
49
|
+
```
|
|
50
|
+
|
|
51
|
+
## 📚 Full Documentation
|
|
52
|
+
|
|
53
|
+
We use [MyST](https://mystmd.org/) for our comprehensive documentation.
|
|
54
|
+
The complete documentation covers the Dual-Context Architecture, Custom Middleware creation, and specific Extractor details.
|
|
55
|
+
|
|
56
|
+
To build and view the documentation locally:
|
|
57
|
+
|
|
58
|
+
```bash
|
|
59
|
+
pnpm install
|
|
60
|
+
pnpm run docs:build
|
|
61
|
+
```
|
|
62
|
+
|
|
63
|
+
Then open `docs/_build/html/index.html` in your browser.
|
|
64
|
+
|
|
65
|
+
## 📋 Requirements
|
|
66
|
+
|
|
67
|
+
In order to run locally or deploy the samples in this repository, you will need the following tools:
|
|
68
|
+
|
|
69
|
+
- nvm ([↗][href:nvm])
|
|
70
|
+
- Azure Functions Core Tools ([↗][href:azfct])
|
|
71
|
+
- Azure CLI ([↗][href:azcli])
|
|
72
|
+
- Azure Bicep ([↗][href:azbicep])
|
|
73
|
+
- New Relic account ([↗][href:newrelic])
|
|
74
|
+
- Visual Studio Code ([VSCode ↗][href:vscode]) with the Azure Functions
|
|
75
|
+
extension
|
|
76
|
+
|
|
77
|
+
Read the [REQUIREMENTS.md ↗][href:requirements] file for more information.
|
|
78
|
+
|
|
79
|
+
## Directory Structure
|
|
80
|
+
|
|
81
|
+
The principal components of this repository are organized as follows:
|
|
82
|
+
|
|
83
|
+
```
|
|
84
|
+
.
|
|
85
|
+
├── scripts
|
|
86
|
+
└── src
|
|
87
|
+
```
|
|
88
|
+
|
|
89
|
+
- `scripts`: Contains scripts to automate the deployment to Azure.
|
|
90
|
+
- `src`: Contains the source code for the Log Forwarder.
|
|
91
|
+
|
|
92
|
+
## 🚀 Usage
|
|
93
|
+
|
|
94
|
+
To use this project, you will need to follow these steps:
|
|
95
|
+
|
|
96
|
+
1. Clone this repository to your local machine.
|
|
97
|
+
|
|
98
|
+
```bash
|
|
99
|
+
git clone https://github.com/dynovaio/newrelic-sb-azure-forwarder.git
|
|
100
|
+
```
|
|
101
|
+
|
|
102
|
+
2. Open a terminal and navigate to the root of the repository.
|
|
103
|
+
|
|
104
|
+
```bash
|
|
105
|
+
cd newrelic-log-forwarder
|
|
106
|
+
```
|
|
107
|
+
|
|
108
|
+
3. Read the `REQUIREMENTS.md` file and meet the requirements.
|
|
109
|
+
|
|
110
|
+
4. Deploy the sample to azure using the scripts provided in the `scripts`
|
|
111
|
+
directory. This scripts can be run using the following command:
|
|
112
|
+
|
|
113
|
+
```bash
|
|
114
|
+
# Modify the values of the variables according to your requirements
|
|
115
|
+
location="eastus"
|
|
116
|
+
resource_group_name="my-resource-group"
|
|
117
|
+
function_app_runtime_version="20"
|
|
118
|
+
new_relic_license_key="YOUR_NEW_RELIC_LICENSE_KEY"
|
|
119
|
+
source_service_type="my_source_service_type"
|
|
120
|
+
|
|
121
|
+
./scripts/create_resourcegroup.sh \
|
|
122
|
+
$resource_group_name \
|
|
123
|
+
$location
|
|
124
|
+
|
|
125
|
+
./scripts/create_functionapp.sh \
|
|
126
|
+
$function_app_runtime_version \
|
|
127
|
+
$resource_group_name \
|
|
128
|
+
$location
|
|
129
|
+
|
|
130
|
+
./scripts/create_eventhub.sh \
|
|
131
|
+
$resource_group_name \
|
|
132
|
+
$location
|
|
133
|
+
|
|
134
|
+
./scripts/configure_functionapp.sh \
|
|
135
|
+
$function_app_name \
|
|
136
|
+
$resource_group_name \
|
|
137
|
+
$source_service_type \
|
|
138
|
+
$new_relic_license_key
|
|
139
|
+
|
|
140
|
+
./scripts/publish_functionapp.sh \
|
|
141
|
+
$function_app_name \
|
|
142
|
+
$function_app_runtime \
|
|
143
|
+
$resource_group_name
|
|
144
|
+
```
|
|
145
|
+
|
|
146
|
+
5. Configure your target Azure component to send logs to the Azure Event Hub
|
|
147
|
+
using the following command:
|
|
148
|
+
|
|
149
|
+
```bash
|
|
150
|
+
# Modify the values of the variables according to your requirements
|
|
151
|
+
resource_id = "my-resource-id"
|
|
152
|
+
resource_group_name = "my-resource-group"
|
|
153
|
+
log_configuration = "my-log-configuration"
|
|
154
|
+
metrics_configuration = "my-metrics-configuration"
|
|
155
|
+
|
|
156
|
+
./scripts/stream_logs_to_eventhub.sh \
|
|
157
|
+
$resource_id \
|
|
158
|
+
$resource_group_name \
|
|
159
|
+
$log_configuration \
|
|
160
|
+
$metrics_configuration
|
|
161
|
+
```
|
|
162
|
+
|
|
163
|
+
The variables must be replaced with the correct values according to your
|
|
164
|
+
target resource. The `resource_id` is the id of the resource that you want
|
|
165
|
+
to stream logs from. The `log_configuration` and `metrics_configuration`
|
|
166
|
+
are the log and metrics configurations that you want to stream to the Event
|
|
167
|
+
Hub.
|
|
168
|
+
|
|
169
|
+
For example, in the case of azure functions, you can use the following
|
|
170
|
+
|
|
171
|
+
```bash
|
|
172
|
+
resource_id=$(
|
|
173
|
+
az functionapp show
|
|
174
|
+
--name $function_app_name
|
|
175
|
+
--resource-group $function_app_resource_group_name
|
|
176
|
+
--query id
|
|
177
|
+
--output tsv
|
|
178
|
+
)
|
|
179
|
+
log_configuration="'[{\\\"category\\\": \\\"FunctionAppLogs\\\", \\\"enabled\\\": true}]'"
|
|
180
|
+
metrics_configuration="'[{\\\"category\\\": \\\"AllMetrics\\\", \\\"enabled\\\": true}]'"
|
|
181
|
+
```
|
|
182
|
+
|
|
183
|
+
6. Check the logs in New Relic.
|
|
184
|
+
|
|
185
|
+
7. Clean up the resources using the following command:
|
|
186
|
+
|
|
187
|
+
```bash
|
|
188
|
+
az group delete --name $resource_group_name --yes
|
|
189
|
+
```
|
|
190
|
+
|
|
191
|
+
## 🤝 Contributing
|
|
192
|
+
|
|
193
|
+
Sugestions and contributions are welcome!
|
|
194
|
+
|
|
195
|
+
> Please note that this project is released with a Contributor Code of Conduct.
|
|
196
|
+
> By participating in this project you agree to abide by its terms.
|
|
197
|
+
|
|
198
|
+
For more information, please refer to the
|
|
199
|
+
[Code of Conduct ↗][href:code_of_conduct].
|
|
200
|
+
|
|
201
|
+
## 👥 Contributors
|
|
202
|
+
|
|
203
|
+
See our [list of contributors][repository:contributors].
|
|
204
|
+
|
|
205
|
+
## 📜 Changelog
|
|
206
|
+
|
|
207
|
+
See the [CHANGELOG.md](./CHANGELOG.md) for a history of changes.
|
|
208
|
+
|
|
209
|
+
## 📄 License
|
|
210
|
+
|
|
211
|
+
This project is licensed under the terms of the
|
|
212
|
+
[BSD-3-Clause ↗][href:license] license.
|
|
213
|
+
|
|
214
|
+
[repository]: https://gitlab.com/softbutterfly/open-source/newrelic-sb-log-forwarder
|
|
215
|
+
[repository:banner]: https://gitlab.com/softbutterfly/open-source/open-source-office/-/raw/master/assets/dynova/dynova-open-source--banner--community-project.png
|
|
216
|
+
[repository:contributors]: https://gitlab.com/softbutterfly/open-source/newrelic-sb-log-forwarder/-/graphs/master
|
|
217
|
+
[repository:pipeline]: https://gitlab.com/softbutterfly/open-source/newrelic-sb-log-forwarder/badges/master/pipeline.svg
|
|
218
|
+
[repository:commits]: https://gitlab.com/softbutterfly/open-source/newrelic-sb-log-forwarder/-/commits/master
|
|
219
|
+
[href:nrlogforwarder]: https://github.com/newrelic/newrelic-azure-functions/tree/master
|
|
220
|
+
[href:nvm]: https://github.com/nvm-sh/nvm
|
|
221
|
+
[href:azfct]: https://github.com/Azure/azure-functions-core-tools
|
|
222
|
+
[href:azcli]: https://docs.microsoft.com/en-us/cli/azure/install-azure-cli
|
|
223
|
+
[href:azbicep]: https://learn.microsoft.com/en-us/azure/azure-resource-manager/bicep/install#azure-cli
|
|
224
|
+
[href:newrelic]: https://newrelic.com/signup
|
|
225
|
+
[href:requirements]: ./REQUIREMENTS.md
|
|
226
|
+
[href:license]: ./LICENSE
|
|
227
|
+
[href:code_of_conduct]: ./CODE_OF_CONDUCT.md
|
|
228
|
+
[href:vscode]: https://code.visualstudio.com
|
package/package.json
ADDED
|
@@ -0,0 +1,45 @@
|
|
|
1
|
+
{
|
|
2
|
+
"name": "@dynova/newrelic-sb-log-forwarder",
|
|
3
|
+
"version": "0.0.1",
|
|
4
|
+
"description": "A collection of forwarder functions for Azure Service",
|
|
5
|
+
"type": "module",
|
|
6
|
+
"main": "src/newrelic-sb-log-forwarder/index.js",
|
|
7
|
+
"exports": {
|
|
8
|
+
".": "./src/newrelic-sb-log-forwarder/index.js"
|
|
9
|
+
},
|
|
10
|
+
"files": [
|
|
11
|
+
"src/newrelic-sb-log-forwarder",
|
|
12
|
+
"README.md",
|
|
13
|
+
"LICENSE",
|
|
14
|
+
"package.json"
|
|
15
|
+
],
|
|
16
|
+
"engines": {
|
|
17
|
+
"node": ">=22.0.0"
|
|
18
|
+
},
|
|
19
|
+
"dependencies": {
|
|
20
|
+
"@opentelemetry/exporter-logs-otlp-http": "^0.218.0",
|
|
21
|
+
"winston": "^3.19.0",
|
|
22
|
+
"zod": "^4.4.3"
|
|
23
|
+
},
|
|
24
|
+
"devDependencies": {
|
|
25
|
+
"@vitest/coverage-v8": "^4.1.6",
|
|
26
|
+
"commit-and-tag-version": "^12.7.3",
|
|
27
|
+
"eslint": "^10.3.0",
|
|
28
|
+
"eslint-config-prettier": "^10.1.8",
|
|
29
|
+
"eslint-plugin-prettier": "^5.5.5",
|
|
30
|
+
"husky": "^9.1.7",
|
|
31
|
+
"mystmd": "^1.9.0",
|
|
32
|
+
"prettier": "^3.8.3",
|
|
33
|
+
"vitest": "^4.1.6"
|
|
34
|
+
},
|
|
35
|
+
"scripts": {
|
|
36
|
+
"start": "func start",
|
|
37
|
+
"test": "vitest run",
|
|
38
|
+
"coverage": "vitest run --coverage",
|
|
39
|
+
"format": "prettier . --write",
|
|
40
|
+
"lint": "eslint . --fix",
|
|
41
|
+
"release": "commit-and-tag-version",
|
|
42
|
+
"docs:build": "myst build --html",
|
|
43
|
+
"docs:dev": "myst start"
|
|
44
|
+
}
|
|
45
|
+
}
|
|
@@ -0,0 +1,45 @@
|
|
|
1
|
+
import { APIManagementSchema } from '../schemas/azure/apim.js'
|
|
2
|
+
|
|
3
|
+
function safeStringify(obj, prefix) {
|
|
4
|
+
if (typeof obj === 'string') {
|
|
5
|
+
try {
|
|
6
|
+
return prefix + JSON.stringify(JSON.parse(obj))
|
|
7
|
+
} catch {
|
|
8
|
+
return prefix + obj
|
|
9
|
+
}
|
|
10
|
+
} else if (typeof obj === 'object') {
|
|
11
|
+
return prefix + JSON.stringify(obj)
|
|
12
|
+
}
|
|
13
|
+
return prefix + String(obj)
|
|
14
|
+
}
|
|
15
|
+
|
|
16
|
+
export function extract(rawLog, envContext = {}) {
|
|
17
|
+
envContext.logger?.warn('Using APIM extractor for log: %j', rawLog)
|
|
18
|
+
|
|
19
|
+
const validation = APIManagementSchema.safeParse(rawLog)
|
|
20
|
+
if (validation.success && rawLog.properties) {
|
|
21
|
+
const props = rawLog.properties
|
|
22
|
+
|
|
23
|
+
// Prevent JSON nesting explosion in New Relic
|
|
24
|
+
if (props.request?.body !== undefined) {
|
|
25
|
+
props.request.body = safeStringify(props.request.body, 'RequestBody::')
|
|
26
|
+
}
|
|
27
|
+
if (props.request?.headers !== undefined) {
|
|
28
|
+
props.request.headers = safeStringify(props.request.headers, 'RequestHeaders::')
|
|
29
|
+
}
|
|
30
|
+
if (props.response?.body !== undefined) {
|
|
31
|
+
props.response.body = safeStringify(props.response.body, 'ResponseBody::')
|
|
32
|
+
}
|
|
33
|
+
if (props.response?.headers !== undefined && props.response.headers !== '{}') {
|
|
34
|
+
props.response.headers = safeStringify(props.response.headers, 'ResponseHeaders::')
|
|
35
|
+
}
|
|
36
|
+
|
|
37
|
+
return {
|
|
38
|
+
type: 'apim',
|
|
39
|
+
properties: props,
|
|
40
|
+
traceId: rawLog.traceId,
|
|
41
|
+
spanId: rawLog.spanId
|
|
42
|
+
}
|
|
43
|
+
}
|
|
44
|
+
return null
|
|
45
|
+
}
|
|
@@ -0,0 +1,18 @@
|
|
|
1
|
+
import { ContainerAppSchema } from '../schemas/azure/containerapps.js'
|
|
2
|
+
|
|
3
|
+
export function extract(rawLog, envContext = {}) {
|
|
4
|
+
envContext.logger?.warn('Using ContainerApps extractor for log: %j', rawLog)
|
|
5
|
+
|
|
6
|
+
const validation = ContainerAppSchema.safeParse(rawLog)
|
|
7
|
+
if (validation.success && rawLog.properties) {
|
|
8
|
+
return {
|
|
9
|
+
type: 'containerapps',
|
|
10
|
+
log: rawLog.properties.Log || rawLog.properties.log,
|
|
11
|
+
stream: rawLog.properties.Stream || rawLog.properties.stream,
|
|
12
|
+
hostname: rawLog.properties.EnvironmentName || rawLog.properties.environmentName,
|
|
13
|
+
extractedServiceName: rawLog.properties.ContainerAppName || rawLog.properties.containerAppName,
|
|
14
|
+
properties: rawLog.properties
|
|
15
|
+
}
|
|
16
|
+
}
|
|
17
|
+
return null
|
|
18
|
+
}
|
|
@@ -0,0 +1,15 @@
|
|
|
1
|
+
import { DataFactorySchema } from '../schemas/azure/datafactory.js'
|
|
2
|
+
|
|
3
|
+
export function extract(rawLog, envContext = {}) {
|
|
4
|
+
envContext.logger?.warn('Using DataFactory extractor for log: %j', rawLog)
|
|
5
|
+
|
|
6
|
+
const validation = DataFactorySchema.safeParse(rawLog)
|
|
7
|
+
if (validation.success) {
|
|
8
|
+
return {
|
|
9
|
+
type: 'datafactory',
|
|
10
|
+
pipelineName: rawLog.properties?.PipelineName,
|
|
11
|
+
activityName: rawLog.properties?.ActivityName
|
|
12
|
+
}
|
|
13
|
+
}
|
|
14
|
+
return null
|
|
15
|
+
}
|
|
@@ -0,0 +1,14 @@
|
|
|
1
|
+
import { FunctionAppSchema } from '../schemas/azure/functionapps.js'
|
|
2
|
+
|
|
3
|
+
export function extract(rawLog, envContext = {}) {
|
|
4
|
+
envContext.logger?.warn('Using FunctionApps extractor for log: %j', rawLog)
|
|
5
|
+
|
|
6
|
+
const validation = FunctionAppSchema.safeParse(rawLog)
|
|
7
|
+
if (validation.success) {
|
|
8
|
+
return {
|
|
9
|
+
type: 'functionapps',
|
|
10
|
+
message: rawLog.message
|
|
11
|
+
}
|
|
12
|
+
}
|
|
13
|
+
return null
|
|
14
|
+
}
|
|
@@ -0,0 +1,13 @@
|
|
|
1
|
+
import { extract as apim } from './apim.js'
|
|
2
|
+
import { extract as containerapps } from './containerapps.js'
|
|
3
|
+
import { extract as functionapps } from './functionapps.js'
|
|
4
|
+
import { extract as datafactory } from './datafactory.js'
|
|
5
|
+
import { extract as defaultExtractor } from './default.js'
|
|
6
|
+
|
|
7
|
+
export default {
|
|
8
|
+
apim,
|
|
9
|
+
containerapps,
|
|
10
|
+
functionapps,
|
|
11
|
+
datafactory,
|
|
12
|
+
default: defaultExtractor
|
|
13
|
+
}
|
|
@@ -0,0 +1,8 @@
|
|
|
1
|
+
export { ForwarderPipeline } from './pipeline/ForwarderPipeline.js'
|
|
2
|
+
export { LogContext } from './pipeline/LogContext.js'
|
|
3
|
+
export { AzureExtractorMiddleware } from './middlewares/AzureExtractorMiddleware.js'
|
|
4
|
+
export { NewRelicEnrichmentMiddleware } from './middlewares/NewRelicEnrichmentMiddleware.js'
|
|
5
|
+
export { OTLPFormatterMiddleware } from './middlewares/OTLPFormatterMiddleware.js'
|
|
6
|
+
export { OTLPSender } from './senders/OTLPSender.js'
|
|
7
|
+
export { flattenEventHubMessages } from './parsers/EventHubParser.js'
|
|
8
|
+
export { createForwarderLogger } from './utils/logger.js'
|
|
@@ -0,0 +1,28 @@
|
|
|
1
|
+
import extractors from '../extractors/index.js'
|
|
2
|
+
|
|
3
|
+
export function AzureExtractorMiddleware() {
|
|
4
|
+
return async (logContext, envContext, next) => {
|
|
5
|
+
envContext.logger.debug('AzureExtractorMiddleware received logContext: %j', logContext)
|
|
6
|
+
|
|
7
|
+
const type = logContext.environment.sourceServiceType
|
|
8
|
+
const rawLog = logContext.rawLog
|
|
9
|
+
|
|
10
|
+
try {
|
|
11
|
+
const extractor = extractors[type] || extractors.default
|
|
12
|
+
const serviceInfo = extractor(rawLog, envContext)
|
|
13
|
+
|
|
14
|
+
if (serviceInfo) {
|
|
15
|
+
logContext.serviceInfo = serviceInfo
|
|
16
|
+
} else {
|
|
17
|
+
// If specific extractor failed validation, fallback to default or log warning?
|
|
18
|
+
// Using default to capture raw payload instead of dropping silently.
|
|
19
|
+
logContext.serviceInfo = extractors.default(rawLog, envContext)
|
|
20
|
+
}
|
|
21
|
+
} catch (e) {
|
|
22
|
+
logContext.addError(e)
|
|
23
|
+
logContext.drop()
|
|
24
|
+
}
|
|
25
|
+
|
|
26
|
+
await next()
|
|
27
|
+
}
|
|
28
|
+
}
|
|
@@ -0,0 +1,94 @@
|
|
|
1
|
+
export function NewRelicEnrichmentMiddleware() {
|
|
2
|
+
const nrLinkingRegex = /NR-LINKING\|([^|]*)\|([^|]*)\|([^|]*)\|([^|]*)\|([^|]*)\|?/
|
|
3
|
+
|
|
4
|
+
return async (logContext, envContext, next) => {
|
|
5
|
+
envContext.logger?.debug('NewRelicEnrichmentMiddleware received logContext: %j', logContext)
|
|
6
|
+
|
|
7
|
+
let extracted = false
|
|
8
|
+
const nrMetadata = {}
|
|
9
|
+
|
|
10
|
+
// Helper to check and extract from string
|
|
11
|
+
const extractFromString = (str) => {
|
|
12
|
+
if (typeof str !== 'string') return str
|
|
13
|
+
|
|
14
|
+
const match = str.match(nrLinkingRegex)
|
|
15
|
+
if (match) {
|
|
16
|
+
extracted = true
|
|
17
|
+
const mappings = [
|
|
18
|
+
{ idx: 1, key: 'entityGuid' },
|
|
19
|
+
{ idx: 2, key: 'hostname' },
|
|
20
|
+
{ idx: 3, key: 'traceId' },
|
|
21
|
+
{ idx: 4, key: 'spanId' },
|
|
22
|
+
{ idx: 5, key: 'entityName' }
|
|
23
|
+
]
|
|
24
|
+
for (const m of mappings) {
|
|
25
|
+
if (match[m.idx]) {
|
|
26
|
+
nrMetadata[m.key] = m.idx === 5 ? decodeURIComponent(match[m.idx]) : match[m.idx]
|
|
27
|
+
}
|
|
28
|
+
}
|
|
29
|
+
|
|
30
|
+
// Strip the blob from the message
|
|
31
|
+
return str.replace(match[0], '').trim()
|
|
32
|
+
}
|
|
33
|
+
return str
|
|
34
|
+
}
|
|
35
|
+
|
|
36
|
+
// Helper to hoist JSON top-level keys
|
|
37
|
+
const hoistFromJson = (obj) => {
|
|
38
|
+
if (typeof obj !== 'object' || obj === null) return
|
|
39
|
+
|
|
40
|
+
const fields = [
|
|
41
|
+
{ key: 'trace.id', target: 'traceId' },
|
|
42
|
+
{ key: 'span.id', target: 'spanId' },
|
|
43
|
+
{ key: 'entity.guid', target: 'entityGuid' },
|
|
44
|
+
{ key: 'entity.name', target: 'entityName' },
|
|
45
|
+
{ key: 'hostname', target: 'hostname' }
|
|
46
|
+
]
|
|
47
|
+
|
|
48
|
+
for (const field of fields) {
|
|
49
|
+
if (obj[field.key] !== undefined) {
|
|
50
|
+
extracted = true
|
|
51
|
+
nrMetadata[field.target] = obj[field.key]
|
|
52
|
+
delete obj[field.key] // Hoist out of raw log to avoid duplication
|
|
53
|
+
}
|
|
54
|
+
}
|
|
55
|
+
}
|
|
56
|
+
|
|
57
|
+
try {
|
|
58
|
+
if (logContext.serviceInfo) {
|
|
59
|
+
// 1. Check known string fields based on service type
|
|
60
|
+
if (logContext.serviceInfo.log) {
|
|
61
|
+
logContext.serviceInfo.log = extractFromString(logContext.serviceInfo.log)
|
|
62
|
+
}
|
|
63
|
+
if (logContext.serviceInfo.message) {
|
|
64
|
+
logContext.serviceInfo.message = extractFromString(logContext.serviceInfo.message)
|
|
65
|
+
}
|
|
66
|
+
|
|
67
|
+
// 2. If it's APIM or unknown, properties might be an object
|
|
68
|
+
if (logContext.serviceInfo.properties) {
|
|
69
|
+
if (typeof logContext.serviceInfo.properties === 'object') {
|
|
70
|
+
hoistFromJson(logContext.serviceInfo.properties)
|
|
71
|
+
} else if (typeof logContext.serviceInfo.properties === 'string') {
|
|
72
|
+
logContext.serviceInfo.properties = extractFromString(logContext.serviceInfo.properties)
|
|
73
|
+
}
|
|
74
|
+
}
|
|
75
|
+
} else if (logContext.rawLog) {
|
|
76
|
+
// Fallback to rawLog if no serviceInfo
|
|
77
|
+
if (typeof logContext.rawLog === 'object') {
|
|
78
|
+
hoistFromJson(logContext.rawLog)
|
|
79
|
+
} else if (typeof logContext.rawLog === 'string') {
|
|
80
|
+
logContext.rawLog = extractFromString(logContext.rawLog)
|
|
81
|
+
}
|
|
82
|
+
}
|
|
83
|
+
|
|
84
|
+
if (extracted) {
|
|
85
|
+
logContext.metadata.newrelic = nrMetadata
|
|
86
|
+
}
|
|
87
|
+
} catch (e) {
|
|
88
|
+
logContext.addError(e)
|
|
89
|
+
// We don't drop here; enrichment failure shouldn't kill the log
|
|
90
|
+
}
|
|
91
|
+
|
|
92
|
+
await next()
|
|
93
|
+
}
|
|
94
|
+
}
|
|
@@ -0,0 +1,127 @@
|
|
|
1
|
+
import { getPackageInfo } from '../utils/pkg.js'
|
|
2
|
+
import { shallowCamelCase } from '../utils/camelCase.js'
|
|
3
|
+
|
|
4
|
+
const pkg = getPackageInfo()
|
|
5
|
+
|
|
6
|
+
export function OTLPFormatterMiddleware(config = {}) {
|
|
7
|
+
return async (logContext, envContext, next) => {
|
|
8
|
+
envContext.logger?.debug('OTLPFormatterMiddleware received logContext: %j', logContext)
|
|
9
|
+
|
|
10
|
+
// Safe check for properties
|
|
11
|
+
const rawProps =
|
|
12
|
+
typeof logContext.serviceInfo?.properties === 'object' && logContext.serviceInfo.properties !== null
|
|
13
|
+
? logContext.serviceInfo.properties
|
|
14
|
+
: {}
|
|
15
|
+
|
|
16
|
+
// 5. Shallow convert to camel case
|
|
17
|
+
const props = shallowCamelCase(rawProps)
|
|
18
|
+
|
|
19
|
+
const timeVal = props.time || logContext.rawLog?.time
|
|
20
|
+
const timestampMs = timeVal ? new Date(timeVal).getTime() : Date.now()
|
|
21
|
+
|
|
22
|
+
// hrTime format for OpenTelemetry [seconds, nanoseconds]
|
|
23
|
+
const hrTime = [Math.floor(timestampMs / 1000), (timestampMs % 1000) * 1e6]
|
|
24
|
+
|
|
25
|
+
// 8. Find extractor type
|
|
26
|
+
const extractorType = logContext.serviceInfo?.type || 'unknown'
|
|
27
|
+
|
|
28
|
+
const attributes = {
|
|
29
|
+
'plugin.type': pkg.name, // 3. plugin.type from package
|
|
30
|
+
'cloud.provider': 'azure',
|
|
31
|
+
'otel.library.extractor': extractorType
|
|
32
|
+
}
|
|
33
|
+
|
|
34
|
+
// Export stream without prefix
|
|
35
|
+
const streamValue = logContext.serviceInfo?.stream || props.stream
|
|
36
|
+
if (streamValue) {
|
|
37
|
+
attributes['stream'] = streamValue
|
|
38
|
+
}
|
|
39
|
+
|
|
40
|
+
// Prefix all other props with `azure.`
|
|
41
|
+
for (const [key, value] of Object.entries(props)) {
|
|
42
|
+
if (key !== 'stream') {
|
|
43
|
+
attributes[`azure.${key}`] = value
|
|
44
|
+
}
|
|
45
|
+
}
|
|
46
|
+
|
|
47
|
+
if (logContext.rawLog?.category) attributes['azure.category'] = logContext.rawLog.category
|
|
48
|
+
if (logContext.rawLog?.resourceId) attributes['azure.resourceId'] = logContext.rawLog.resourceId
|
|
49
|
+
if (logContext.rawLog?.operationName) attributes['azure.operationName'] = logContext.rawLog.operationName
|
|
50
|
+
if (logContext.rawLog?.location) attributes['azure.location'] = logContext.rawLog.location
|
|
51
|
+
|
|
52
|
+
// 7. Extract service name fallback from resourceId
|
|
53
|
+
let resourceIdFallback = null
|
|
54
|
+
const resourceId = logContext.rawLog?.resourceId || rawProps.resourceId
|
|
55
|
+
if (resourceId) {
|
|
56
|
+
const parts = resourceId.split('/')
|
|
57
|
+
// Take the last segment of the resourceId and lowercase it
|
|
58
|
+
if (parts.length > 0) {
|
|
59
|
+
resourceIdFallback = parts[parts.length - 1].toLowerCase()
|
|
60
|
+
}
|
|
61
|
+
}
|
|
62
|
+
|
|
63
|
+
// Service name precedence:
|
|
64
|
+
// 1. Explicitly configured
|
|
65
|
+
// 2. New Relic decoration (entityName)
|
|
66
|
+
// 3. Extractor specific properties (extractedServiceName)
|
|
67
|
+
// 4. Fallback to resourceId last part
|
|
68
|
+
// 5. Empty
|
|
69
|
+
const nrEntityName = logContext.metadata?.newrelic?.entityName
|
|
70
|
+
const extractorPropertiesFallback = logContext.serviceInfo?.extractedServiceName
|
|
71
|
+
const serviceName = config.serviceName || nrEntityName || extractorPropertiesFallback || resourceIdFallback
|
|
72
|
+
|
|
73
|
+
const resourceAttributes = {}
|
|
74
|
+
if (serviceName) {
|
|
75
|
+
resourceAttributes['service.name'] = serviceName
|
|
76
|
+
}
|
|
77
|
+
|
|
78
|
+
if (logContext.serviceInfo?.hostname) {
|
|
79
|
+
resourceAttributes['hostname'] = logContext.serviceInfo.hostname
|
|
80
|
+
}
|
|
81
|
+
|
|
82
|
+
if (logContext.metadata?.newrelic) {
|
|
83
|
+
const nr = logContext.metadata.newrelic
|
|
84
|
+
if (nr.entityGuid) resourceAttributes['entity.guid'] = nr.entityGuid
|
|
85
|
+
if (nr.entityName) resourceAttributes['entity.name'] = nr.entityName
|
|
86
|
+
if (nr.hostname) resourceAttributes['hostname'] = nr.hostname
|
|
87
|
+
}
|
|
88
|
+
|
|
89
|
+
let spanContext
|
|
90
|
+
if (logContext.metadata?.newrelic?.traceId) {
|
|
91
|
+
spanContext = {
|
|
92
|
+
traceId: logContext.metadata.newrelic.traceId,
|
|
93
|
+
spanId: logContext.metadata.newrelic.spanId || '0000000000000000', // Span ID is 16 hex chars
|
|
94
|
+
traceFlags: 1 // sampled
|
|
95
|
+
}
|
|
96
|
+
}
|
|
97
|
+
|
|
98
|
+
let body = logContext.serviceInfo?.log || logContext.serviceInfo?.message || rawProps.Log || rawProps.log
|
|
99
|
+
if (!body) {
|
|
100
|
+
const propsString = JSON.stringify(props)
|
|
101
|
+
body = propsString !== '{}' ? propsString : 'Empty Log'
|
|
102
|
+
}
|
|
103
|
+
|
|
104
|
+
// Map severity
|
|
105
|
+
let severityText = 'INFO'
|
|
106
|
+
const rawLevel = (props.level || rawProps.Level || '').toUpperCase()
|
|
107
|
+
if (['ERROR', 'WARN', 'DEBUG', 'INFO'].includes(rawLevel)) {
|
|
108
|
+
severityText = rawLevel
|
|
109
|
+
}
|
|
110
|
+
|
|
111
|
+
// 4. Also provide as level attribute
|
|
112
|
+
attributes.level = severityText
|
|
113
|
+
|
|
114
|
+
logContext.otlpLog = {
|
|
115
|
+
hrTime,
|
|
116
|
+
hrTimeObserved: hrTime,
|
|
117
|
+
severityText,
|
|
118
|
+
body,
|
|
119
|
+
attributes,
|
|
120
|
+
instrumentationScope: { name: pkg.name, version: pkg.version }, // 1 & 2. name and version from package
|
|
121
|
+
resource: { attributes: resourceAttributes },
|
|
122
|
+
spanContext
|
|
123
|
+
}
|
|
124
|
+
|
|
125
|
+
await next()
|
|
126
|
+
}
|
|
127
|
+
}
|
|
@@ -0,0 +1,54 @@
|
|
|
1
|
+
import { EventHubMessageSchema, EventHubRecordSchema } from '../schemas/eventhub.js'
|
|
2
|
+
|
|
3
|
+
export function flattenEventHubMessages(messages, envContext = {}) {
|
|
4
|
+
envContext.logger?.debug('EventHubParser received raw messages:\n%j', messages)
|
|
5
|
+
|
|
6
|
+
const rawArray = Array.isArray(messages) ? messages : [messages]
|
|
7
|
+
const flatRecords = []
|
|
8
|
+
|
|
9
|
+
for (let msg of rawArray) {
|
|
10
|
+
let parsed = msg
|
|
11
|
+
if (typeof msg === 'string') {
|
|
12
|
+
try {
|
|
13
|
+
parsed = JSON.parse(msg.trim())
|
|
14
|
+
} catch (e) {
|
|
15
|
+
envContext.logger?.warn('Failed to parse string message as JSON, treating as raw string.')
|
|
16
|
+
}
|
|
17
|
+
}
|
|
18
|
+
|
|
19
|
+
if (parsed && typeof parsed === 'object' && !Array.isArray(parsed)) {
|
|
20
|
+
// Check for Records or records array (Azure Monitor format)
|
|
21
|
+
const recordsToParse = parsed.Records || parsed.records
|
|
22
|
+
|
|
23
|
+
if (recordsToParse !== undefined) {
|
|
24
|
+
const validation = EventHubMessageSchema.safeParse({ Records: recordsToParse })
|
|
25
|
+
if (validation.success) {
|
|
26
|
+
flatRecords.push(...validation.data.Records)
|
|
27
|
+
} else {
|
|
28
|
+
envContext.logger?.warn('Message contained Records but failed Zod validation.', validation.error)
|
|
29
|
+
}
|
|
30
|
+
} else {
|
|
31
|
+
// Treated as a single record
|
|
32
|
+
const validation = EventHubRecordSchema.safeParse(parsed)
|
|
33
|
+
if (validation.success) {
|
|
34
|
+
flatRecords.push(validation.data)
|
|
35
|
+
} else {
|
|
36
|
+
envContext.logger?.warn('Message failed Zod validation as single record.', validation.error)
|
|
37
|
+
}
|
|
38
|
+
}
|
|
39
|
+
} else if (Array.isArray(parsed)) {
|
|
40
|
+
// If the message is a raw array of records
|
|
41
|
+
for (let item of parsed) {
|
|
42
|
+
const validation = EventHubRecordSchema.safeParse(item)
|
|
43
|
+
if (validation.success) {
|
|
44
|
+
flatRecords.push(validation.data)
|
|
45
|
+
}
|
|
46
|
+
}
|
|
47
|
+
} else {
|
|
48
|
+
// Primitive or string that failed parsing
|
|
49
|
+
flatRecords.push({ properties: parsed })
|
|
50
|
+
}
|
|
51
|
+
}
|
|
52
|
+
|
|
53
|
+
return flatRecords
|
|
54
|
+
}
|
|
@@ -0,0 +1,47 @@
|
|
|
1
|
+
import { LogContext } from './LogContext.js'
|
|
2
|
+
|
|
3
|
+
export class ForwarderPipeline {
|
|
4
|
+
constructor(config = {}) {
|
|
5
|
+
this.config = config
|
|
6
|
+
this.middlewares = []
|
|
7
|
+
}
|
|
8
|
+
|
|
9
|
+
use(middleware) {
|
|
10
|
+
this.middlewares.push(middleware)
|
|
11
|
+
return this
|
|
12
|
+
}
|
|
13
|
+
|
|
14
|
+
async processLog(logContext, envContext = {}) {
|
|
15
|
+
let index = -1
|
|
16
|
+
|
|
17
|
+
const dispatch = async (i) => {
|
|
18
|
+
if (i <= index) throw new Error('next() called multiple times')
|
|
19
|
+
index = i
|
|
20
|
+
|
|
21
|
+
if (i === this.middlewares.length) return
|
|
22
|
+
|
|
23
|
+
const middleware = this.middlewares[i]
|
|
24
|
+
await middleware(logContext, envContext, () => dispatch(i + 1))
|
|
25
|
+
}
|
|
26
|
+
|
|
27
|
+
try {
|
|
28
|
+
await dispatch(0)
|
|
29
|
+
} catch (error) {
|
|
30
|
+
logContext.addError(error)
|
|
31
|
+
logContext.drop()
|
|
32
|
+
}
|
|
33
|
+
|
|
34
|
+
return logContext
|
|
35
|
+
}
|
|
36
|
+
|
|
37
|
+
async run(messages, envContext = {}) {
|
|
38
|
+
envContext.logger?.debug('ForwarderPipeline processing flat records:\n%j', messages)
|
|
39
|
+
|
|
40
|
+
const batch = Array.isArray(messages) ? messages : [messages]
|
|
41
|
+
const logContexts = batch.map((msg) => new LogContext(msg, envContext))
|
|
42
|
+
|
|
43
|
+
await Promise.all(logContexts.map((ctx) => this.processLog(ctx, envContext)))
|
|
44
|
+
|
|
45
|
+
return logContexts
|
|
46
|
+
}
|
|
47
|
+
}
|
|
@@ -0,0 +1,19 @@
|
|
|
1
|
+
export class LogContext {
|
|
2
|
+
constructor(rawLog, environmentContext = {}) {
|
|
3
|
+
this.rawLog = rawLog
|
|
4
|
+
this.environment = environmentContext
|
|
5
|
+
this.serviceInfo = null
|
|
6
|
+
this.otlpLog = null
|
|
7
|
+
this.metadata = {}
|
|
8
|
+
this.errors = []
|
|
9
|
+
this.dropped = false
|
|
10
|
+
}
|
|
11
|
+
|
|
12
|
+
addError(error) {
|
|
13
|
+
this.errors.push(error)
|
|
14
|
+
}
|
|
15
|
+
|
|
16
|
+
drop() {
|
|
17
|
+
this.dropped = true
|
|
18
|
+
}
|
|
19
|
+
}
|
|
@@ -0,0 +1,15 @@
|
|
|
1
|
+
import { z } from 'zod'
|
|
2
|
+
|
|
3
|
+
export const APIManagementSchema = z
|
|
4
|
+
.object({
|
|
5
|
+
properties: z
|
|
6
|
+
.object({
|
|
7
|
+
request: z.any().optional(),
|
|
8
|
+
response: z.any().optional()
|
|
9
|
+
})
|
|
10
|
+
.catchall(z.any())
|
|
11
|
+
.optional(),
|
|
12
|
+
traceId: z.string().optional(),
|
|
13
|
+
spanId: z.string().optional()
|
|
14
|
+
})
|
|
15
|
+
.catchall(z.any())
|
|
@@ -0,0 +1,31 @@
|
|
|
1
|
+
import { z } from 'zod'
|
|
2
|
+
|
|
3
|
+
export const ContainerAppSchema = z
|
|
4
|
+
.object({
|
|
5
|
+
properties: z
|
|
6
|
+
.object({
|
|
7
|
+
Log: z.any().optional(),
|
|
8
|
+
Stream: z.string().optional()
|
|
9
|
+
})
|
|
10
|
+
.catchall(z.any())
|
|
11
|
+
.optional()
|
|
12
|
+
})
|
|
13
|
+
.catchall(z.any())
|
|
14
|
+
|
|
15
|
+
export const FunctionAppSchema = z
|
|
16
|
+
.object({
|
|
17
|
+
message: z.any().optional()
|
|
18
|
+
})
|
|
19
|
+
.catchall(z.any())
|
|
20
|
+
|
|
21
|
+
export const DataFactorySchema = z
|
|
22
|
+
.object({
|
|
23
|
+
properties: z
|
|
24
|
+
.object({
|
|
25
|
+
PipelineName: z.string().optional(),
|
|
26
|
+
ActivityName: z.string().optional()
|
|
27
|
+
})
|
|
28
|
+
.catchall(z.any())
|
|
29
|
+
.optional()
|
|
30
|
+
})
|
|
31
|
+
.catchall(z.any())
|
|
@@ -0,0 +1,17 @@
|
|
|
1
|
+
import { z } from 'zod'
|
|
2
|
+
|
|
3
|
+
export const EventHubRecordSchema = z
|
|
4
|
+
.object({
|
|
5
|
+
time: z.string().optional(),
|
|
6
|
+
resourceId: z.string().optional(),
|
|
7
|
+
category: z.string().optional(),
|
|
8
|
+
operationName: z.string().optional(),
|
|
9
|
+
properties: z.any().optional()
|
|
10
|
+
})
|
|
11
|
+
.catchall(z.any())
|
|
12
|
+
|
|
13
|
+
export const EventHubMessageSchema = z
|
|
14
|
+
.object({
|
|
15
|
+
Records: z.array(EventHubRecordSchema)
|
|
16
|
+
})
|
|
17
|
+
.catchall(z.any())
|
|
@@ -0,0 +1,13 @@
|
|
|
1
|
+
import { z } from 'zod'
|
|
2
|
+
|
|
3
|
+
export const OTLPLogRecordSchema = z
|
|
4
|
+
.object({
|
|
5
|
+
timestamp: z.number().optional(), // Unix epoch in ms
|
|
6
|
+
body: z.any(),
|
|
7
|
+
severityNumber: z.number().optional(),
|
|
8
|
+
severityText: z.string().optional(),
|
|
9
|
+
attributes: z.record(z.any()).optional(),
|
|
10
|
+
traceId: z.string().optional(),
|
|
11
|
+
spanId: z.string().optional()
|
|
12
|
+
})
|
|
13
|
+
.catchall(z.any())
|
|
@@ -0,0 +1,32 @@
|
|
|
1
|
+
import { OTLPLogExporter } from '@opentelemetry/exporter-logs-otlp-http'
|
|
2
|
+
|
|
3
|
+
export class OTLPSender {
|
|
4
|
+
constructor(config = {}) {
|
|
5
|
+
this.exporter = new OTLPLogExporter({
|
|
6
|
+
url: config.endpoint || 'https://otlp.nr-data.net/v1/logs',
|
|
7
|
+
headers: {
|
|
8
|
+
'api-key': config.licenseKey || ''
|
|
9
|
+
}
|
|
10
|
+
})
|
|
11
|
+
}
|
|
12
|
+
|
|
13
|
+
async send(contexts) {
|
|
14
|
+
// Collect contexts that haven't been dropped and have an otlpLog payload
|
|
15
|
+
const validLogs = contexts.filter((ctx) => !ctx.dropped && ctx.otlpLog).map((ctx) => ctx.otlpLog)
|
|
16
|
+
|
|
17
|
+
if (validLogs.length === 0) {
|
|
18
|
+
return { success: true, count: 0 }
|
|
19
|
+
}
|
|
20
|
+
|
|
21
|
+
return new Promise((resolve, reject) => {
|
|
22
|
+
this.exporter.export(validLogs, (result) => {
|
|
23
|
+
if (result.code !== 0) {
|
|
24
|
+
// 0 usually maps to ExportResultCode.SUCCESS
|
|
25
|
+
reject(new Error(`OTLP Export Failed: ${result.error?.message || 'Unknown error'}`))
|
|
26
|
+
} else {
|
|
27
|
+
resolve({ success: true, count: validLogs.length })
|
|
28
|
+
}
|
|
29
|
+
})
|
|
30
|
+
})
|
|
31
|
+
}
|
|
32
|
+
}
|
|
@@ -0,0 +1,24 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* Shallowly converts the keys of an object to camelCase.
|
|
3
|
+
*
|
|
4
|
+
* @param {Object} obj The input object
|
|
5
|
+
* @returns {Object} A new object with camelCase keys
|
|
6
|
+
*/
|
|
7
|
+
export function shallowCamelCase(obj) {
|
|
8
|
+
if (!obj || typeof obj !== 'object' || Array.isArray(obj)) {
|
|
9
|
+
return obj
|
|
10
|
+
}
|
|
11
|
+
|
|
12
|
+
const result = {}
|
|
13
|
+
for (const [key, value] of Object.entries(obj)) {
|
|
14
|
+
// Convert PascalCase or snake_case to camelCase
|
|
15
|
+
const camelKey = key
|
|
16
|
+
.replace(/^_+/, '') // remove leading underscores
|
|
17
|
+
.replace(/[-_\s]+(.)?/g, (_, c) => (c ? c.toUpperCase() : '')) // kebab or snake to camel
|
|
18
|
+
.replace(/^([A-Z])/, (m) => m.toLowerCase()) // Pascal to camel
|
|
19
|
+
|
|
20
|
+
result[camelKey] = value
|
|
21
|
+
}
|
|
22
|
+
|
|
23
|
+
return result
|
|
24
|
+
}
|
|
@@ -0,0 +1,67 @@
|
|
|
1
|
+
import winston from 'winston'
|
|
2
|
+
import Transport from 'winston-transport'
|
|
3
|
+
|
|
4
|
+
class AzureContextTransport extends Transport {
|
|
5
|
+
constructor(opts) {
|
|
6
|
+
super(opts)
|
|
7
|
+
this.azureContext = opts.azureContext
|
|
8
|
+
}
|
|
9
|
+
|
|
10
|
+
log(info, callback) {
|
|
11
|
+
setImmediate(() => {
|
|
12
|
+
this.emit('logged', info)
|
|
13
|
+
})
|
|
14
|
+
|
|
15
|
+
const msg = info.message
|
|
16
|
+
const level = info.level
|
|
17
|
+
|
|
18
|
+
if (this.azureContext?.log) {
|
|
19
|
+
if (level === 'error' && typeof this.azureContext.log.error === 'function') {
|
|
20
|
+
this.azureContext.log.error(msg)
|
|
21
|
+
} else if (level === 'error' && typeof this.azureContext.error === 'function') {
|
|
22
|
+
this.azureContext.error(msg)
|
|
23
|
+
} else if (level === 'warn' && typeof this.azureContext.log.warn === 'function') {
|
|
24
|
+
this.azureContext.log.warn(msg)
|
|
25
|
+
} else if (level === 'warn' && typeof this.azureContext.warn === 'function') {
|
|
26
|
+
this.azureContext.warn(msg)
|
|
27
|
+
} else if (level === 'info' && typeof this.azureContext.log.info === 'function') {
|
|
28
|
+
this.azureContext.log.info(msg)
|
|
29
|
+
} else if (level === 'info' && typeof this.azureContext.info === 'function') {
|
|
30
|
+
this.azureContext.info(msg)
|
|
31
|
+
} else if (
|
|
32
|
+
(level === 'debug' || level === 'verbose') &&
|
|
33
|
+
typeof this.azureContext.log.verbose === 'function'
|
|
34
|
+
) {
|
|
35
|
+
this.azureContext.log.verbose(msg)
|
|
36
|
+
} else if (level === 'debug' && typeof this.azureContext.debug === 'function') {
|
|
37
|
+
this.azureContext.debug(msg)
|
|
38
|
+
} else if (level === 'verbose' && typeof this.azureContext.trace === 'function') {
|
|
39
|
+
this.azureContext.trace(msg)
|
|
40
|
+
} else {
|
|
41
|
+
this.azureContext.log(msg)
|
|
42
|
+
}
|
|
43
|
+
} else {
|
|
44
|
+
console.log(`[${level.toUpperCase()}] ${msg}`)
|
|
45
|
+
}
|
|
46
|
+
|
|
47
|
+
if (callback) {
|
|
48
|
+
callback()
|
|
49
|
+
}
|
|
50
|
+
}
|
|
51
|
+
}
|
|
52
|
+
|
|
53
|
+
export function createForwarderLogger({ level = 'info', azureContext = null } = {}) {
|
|
54
|
+
const transports = []
|
|
55
|
+
|
|
56
|
+
if (azureContext) {
|
|
57
|
+
transports.push(new AzureContextTransport({ azureContext, level }))
|
|
58
|
+
} else {
|
|
59
|
+
transports.push(new winston.transports.Console({ level }))
|
|
60
|
+
}
|
|
61
|
+
|
|
62
|
+
return winston.createLogger({
|
|
63
|
+
level,
|
|
64
|
+
format: winston.format.combine(winston.format.splat(), winston.format.simple()),
|
|
65
|
+
transports
|
|
66
|
+
})
|
|
67
|
+
}
|
|
@@ -0,0 +1,22 @@
|
|
|
1
|
+
import fs from 'fs'
|
|
2
|
+
import { fileURLToPath } from 'url'
|
|
3
|
+
import path from 'path'
|
|
4
|
+
|
|
5
|
+
const __dirname = path.dirname(fileURLToPath(import.meta.url))
|
|
6
|
+
const pkgPath = path.resolve(__dirname, '../../../package.json')
|
|
7
|
+
|
|
8
|
+
let pkg = { name: 'newrelic-sb-azure-forwarder', version: '1.0.0' }
|
|
9
|
+
|
|
10
|
+
try {
|
|
11
|
+
const raw = fs.readFileSync(pkgPath, 'utf8')
|
|
12
|
+
pkg = JSON.parse(raw)
|
|
13
|
+
} catch (e) {
|
|
14
|
+
console.warn('Could not read package.json, falling back to default package metadata.')
|
|
15
|
+
}
|
|
16
|
+
|
|
17
|
+
export const getPackageInfo = () => {
|
|
18
|
+
// Strip scope from package name if it exists (e.g. @dynova/newrelic-sb-log-forwarder -> newrelic-sb-log-forwarder)
|
|
19
|
+
const name = pkg.name ? pkg.name.split('/').pop() : 'newrelic-sb-azure-forwarder'
|
|
20
|
+
const version = pkg.version || '1.0.0'
|
|
21
|
+
return { name, version }
|
|
22
|
+
}
|