-
Notifications
You must be signed in to change notification settings - Fork 1.3k
feat: support otlp http binary protobuf format log data and update doc #3986
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
Merged
Merged
Changes from 2 commits
Commits
Show all changes
10 commits
Select commit
Hold shift + click to select a range
57dae77
feat: support otlp http binary protobuf format log data and update doc
bigcyy f26c3bd
fix: fix doc lint error
bigcyy a403425
Update home/i18n/zh-cn/docusaurus-plugin-content-docs/current/help/lo…
bigcyy 1164473
improvement: Improve OtlpLogController error catch
bigcyy 0a681fb
[feature] Add log content filtering to log stream component
zqr10159 0bd4874
feat: Add log content search
bigcyy ca67d87
fix: add log content search test
bigcyy e6d5b09
fix: add log content search test
bigcyy 5a23ce2
fix: add log content search test
bigcyy 7dd335b
Merge branch 'master' into otlp-http-binary-protobuf
zqr10159 File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or 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
145 changes: 145 additions & 0 deletions
145
hertzbeat-log/src/main/java/org/apache/hertzbeat/log/controller/OtlpLogController.java
This file contains hidden or 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,145 @@ | ||
| /* | ||
| * Licensed to the Apache Software Foundation (ASF) under one or more | ||
| * contributor license agreements. See the NOTICE file distributed with | ||
| * this work for additional information regarding copyright ownership. | ||
| * The ASF licenses this file to You 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. | ||
| */ | ||
|
|
||
| package org.apache.hertzbeat.log.controller; | ||
|
|
||
| import com.google.protobuf.InvalidProtocolBufferException; | ||
| import com.google.protobuf.util.JsonFormat; | ||
| import com.google.rpc.Status; | ||
| import io.opentelemetry.proto.collector.logs.v1.ExportLogsServiceResponse; | ||
| import io.swagger.v3.oas.annotations.Operation; | ||
| import io.swagger.v3.oas.annotations.tags.Tag; | ||
| import lombok.extern.slf4j.Slf4j; | ||
| import org.apache.hertzbeat.log.service.impl.OtlpLogProtocolAdapter; | ||
| import org.springframework.http.HttpStatus; | ||
| import org.springframework.http.MediaType; | ||
| import org.springframework.http.ResponseEntity; | ||
| import org.springframework.web.bind.annotation.PostMapping; | ||
| import org.springframework.web.bind.annotation.RequestBody; | ||
| import org.springframework.web.bind.annotation.RequestMapping; | ||
| import org.springframework.web.bind.annotation.RestController; | ||
|
|
||
| /** | ||
| * OTLP Log Ingestion Controller | ||
| * Implements OTLP/HTTP specification for log ingestion. | ||
| * Supports both binary-encoded Protobuf (application/x-protobuf) and JSON-encoded Protobuf (application/json). | ||
| * | ||
| * @see <a href="https://opentelemetry.io/docs/specs/otlp/#otlphttp">OTLP/HTTP Specification</a> | ||
| */ | ||
| @Tag(name = "OTLP Log Controller") | ||
| @RestController | ||
| @RequestMapping(path = "/api/logs/otlp") | ||
| @Slf4j | ||
| public class OtlpLogController { | ||
|
|
||
| private static final String CONTENT_TYPE_PROTOBUF = "application/x-protobuf"; | ||
|
|
||
| private static final ExportLogsServiceResponse EMPTY_RESPONSE = ExportLogsServiceResponse.newBuilder().build(); | ||
|
|
||
| private final OtlpLogProtocolAdapter otlpLogProtocolAdapter; | ||
|
|
||
| public OtlpLogController(OtlpLogProtocolAdapter otlpLogProtocolAdapter) { | ||
| this.otlpLogProtocolAdapter = otlpLogProtocolAdapter; | ||
| } | ||
|
|
||
| /** | ||
| * OTLP/HTTP standard endpoint for logs with JSON-encoded Protobuf payload. | ||
| * Content-Type: application/json | ||
| * | ||
| * Response follows OTLP specification: | ||
| * - Success: HTTP 200 with ExportLogsServiceResponse (JSON encoded) | ||
| * - Failure: HTTP 400 with google.rpc.Status (JSON encoded) | ||
| * | ||
| * @param content JSON-encoded ExportLogsServiceRequest | ||
| * @return ExportLogsServiceResponse on success, Status on failure | ||
| */ | ||
| @Operation(summary = "Ingest OTLP logs (JSON format)") | ||
| @PostMapping(value = "/v1/logs", consumes = MediaType.APPLICATION_JSON_VALUE, produces = MediaType.APPLICATION_JSON_VALUE) | ||
| public ResponseEntity<String> ingestJsonLogs(@RequestBody String content) { | ||
| log.debug("Receive OTLP JSON logs, content length: {}", content == null ? 0 : content.length()); | ||
| try { | ||
| otlpLogProtocolAdapter.ingest(content); | ||
| return ResponseEntity.ok(toJsonResponse(EMPTY_RESPONSE)); | ||
| } catch (Exception e) { | ||
| log.error("Failed to ingest OTLP JSON logs: {}", e.getMessage(), e); | ||
| return ResponseEntity.status(HttpStatus.BAD_REQUEST) | ||
| .body(toJsonErrorResponse(e.getMessage())); | ||
| } | ||
| } | ||
|
|
||
| /** | ||
| * OTLP/HTTP standard endpoint for logs with binary-encoded Protobuf payload. | ||
| * Content-Type: application/x-protobuf | ||
| * | ||
| * Response follows OTLP specification: | ||
| * - Success: HTTP 200 with ExportLogsServiceResponse (binary encoded) | ||
| * - Failure: HTTP 400 with google.rpc.Status (binary encoded) | ||
| * | ||
| * @param content binary-encoded ExportLogsServiceRequest | ||
| * @return ExportLogsServiceResponse on success, Status on failure | ||
| */ | ||
| @Operation(summary = "Ingest OTLP logs (binary Protobuf format)") | ||
| @PostMapping(value = "/v1/logs", consumes = CONTENT_TYPE_PROTOBUF, produces = CONTENT_TYPE_PROTOBUF) | ||
| public ResponseEntity<byte[]> ingestBinaryLogs(@RequestBody byte[] content) { | ||
| log.debug("Receive OTLP binary logs, content length: {}", content == null ? 0 : content.length); | ||
| try { | ||
| otlpLogProtocolAdapter.ingestBinary(content); | ||
| return ResponseEntity.ok(EMPTY_RESPONSE.toByteArray()); | ||
| } catch (Exception e) { | ||
| log.error("Failed to ingest OTLP binary logs: {}", e.getMessage(), e); | ||
| return ResponseEntity.status(HttpStatus.BAD_REQUEST) | ||
| .body(createBinaryErrorResponse(e.getMessage())); | ||
| } | ||
| } | ||
|
bigcyy marked this conversation as resolved.
Outdated
|
||
|
|
||
| private String toJsonResponse(ExportLogsServiceResponse response) { | ||
| try { | ||
| return JsonFormat.printer().print(response); | ||
| } catch (InvalidProtocolBufferException e) { | ||
| return "{}"; | ||
| } | ||
| } | ||
|
bigcyy marked this conversation as resolved.
|
||
|
|
||
| private String toJsonErrorResponse(String message) { | ||
| Status status = Status.newBuilder() | ||
| .setMessage(message != null ? message : "Unknown error") | ||
| .build(); | ||
| try { | ||
| return JsonFormat.printer().print(status); | ||
| } catch (InvalidProtocolBufferException e) { | ||
| return "{\"message\":\"" + escapeJson(message) + "\"}"; | ||
| } | ||
| } | ||
|
|
||
| private String escapeJson(String message) { | ||
| if (message == null) { | ||
| return ""; | ||
| } | ||
| return message.replace("\\", "\\\\") | ||
| .replace("\"", "\\\"") | ||
| .replace("\n", "\\n") | ||
| .replace("\r", "\\r") | ||
| .replace("\t", "\\t"); | ||
| } | ||
|
bigcyy marked this conversation as resolved.
|
||
|
|
||
| private byte[] createBinaryErrorResponse(String message) { | ||
| return Status.newBuilder() | ||
| .setMessage(message != null ? message : "Unknown error") | ||
| .build() | ||
| .toByteArray(); | ||
| } | ||
| } | ||
This file contains hidden or 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 hidden or 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
Oops, something went wrong.
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
Uh oh!
There was an error while loading. Please reload this page.